-
Notifications
You must be signed in to change notification settings - Fork 1.7k
proxy_h2: acknowledge downstream RST_STREAM while the upstream request-body write is blocked #911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
molocule
wants to merge
8
commits into
cloudflare:main
Choose a base branch
from
modal-labs:rst-stream-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+361
−17
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
922df3c
add
molocule 81be571
Update proxy_h2.rs
molocule 2d8db2a
add unit test
molocule 27aefe1
caching path
molocule 6adf921
add test
molocule c093a91
Merge branch 'main' into rst-stream-fix
molocule 6fb2e5c
Update test_basic.rs
molocule 050f826
address comments
molocule File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,9 +214,13 @@ where | |
| Ok((downstream_can_reuse, _upstream)) => (downstream_can_reuse, None), | ||
| Err(e) => { | ||
| // On application level upstream read timeouts, send RST_STREAM CANCEL, | ||
| // we know we have not received END_STREAM at this point since we read timed out | ||
| // we know we have not received END_STREAM at this point since we read timed out. | ||
| // Also cancel the upstream stream when downstream goes away/resets so the | ||
| // upstream peer can release the stream promptly. | ||
| // TODO: implement for write timeouts? | ||
| if e.esource == ErrorSource::Upstream && matches!(e.etype, ReadTimedout) { | ||
| if (e.esource == ErrorSource::Upstream && matches!(e.etype, ReadTimedout)) | ||
| || e.esource == ErrorSource::Downstream | ||
| { | ||
| client_body.send_reset(h2::Reason::CANCEL); | ||
| // Mark the underlying H2 connection for shutdown so it's not used | ||
| // for new streams in case it is hung. | ||
|
|
@@ -470,6 +474,28 @@ where | |
| Ok(request_done) => { | ||
| downstream_state.maybe_finished(request_done); | ||
| }, | ||
| Err(e) if e.esource == ErrorSource::Downstream => { | ||
| // Downstream reset/errored while the upstream write was blocked | ||
| // (e.g. on upstream flow control). Same policy as the read error | ||
| // handling above: ignore the downstream error if the upstream | ||
| // response is being admitted to cache, otherwise fail so the | ||
| // downstream stream handles are dropped promptly. | ||
| let wait_for_cache_fill = (!serve_from_cache.is_on() && support_cache_partial_read) | ||
| || serve_from_cache.is_miss(); | ||
| if !wait_for_cache_fill { | ||
| return Err(e); | ||
| } | ||
| // ignore downstream error so that upstream can continue to write cache | ||
| downstream_state.to_errored(); | ||
| warn!( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use the same pattern of checking
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| "Downstream Error ignored during caching: {}, {}", | ||
| e, | ||
| self.inner.request_summary(session, ctx) | ||
| ); | ||
| // This will not be treated as a final error, but we should signal to | ||
| // downstream session anyway. | ||
| session.downstream_session.on_proxy_failure(e); | ||
| }, | ||
| Err(e) => { | ||
| // mark request done, attempt to drain receive | ||
| warn!("Upstream h2 body send error: {e}"); | ||
|
|
@@ -897,17 +923,37 @@ where | |
| return Ok(false); | ||
| } | ||
|
|
||
| if let Some(data) = data { | ||
| debug!("Write {} bytes body to h2 upstream", data.len()); | ||
| write_body(client_body, data, end_of_body, write_timeout) | ||
| .await | ||
| .map_err(|e| e.into_up())?; | ||
| } else { | ||
| debug!("Read downstream body done"); | ||
| /* send a standalone END_STREAM flag */ | ||
| write_body(client_body, Bytes::new(), true, write_timeout) | ||
| .await | ||
| .map_err(|e| e.into_up())?; | ||
| let (data, end) = match data { | ||
| Some(data) => { | ||
| debug!("Write {} bytes body to h2 upstream", data.len()); | ||
| (data, end_of_body) | ||
| } | ||
| None => { | ||
| debug!("Read downstream body done"); | ||
| /* send a standalone END_STREAM flag */ | ||
| (Bytes::new(), true) | ||
| } | ||
| }; | ||
|
|
||
| /* Race the upstream write against a downstream stream reset. A write blocked | ||
| * on upstream flow control would otherwise keep the downstream stream handles | ||
| * referenced while a downstream RST_STREAM goes unobserved, pinning the | ||
| * downstream connection window credit until the write completes. */ | ||
| tokio::select! { | ||
| biased; | ||
| res = write_body(client_body, data, end, write_timeout) => { | ||
| res.map_err(|e| e.into_up())?; | ||
| } | ||
| reset = session.downstream_session.watch_h2_stream_reset() => { | ||
| return match reset { | ||
| Ok(reason) => Error::e_explain( | ||
| H2Error, | ||
| format!("downstream reset stream (reason: {reason}) while writing body to upstream"), | ||
| ), | ||
| Err(e) => Err(e), | ||
| } | ||
| .map_err(|e| e.into_down()); | ||
| } | ||
| } | ||
|
|
||
| Ok(end_of_body) | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer us return the future here
Option<Idle<'_>>, we then avoid the future andselectfor downstream HTTP/1.1 clients inproxy_h2. We also avoid the case where a caller mistakenly invokes this for something other than H2 outside ofselectand stays pending.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah good point, I just fixed this