-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
quic: fix readable stream truncation on stop-sending, abort & timeout #63967
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
pimterry
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
pimterry:fix-reader-after-timeout
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.
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,66 +1,55 @@ | ||
| // Flags: --experimental-quic --experimental-stream-iter --no-warnings | ||
|
|
||
| // Test: peer RESET_STREAM causes iterator to error. | ||
| // When the server resets the stream, the client's async iterator | ||
| // should throw or return early. | ||
| // Test: a peer RESET_STREAM truncates the readable. The async iterator | ||
| // delivers the data received before the reset, then throws | ||
| // ERR_QUIC_STREAM_RESET (carrying the peer's code) at the end - rather than | ||
| // ending cleanly. | ||
|
|
||
| import { hasQuic, skip, mustCall } from '../common/index.mjs'; | ||
| import * as assert from 'node:assert'; | ||
| import { setTimeout as delay } from 'node:timers/promises'; | ||
| import assert from 'node:assert'; | ||
|
|
||
| const { ok, rejects } = assert; | ||
| const { strictEqual } = assert; | ||
|
|
||
| if (!hasQuic) { | ||
| skip('QUIC is not enabled'); | ||
| } | ||
|
|
||
| const { listen, connect } = await import('../common/quic.mjs'); | ||
|
|
||
| const encoder = new TextEncoder(); | ||
|
|
||
| const serverReady = Promise.withResolvers(); | ||
|
|
||
| const serverEndpoint = await listen(mustCall((serverSession) => { | ||
| serverSession.onstream = mustCall(async (stream) => { | ||
| // Reset the stream from the server side. | ||
| stream.writer.write(new Uint8Array(1000).fill(7)); | ||
| while (stream.stats.maxOffsetAcknowledged < 1000n) await delay(5); | ||
| stream.resetStream(42n); | ||
| await rejects(stream.closed, mustCall((err) => { | ||
| assert.ok(err); | ||
| return true; | ||
| })); | ||
| serverReady.resolve(); | ||
| await serverSession.closed; | ||
| stream.closed.catch(() => {}); | ||
| }); | ||
| }), { transportParams: { maxIdleTimeout: 1 } }); | ||
| })); | ||
|
|
||
| const clientSession = await connect(serverEndpoint.address, { | ||
| transportParams: { maxIdleTimeout: 1 }, | ||
| }); | ||
| await clientSession.opened; | ||
|
|
||
| const stream = await clientSession.createBidirectionalStream({ | ||
| body: encoder.encode('will be reset by server'), | ||
| }); | ||
|
|
||
| // Set up the closed handler before the reset to avoid unhandled rejection. | ||
| const closedPromise = rejects(stream.closed, mustCall((err) => { | ||
| assert.ok(err); | ||
| return true; | ||
| })); | ||
|
|
||
| await serverReady.promise; | ||
| // Keep our write side open so the stream stays alive while we read. | ||
| const stream = await clientSession.createBidirectionalStream(); | ||
| await stream.writer.write(new Uint8Array([1])); | ||
| stream.closed.catch(() => {}); | ||
|
|
||
| // The async iterator should either throw or return early when the | ||
| // peer resets the readable side. | ||
| let received = 0; | ||
| let threw; | ||
| try { | ||
| for await (const batch of stream) { | ||
| // May receive some data before the reset arrives. | ||
| ok(Array.isArray(batch)); | ||
| for await (const chunk of stream) { | ||
| for (const c of chunk) received += c.byteLength; | ||
| } | ||
| } catch { | ||
| // The iterator may throw when the reset arrives mid-iteration. | ||
| } catch (err) { | ||
| threw = err; | ||
| } | ||
|
|
||
| // Either way, the stream should close. | ||
| await closedPromise; | ||
| await clientSession.closed; | ||
| // The buffered data was delivered before the error. | ||
| strictEqual(received, 1000); | ||
| // The reset surfaced as a reset error (with its code), not a clean end. | ||
| strictEqual(threw?.code, 'ERR_QUIC_STREAM_RESET'); | ||
|
|
||
| clientSession.close(); | ||
| await serverEndpoint.close(); |
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,58 @@ | ||
| // Flags: --experimental-quic --experimental-stream-iter --no-warnings | ||
|
|
||
| // Test: a readable stream truncated by the connection idle timeout delivers | ||
| // the data it received, then surfaces the truncation as an error at the end of | ||
| // iteration - rather than a silent clean end-of-stream that would make an | ||
| // incomplete stream look complete. | ||
|
|
||
| import { hasQuic, skip, mustCall } from '../common/index.mjs'; | ||
| import assert from 'node:assert'; | ||
|
|
||
| const { strictEqual } = assert; | ||
|
|
||
| if (!hasQuic) { | ||
| skip('QUIC is not enabled'); | ||
| } | ||
|
|
||
| const { listen, connect } = await import('../common/quic.mjs'); | ||
|
|
||
| // The body sends 1000 bytes then hangs (never a FIN), so the only thing that | ||
| // ends the client's read side is the connection idle timeout. | ||
| async function* stallingBody() { | ||
| yield new Uint8Array(1000).fill(7); | ||
| await new Promise(() => {}); | ||
| } | ||
|
|
||
| const serverEndpoint = await listen(mustCall((serverSession) => { | ||
| serverSession.onstream = mustCall((stream) => { | ||
| stream.setBody(stallingBody()); | ||
| stream.closed.catch(() => {}); | ||
| }); | ||
| })); | ||
|
|
||
| const clientSession = await connect(serverEndpoint.address, { | ||
| // Short connection idle timeout so the truncation happens quickly. | ||
| transportParams: { maxIdleTimeout: 1 }, | ||
| }); | ||
| await clientSession.opened; | ||
|
|
||
| const stream = await clientSession.createBidirectionalStream(); | ||
| await stream.writer.write(new Uint8Array([1])); | ||
| stream.closed.catch(() => {}); | ||
|
|
||
| let received = 0; | ||
| let threw; | ||
| try { | ||
| for await (const chunk of stream) { | ||
| for (const c of chunk) received += c.byteLength; | ||
| } | ||
| } catch (err) { | ||
| threw = err; | ||
| } | ||
|
|
||
| // All the buffered data was delivered before the error. | ||
| strictEqual(received, 1000); | ||
| // The truncation surfaced as an error at the end, not a clean end-of-stream. | ||
| strictEqual(threw?.code, 'ERR_QUIC_STREAM_ABORTED'); | ||
|
|
||
| await serverEndpoint.close(); |
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.
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.
This may not be new in this PR. But does stopSending really call
endReadable?I do not think that it should do this, it just says, that you should not send any more. (At least this is the behaviour I know from browsers and their W3C spec. I had a hard time more my package do this cleanly).
And actually in quic the order is not guaranteed, so the fin can come after stopsending and everything is fine.
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.
Sorry, I confused the direction.
is wrong. I meant
resetStreamin this text,And I do not see this case in the patch touched.
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.
Yes, I think you're right, comment updwted.