diff --git a/src/quic/session.cc b/src/quic/session.cc index c1b803b3f20..80c09a71426 100644 --- a/src/quic/session.cc +++ b/src/quic/session.cc @@ -1381,6 +1381,14 @@ struct Session::Impl final : public MemoryRetainer { return NGTCP2_SUCCESS; } + static int on_extend_max_data(ngtcp2_conn* conn, + uint64_t max_data, + void* user_data) { + NGTCP2_CALLBACK_SCOPE(session) + session->ExtendMaxData(max_data); + return NGTCP2_SUCCESS; + } + static int on_get_new_cid(ngtcp2_conn* conn, ngtcp2_cid* cid, ngtcp2_stateless_reset_token* token, @@ -1728,10 +1736,10 @@ struct Session::Impl final : public MemoryRetainer { on_receive_stream_stop_sending, #ifdef NGTCP2_CALLBACKS_V5 nullptr, // stream_close2 -#endif #ifdef NGTCP2_CALLBACKS_V6 - nullptr, // extend_max_data + on_extend_max_data, #endif +#endif // NGTCP2_CALLBACKS_V5 }; static constexpr ngtcp2_callbacks SERVER = { @@ -1783,10 +1791,10 @@ struct Session::Impl final : public MemoryRetainer { on_receive_stream_stop_sending, #ifdef NGTCP2_CALLBACKS_V5 nullptr, // stream_close2 -#endif #ifdef NGTCP2_CALLBACKS_V6 - nullptr, // extend_max_data + on_extend_max_data, #endif +#endif // NGTCP2_CALLBACKS_V5 }; }; @@ -3593,6 +3601,15 @@ void Session::ExtendMaxStreams(stream_id id) { } } +// Called when the Session determines that the flow control window for the +// session has been expanded. +void Session::ExtendMaxData(uint64_t max_data) { + for (auto& [id, stream] : streams()) { + if (is_destroyed()) break; + stream->UpdateWriteDesiredSize(); + } +} + void Session::Consume(stream_id id, size_t len) { ExtendStreamOffset(id, len); ExtendOffset(len); diff --git a/src/quic/session.h b/src/quic/session.h index dae2fb19d6f..ae7ff338380 100644 --- a/src/quic/session.h +++ b/src/quic/session.h @@ -526,6 +526,7 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source { CreateStreamOption option = CreateStreamOption::NOTIFY); void RemoveStream(stream_id id); + void ExtendMaxData(uint64_t max_data); void ExtendMaxStreams(stream_id id); void ResumeStream(stream_id id); void StreamDataBlocked(stream_id id); diff --git a/test/parallel/test-quic-h3-maxdata-external-buffer-failure.mjs b/test/parallel/test-quic-h3-maxdata-external-buffer-failure.mjs new file mode 100644 index 00000000000..362fdba172e --- /dev/null +++ b/test/parallel/test-quic-h3-maxdata-external-buffer-failure.mjs @@ -0,0 +1,75 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: Quic maxdata updates on http/3 +// Client sends a body that precisely fills the session window size, +// and verifies that it is data transfer is not stalled. + +import { hasQuic, skip } from '../common/index.mjs'; +import { readFile } from 'node:fs/promises'; +import { setTimeout as sleep } from 'node:timers/promises'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); +const { drainableProtocol } = await import('stream/iter'); + +const keys = 'test/fixtures/keys'; +const key = createPrivateKey(await readFile(`${keys}/agent1-key.pem`)); +const cert = await readFile(`${keys}/agent1-cert.pem`); + +const WINDOW = 4096; +// Fills the window exactly: +// considers all framing including some initial session capsules +const BODY = WINDOW - 38; + +let letServerRead; +const serverMayRead = new Promise((resolve) => { letServerRead = resolve; }); + +const endpoint = await listen((session) => { + session.onstream = async (stream) => { + await serverMayRead; + for await (const _ of stream) { /* reading extends the window */ } + }; +}, { + sni: { '*': { keys: [key], certs: [cert] } }, + transportParams: { + initialMaxStreamDataBidiRemote: 1024 * 1024, // Make sure maxstreamdata does not block + initialMaxData: WINDOW, + }, + onheaders() { this.sendHeaders({ ':status': '200' }); }, +}); + +const session = await connect(endpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', +}); +await session.opened; + +// Budget well above the window, so the window is what stops the writer. +const stream = await session.createBidirectionalStream({ budget: 1024 * 1024 }); +stream.sendHeaders({ + ':method': 'POST', + ':path': '/', + ':scheme': 'https', + ':authority': 'localhost', +}, { terminal: false }); + +const writer = stream.writer; +writer.writeSync(new Uint8Array(BODY)); + +// Long enough for every byte to be acked. The peer acks as data arrives, +// whether or not its application has read any of it, so by now the window is +// exhausted, the send buffer is empty, and no further ACK can arrive. +await sleep(500); + +const watchdog = setTimeout(() => { + console.error('STALLED: no drain after MAX_STREAM_DATA'); + process.exit(1); +}, 5000); +letServerRead(); // Extend the window, with no ack attached +await writer[drainableProtocol](); + +clearTimeout(watchdog); +process.exit(0); diff --git a/test/parallel/test-quic-maxdata-external-buffer-failure.mjs b/test/parallel/test-quic-maxdata-external-buffer-failure.mjs new file mode 100644 index 00000000000..0a367154b15 --- /dev/null +++ b/test/parallel/test-quic-maxdata-external-buffer-failure.mjs @@ -0,0 +1,51 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: Quic maxdata updates on raw quic +// Client sends a body that precisely fills the session window size, +// and verifies that it is data transfer is not stalled. + +import { hasQuic, skip } from '../common/index.mjs'; +import * as assert from 'node:assert'; +import { readFileSync } from 'node:fs'; +import { setImmediate } from 'node:timers/promises'; + + +if (!hasQuic) { + skip('QUIC is not enabled'); +} +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); + +const tls = { + keys: [createPrivateKey(readFileSync('test/fixtures/keys/agent1-key.pem'))], + certs: [readFileSync('test/fixtures/keys/agent1-cert.pem')], +}; +const WINDOW = 4096; +const serverRead = Promise.withResolvers(); + +// Server: holds the data unread (so no MAX_DATA window updates) until told to read. +const endpoint = await listen((session) => { + session.onstream = async (stream) => { + await serverRead.promise; + for await (const _ of stream); + }; +}, { alpn: 'x', sni: { '*': tls }, transportParams: { initialMaxData: WINDOW } }); + +const session = await connect(endpoint.address, { alpn: 'x', verifyPeer: 'manual' }); +const stream = session.createBidirectionalStream(); +const writer = stream.writer; + +// Fill the connection window exactly. +writer.writeSync(new Uint8Array(WINDOW)); +// Wait for ack, so nothing is in flight. +while (stream.stats.maxOffsetAcknowledged < WINDOW) await setImmediate(); +console.log('window full, canWrite:', writer.canWrite); // false, as expected + +// Server reads -> sends MAX_DATA -> window reopens. +serverRead.resolve(); +// Wait for the writer to drain, or give up after 1s if it never does. +await writer[Symbol.for('Stream.drainableProtocol')](); + +// If the client handles MAX_DATA correctly, this should be true: +assert.ok(writer.canWrite, 'window should have reopened'); +process.exit(0);