Fixed connection release when body isn't run, as well as thread affinity #3261
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
This PR fixes three issues in the async-http-client implementation:
body
of the response was never sequenced, the upstream connection would never be closed and would ultimately end up stuck in TCPCLOSE_WAIT
Response
was run on an AHC thread, rather than on the pool governed by the relevantContextShift
. In order to avoid breaking bincompat, this was fixed by forking a fiber to run theasync
callbackbody
of an HTTP was empty, the prior implementation could deadlock. This is fixed by adding a callback toonComplete
. Note that, by law, Cats Effectasync
callbacks ignore repeated invocations, so it's safe to put this here without a latch to guard it.It's worth noting that there is still a known issue with this fix: namely, if you start running the
body
stream and then the controlling fiber is almost immediatelycancel
ed, then the connection can leak. This can happen because we cannot uninterruptibly callsubscribe
and start the stream (which would register the finalizer that runscancel
). The only way to fix this is to change fs2-reactive-streams'.stream
function to take anF[Unit]
representing the subscription effect, which it would then sequence and manage itself in a properly bracketed fashion.This case is very unlikely, and at least things are better than they were.