Skip to content

Commit e1e016b

Browse files
authored
Fix HTTPClient crash when download fails before shutdown (#837)
Currently, when `FileDownloader.downloadFile()` encounters an error during `client.execute()` (e.g., XPC connection interruption or network timeout), the `HTTPClient` instance is deallocated without calling `shutdown()`, resulting in a crash: `AsyncHTTPClient/HTTPClient.swift:187: Fatal error: Client not shut down before the deinit.` This caused to a control flow issue when attempting to download the default kernel during `container system start` and, under certain conditions (e.g., network latency), encountering the timeout error `HTTPClientError.connectTimeout`. To address this, `client.execute()` is now wrapped in a do/catch to ensure `shutdown()` is always called on both success and error paths. Also I've added an explicit timeout configuration (30s connect, no read timeout) to better accommodate large file downloads.
1 parent cd7c3a1 commit e1e016b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Sources/ContainerClient/FileDownloader.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,23 @@ public struct FileDownloader {
5050
})
5151

5252
let client = FileDownloader.createClient(url: url)
53-
_ = try await client.execute(request: request, delegate: delegate).get()
53+
do {
54+
_ = try await client.execute(request: request, delegate: delegate).get()
55+
} catch {
56+
try? await client.shutdown()
57+
throw error
58+
}
5459
try await client.shutdown()
5560
}
5661

5762
private static func createClient(url: URL) -> HTTPClient {
5863
var httpConfiguration = HTTPClient.Configuration()
64+
// for large file downloads we keep a generous connect timeout, and
65+
// no read timeout since download durations can vary
66+
httpConfiguration.timeout = HTTPClient.Configuration.Timeout(
67+
connect: .seconds(30),
68+
read: .none
69+
)
5970
if let host = url.host {
6071
let proxyURL = ProxyUtils.proxyFromEnvironment(scheme: url.scheme, host: host)
6172
if let proxyURL, let proxyHost = proxyURL.host {

0 commit comments

Comments
 (0)