async-http-client icon indicating copy to clipboard operation
async-http-client copied to clipboard

Fix thread leak in `FileDownloadDelegate`

Open dnadoba opened this issue 3 years ago • 1 comments

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

dnadoba avatar Aug 12 '22 13:08 dnadoba

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.

dnadoba avatar Aug 12 '22 14:08 dnadoba

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.

dnadoba avatar Aug 16 '22 12:08 dnadoba

@Lukasa test added in https://github.com/swift-server/async-http-client/pull/614/commits/9ee5c82cfbf732d7cf2aab533c1f35392b8d8680

dnadoba avatar Aug 17 '22 16:08 dnadoba