-
Notifications
You must be signed in to change notification settings - Fork 5k
fetch: abort the request when the response body reader is cancelled #33231
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
67b0af3
fetch: abort the request when the response body reader is cancelled
robobun 03692e2
test: move #33227 regression test to its own file
robobun c1ece27
fetch: read has_more under the mutex, dedupe abort, skip resume on abort
robobun db50342
test: update backpressure cancel test for the abort-on-cancel behavior
robobun 9efbf56
test: drop host-dependent byte-count assertion in backpressure cancel…
robobun e00e888
fetch: route cert-check aborts through abort_task; drop timing wait i…
robobun 606edae
fetch: condense cancel-path comments to the 3-line limit
robobun c2917fc
fetch: abort unconditionally on reader.cancel(), dropping the has_mor…
robobun 822afc7
fetch: drop inaccurate clause from cancel-path comment
robobun 85ecd3f
Merge branch 'main' into farm/469333cb/fetch-reader-cancel-abort
Jarred-Sumner 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
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { sleep } from "bun"; | ||
| import { expect, test } from "bun:test"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/33227 | ||
| test("reader.cancel() aborts the fetch and triggers the server's stream cancel", async () => { | ||
| const { promise: sawAbort, resolve: onAbort } = Promise.withResolvers<void>(); | ||
| const { promise: sawCancel, resolve: onCancel } = Promise.withResolvers<void>(); | ||
| const encoder = new TextEncoder(); | ||
|
|
||
| using server = Bun.serve({ | ||
| port: 0, | ||
| fetch(request) { | ||
| request.signal.addEventListener("abort", () => onAbort()); | ||
| let count = 0; | ||
| return new Response( | ||
| new ReadableStream({ | ||
| // Stream forever (paced) so the body is always in flight: the unfixed | ||
| // drain path keeps the connection open and actively reading, so the | ||
| // server never sees a close unless the cancel actually aborts. | ||
| async pull(controller) { | ||
| controller.enqueue(encoder.encode(`data: ${count++}\n\n`)); | ||
| await sleep(10); | ||
| }, | ||
| cancel() { | ||
| onCancel(); | ||
| }, | ||
| }), | ||
| { headers: { "Content-Type": "text/event-stream" } }, | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const res = await fetch(`http://localhost:${server.port}`, { method: "POST" }); | ||
| const reader = res.body!.getReader(); | ||
|
|
||
| const first = await reader.read(); | ||
| expect(first.done).toBe(false); | ||
| expect(first.value!.byteLength).toBeGreaterThan(0); | ||
|
|
||
| await reader.cancel(); | ||
|
|
||
| // Cancelling the reader must abort the fetch and close the connection, so the | ||
| // server observes request.signal's abort and runs the body stream's cancel(). | ||
| await Promise.all([sawAbort, sawCancel]); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.