Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/quic/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
};
};

Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/quic/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
75 changes: 75 additions & 0 deletions test/parallel/test-quic-h3-maxdata-external-buffer-failure.mjs
Comment thread
pimterry marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -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);
51 changes: 51 additions & 0 deletions test/parallel/test-quic-maxdata-external-buffer-failure.mjs
Original file line number Diff line number Diff line change
@@ -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);
Loading