async-http-client
async-http-client copied to clipboard
Fix thread leak in `FileDownloadDelegate`
Fixes https://github.com/swift-server/async-http-client/issues/364
Modification
- remember if we own the thread pool or not.
- shutdown the thread pool if we own it after we are done with it
Result
we no longer leak one thread per download
API breakage failure is a false positive
16:14:03 1 breaking change detected in AsyncHTTPClient:
16:14:03 💔 API breakage: constructor FileDownloadDelegate.init(path:pool:reportHead:reportProgress:) has removed default argument from parameter 1
The change in question can be simplified to:
struct Foo {
// old init
init(bar: Int = 0) {}
// new inits
init() {}
init(bar: Int) {}
}
replacing a default argument with two inits, one with the argument and one without, is an ABI breaking change but not an API breaking change.
I have changed the implementation because we would otherwise only ever use one thread for all file IO. This is not good because file IO can block the thread and there is no easy way of making it non-blocking. We therefore initialize the thread pool with ProcessInfo.processInfo.processorCount threads. As this is potentially much more expensive operation compared to just creating one thread we also delay initialization until the first actual write.
@Lukasa test added in https://github.com/swift-server/async-http-client/pull/614/commits/9ee5c82cfbf732d7cf2aab533c1f35392b8d8680