Skip to content
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

fix(ext/fetch): handle errors in req body stream #17081

Merged

Conversation

lucacasonato
Copy link
Member

Right now an error in a request body stream causes an uncatchable
global promise rejection. This PR fixes this to instead propagate the
error correctly into the promise returned from fetch.

It additionally fixes errored readable stream bodies being treated as
successfully completed bodies by Rust.

Right now an error in a request body stream causes an uncatchable
global promise rejection. This PR fixes this to instead propagate the
error correctly into the promise returned from `fetch`.

It additionally fixes errored readable stream bodies being treated as
successfully completed bodies by Rust.
cli/tests/unit/fetch_test.ts Outdated Show resolved Hide resolved
cli/tests/unit/fetch_test.ts Outdated Show resolved Hide resolved
ext/fetch/26_fetch.js Outdated Show resolved Hide resolved
ext/fetch/lib.rs Outdated Show resolved Hide resolved
@lucacasonato lucacasonato requested a review from ry December 19, 2022 10:37
ext/fetch/lib.rs Outdated
Box::pin(async move {
let body = RcRef::map(&self, |r| &r.body).borrow_mut().await;
let cancel = RcRef::map(self, |r| &r.cancel);
body.send(None).or_cancel(cancel).await?.ok(); // we don't care if the receiver is closed, because this is the shutdown
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably not ignore this. If the receiver left before receiving the EOF it is still a failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not always - there is one case where hyper knows the size of the response body up front (through content-length header on the resp), where it will drop the body once that content length has been reached, regardless of if the stream is complete or not. This is expected behaviour, but it means that if you stream a body with an up front known size (eg a Blob), shutdown will never succeed because the body (and by extension the receiver) will have dropped by the time we try to shutdown.

I could try to wire it up to only swallow the error if this specific case has happened (might be a bit tricky to detect though).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not correct. You can see this in action using this playground: https://dash.deno.com/playground/slow-sheep-54
TL;DR: Content-Length too small results in an errored response stream.

🦕 curl https://slow-sheep-54.deno.dev/     # No Content-Length
Hello World%                                                                    
🦕 curl https://slow-sheep-54.deno.dev/11   # Content-Length accurate.
Hello World%                                                                    
🦕 curl https://slow-sheep-54.deno.dev/100  # Content-Length larger than response.
curl: (92) HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
🦕 curl https://slow-sheep-54.deno.dev/1    # Content-Length smaller than response.
curl: (92) HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)

done = res.done;
val = res.value;
} catch (err) {
if (terminator.aborted) break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't want to send an error to the response stream if terminated?

Copy link
Member Author

@lucacasonato lucacasonato Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I do - if terminator.aborted is set, core.shutdown won't be called further down in the code. It uses a different error throwing path though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the same error paths, multiple paths lead to mistakes and difficult-to-understand code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I want to refactor the terminator code paths in a follow up.

@@ -200,6 +200,8 @@
}
terminator[abortSignal.add](onAbort);

let requestSendError;
let requestSendErrorSet = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So cumbersome to use 2 variables for this.
If you're concerned that someone might throw undefined you could use a magic value (e.g. A symbol) to indicate "no error".

resp = await opFetchSend(requestRid);
} catch (err) {
if (terminator.aborted) return;
if (requestSendErrorSet) {
Copy link
Member

@piscisaureus piscisaureus Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So requestSendError gets ignored if opFetchSend() succeeds?
Help me understand why.

Copy link
Member Author

@lucacasonato lucacasonato Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - there is a follow up to be done here that propagates this error into the resp body ReadableStream - but I haven't quite figured out how to wire that up. This is what the TODO I added is about.

Copy link
Member

@piscisaureus piscisaureus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few comments but mostly LGTM.
I remember this code and it's implentation is very hairy, it feels like we must be doing something wrong.

Copy link
Member

@piscisaureus piscisaureus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lucacasonato lucacasonato merged commit 43b6390 into denoland:main Dec 19, 2022
@lucacasonato lucacasonato deleted the fetch_req_body_stream_uncaught branch December 19, 2022 11:49
bartlomieju pushed a commit that referenced this pull request Jan 5, 2023
Right now an error in a request body stream causes an uncatchable
global promise rejection. This PR fixes this to instead propagate the
error correctly into the promise returned from `fetch`.

It additionally fixes errored readable stream bodies being treated as
successfully completed bodies by Rust.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants