Fix HTTP/2 RST_STREAM behaviour, add auto-drain, deprecate 'aborted', fix related compat API issues#63249
Open
pimterry wants to merge 7 commits into
Open
Fix HTTP/2 RST_STREAM behaviour, add auto-drain, deprecate 'aborted', fix related compat API issues#63249pimterry wants to merge 7 commits into
pimterry wants to merge 7 commits into
Conversation
Signed-off-by: Tim Perry <pimterry@gmail.com>
Previously, stream errors were completely swallowed and never exposed. With this change, they're exposed only if there is a listener for them - this is the exact same pattern used in HTTP/1 itself, so hopefully a good fit for the compat API! This also changes the compat API to only report 'finish' when the writable has actually finished - previously all closes were reporting with finish, even when the writable was aborted part way through.
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #63249 +/- ##
==========================================
+ Coverage 90.02% 90.03% +0.01%
==========================================
Files 713 713
Lines 224950 225026 +76
Branches 42530 42561 +31
==========================================
+ Hits 202513 202604 +91
+ Misses 14220 14192 -28
- Partials 8217 8230 +13
🚀 New features to boost your workflow:
|
Member
Author
|
Pushed updates to the deprecation docs, tightened the tests a little, and covered an extra case: it turns out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.Suggestion cannot be applied right now. Please check back later.
This PR replaces #62923, which aims to fix #56627. The goal here is to make hard-shutdown (RST_STREAM messages) for HTTP/2 streams behave predictably and remove a bunch of footguns. This became a bit of a rabbit hole, but I think I have a good understanding of the problem and a much better behaviour that should be workable, although is definitely requires some breaking changes (though ~all affected code is broken in some ways today anyway).
To implement this, this PR does a few related things:
'error'when we receiveRST_STREAM(hard-stopping the stream in both directions) in specific cases only:'response'handler before the response arrives.'data'/pipe/etc).abortedevent & field, while preserving their current behaviour for now. These are confusing to the point of being effectively broken, they're already deprecated in HTTP/1, and they're not necessary.This fixes a few major existing issues. Most notably, without this PR, if you have a stream where you're finished writing but you're only part way through reading (e.g. normal client request flow) and you get a non-error RST_STREAM (a remote stream cancellation) then we close the readable cleanly, with no errors. In effect, we currently truncate cancelled responses with no warning whatsoever. The existing
'aborted'event is only emitted for incomplete writes, not reads.This resolves that issue for reads. For writes, it stays close to existing behaviour but relaxes one key case: if you've read everything, and receive a RST_STREAM cancellation while writing (most commonly: a client aborts a request while a server is responding) then the writable closes immediately but doesn't emit an 'error' event. Effectively, we treat this as a quiet shutdown. This roughly matches HTTP/1 behaviour, and avoid spurious errors server-side. Even though there's no
'error'event, it's still easy to detect this: if you get'close' without'finish'(withoutwritableFinishedbeing true) while writing then the client has cancelled the request.We could make
abortedmatch the error behaviour here, but I've left it as is to avoid churn there, since I think we should deprecate it regardless so there's no point making unnecessary breaking changes in the meantime.In the compat API, there's two other related existing problems this fixes:
'finish'when the stream closes - even if the write was cancelled (and so never actually finished) by either peer. We now emit finish only if the writable finished.streamErrorevent, to deal with people not handling'error', and then in http2: refactor close/destroy for Http2Stream and Http2Session #17406 we removedstreamErrorentirely. The HTTP/1'aborted'event does fire in most (all?) of these cases, but that's deprecated already anyway.As a happy side bonus, as part of this work I hunted down the underlying issue for #58252: buggy cleanup of reset streams. This includes a reliable repro for the underlying issue (
test/parallel/test-http2-server-stream-destroy-after-reset.js) and fixes that test and the flaky one as well, through with better cleanup behaviour in onStreamClose.I suspect there'll be some debate around this, and it is definitely a non-trivial breaking change, but on the flip side this does show some quite substantial major issues in the current APIs, including silent read & write data loss everywhere, so I think it's worthwhile.