-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1403,13 +1403,6 @@ BaseObjectPtr<Blob::Reader> Stream::get_reader() { | |
| return reader; | ||
| } | ||
|
|
||
| void Stream::set_final_size(uint64_t final_size) { | ||
| DCHECK_IMPLIES(state()->fin_received == 1, | ||
| final_size <= STAT_GET(Stats, final_size)); | ||
| state()->fin_received = 1; | ||
| STAT_SET(Stats, final_size, final_size); | ||
| } | ||
|
|
||
| void Stream::set_outbound(std::shared_ptr<DataQueue> source) { | ||
| if (!source || !is_writable()) return; | ||
| Debug(this, "Setting the outbound data source"); | ||
|
|
@@ -1605,13 +1598,20 @@ void Stream::EndWritable() { | |
| state()->write_ended = 1; | ||
| } | ||
|
|
||
| void Stream::EndReadable(std::optional<uint64_t> maybe_final_size) { | ||
| void Stream::EndReadable(std::optional<uint64_t> maybe_final_size, | ||
| bool clean_fin) { | ||
|
Member
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. Nit: I know there are a few places where bool args are used but I've been wanting to go through and clean those up... I really don't like bool args :-P ... consider it non-blocking tho. |
||
| if (!is_readable()) return; | ||
| state()->read_ended = 1; | ||
| // fin_received marks a clean completion of the read side (a real FIN). Any | ||
| // unclean end (reset/abort/session teardown) truncates the stream, which | ||
| // the JS reader will expose as a read error later. | ||
| if (clean_fin) state()->fin_received = 1; | ||
| // Flush any accumulated data before capping so the reader can see it. | ||
| FlushAccumulation(); | ||
| set_final_size(maybe_final_size.value_or(STAT_GET(Stats, bytes_received))); | ||
| inbound_->cap(STAT_GET(Stats, final_size)); | ||
| const uint64_t final_size = | ||
| maybe_final_size.value_or(STAT_GET(Stats, bytes_received)); | ||
| STAT_SET(Stats, final_size, final_size); | ||
| inbound_->cap(final_size); | ||
| // Notify the JS reader so it can see EOS. Pass fin=true so the | ||
| // wakeup promise resolves with a value the iterator can check to | ||
| // avoid waiting for another wakeup that will never come. | ||
|
|
@@ -1640,8 +1640,9 @@ void Stream::Destroy(QuicError error) { | |
| // End the writable before marking as destroyed. | ||
| EndWritable(); | ||
|
|
||
| // Also end the readable side if it isn't already. | ||
| EndReadable(); | ||
| // Also end the readable side if it isn't already. If not already ended, | ||
| // this will eventually surface as a error, since the data is truncated. | ||
| EndReadable(std::nullopt, /* clean_fin = */ false); | ||
|
|
||
| // We are going to release our reference to the outbound_ queue here. | ||
| outbound_.reset(); | ||
|
|
@@ -1688,7 +1689,7 @@ void Stream::ReceiveData(const uint8_t* data, | |
| // end the readable side if this is the last bit of data we've received. | ||
| Debug(this, "Receiving %zu bytes of data", len); | ||
| if (state()->read_ended == 1 || len == 0) { | ||
| if (flags.fin) EndReadable(); | ||
| if (flags.fin) EndReadable(std::nullopt, /* clean_fin = */ true); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -1761,7 +1762,7 @@ void Stream::ReceiveData(const uint8_t* data, | |
|
|
||
| if (flags.fin) { | ||
| FlushAccumulation(); | ||
| EndReadable(); | ||
| EndReadable(std::nullopt, /* clean_fin = */ true); | ||
| } else if (reader_ && was_empty) { | ||
| // Notify the reader once when the accumulator transitions from empty | ||
| // to non-empty. This wakes the reader exactly once per accumulation | ||
|
|
@@ -1800,7 +1801,7 @@ void Stream::ReceiveStreamReset(uint64_t final_size, QuicError error) { | |
| final_size, | ||
| error); | ||
| state()->reset_code = error.code(); | ||
| EndReadable(final_size); | ||
| EndReadable(final_size, /* clean_fin = */ false); | ||
|
Member
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. This |
||
| EmitReset(error); | ||
| } | ||
|
|
||
|
|
@@ -1823,7 +1824,7 @@ void Stream::DoStreamReset(error_code code) { | |
| } | ||
|
|
||
| void Stream::SendStopSending(error_code code) { | ||
| EndReadable(); | ||
| EndReadable(std::nullopt, /* clean_fin = */ false); | ||
|
|
||
| if (!is_pending()) { | ||
| // If the stream is a local unidirectional there's nothing to do here. | ||
|
|
||
| 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(); |
| 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(); |
Uh oh!
There was an error while loading. Please reload this page.