quic: fix stop sending behaviour & callback

Signed-off-by: Tim Perry <pimterry@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64710
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Tim Perry authored and Antoine du Hamel committed 2026-08-05 11:15:12 +02:00
1 parent 2632d606bb
commit 7de3d095b6
15 files changed
+193 -164

No files matched your search

+21 -8
View File
@@ -2031,8 +2031,7 @@ added: v23.8.0
The callback to invoke when the peer aborts a direction of the stream by
sending a `RESET_STREAM` frame (the peer abandons their writable side, so
no further data will arrive on our readable side) or a `STOP_SENDING`
frame (the peer asks us to stop writing on our writable side).
no further data will arrive on our readable side).
The callback receives a Node.js error whose `errorCode` (`bigint`)
property carries the application error code from the wire frame.
@@ -2043,6 +2042,21 @@ continue using the still-active direction on a bidirectional stream),
abort the other direction with [`writer.fail()`][], or tear down the
whole stream with [`stream.destroy()`][]. Read/write.
### `stream.onstopsending`
<!-- YAML
added: REPLACEME
-->
* Type: {quic.OnStreamErrorCallback}
The callback to invoke when the peer aborts a direction of the stream by
sending a `STOP_SENDING` frame (the peer asks us to stop writing on our
writable side).
The callback receives a Node.js error whose `errorCode` (`bigint`)
property carries the application error code from the wire frame. Read/write.
### `stream.headers`
<!-- YAML
@@ -3587,8 +3601,8 @@ functions. If a callback throws synchronously or returns a promise that
rejects, the error is caught and the owning session or stream is destroyed
with that error:
* Stream callbacks (`onblocked`, `onreset`, `onheaders`, `ontrailers`,
`oninfo`, `onwanttrailers`): the stream is destroyed.
* Stream callbacks (`onblocked`, `onreset`, `onstopsending`, `onheaders`,
`ontrailers`, `oninfo`, `onwanttrailers`): the stream is destroyed.
* Session callbacks (`onapplication`, `onstream`, `ondatagram`,
`ondatagramstatus`, `onpathvalidation`, `onsessionticket`,
`onnewtoken`, `onversionnegotiation`, `onorigin`, `ongoaway`,
@@ -4457,10 +4471,9 @@ added: v26.2.0
* `session` {quic.QuicSession}
* `error` {any} The QUIC error associated with the reset.
Published when a stream receives a STOP\_SENDING or RESET\_STREAM frame
from the peer, indicating the peer has aborted the stream. This is a
key signal for diagnosing application-level issues such as cancelled
requests.
Published when a stream receives a RESET\_STREAM frame from the peer,
indicating the peer has aborted its sending direction. This is a key signal
for diagnosing application-level issues such as cancelled requests.
### Channel: `quic.stream.blocked`
+50
View File
@@ -12,6 +12,7 @@ const {
DataViewPrototypeGetByteLength,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
FunctionPrototypeCall,
Number,
ObjectDefineProperties,
ObjectKeys,
@@ -210,6 +211,7 @@ const {
kSendHeaders,
kSessionApplication,
kSessionTicket,
kStopSending,
kTrailers,
kVersionNegotiation,
kInspect,
@@ -991,6 +993,14 @@ setCallbacks({
this[kOwner][kReset](error);
},
onStreamStopSending(error) {
if (error !== undefined) {
error = convertQuicError(error);
}
debug('stream stop sending callback', this[kOwner], error);
this[kOwner][kStopSending](error);
},
onStreamHeaders(headers, kind) {
// Called when the stream C++ handle has received a full block of headers.
debug(`stream ${this[kOwner].id} headers callback`, headers, kind);
@@ -1576,6 +1586,7 @@ class QuicStream {
onerror: undefined,
onblocked: undefined,
onreset: undefined,
onstopsending: undefined,
onheaders: undefined,
ontrailers: undefined,
oninfo: undefined,
@@ -1781,6 +1792,25 @@ class QuicStream {
}
}
/** @type {OnStreamErrorCallback} */
get onstopsending() {
assertIsQuicStream(this);
return this.#inner.onstopsending;
}
set onstopsending(fn) {
assertIsQuicStream(this);
const inner = this.#inner;
if (fn === undefined) {
inner.onstopsending = undefined;
inner.state.wantsStopSending = false;
} else {
validateFunction(fn, 'onstopsending');
inner.onstopsending = FunctionPrototypeBind(fn, this);
inner.state.wantsStopSending = true;
}
}
/** @type {OnHeadersCallback} */
get onheaders() {
assertIsQuicStream(this);
@@ -2143,6 +2173,19 @@ class QuicStream {
}
};
const onStopSending = stream[kStopSending];
stream[kStopSending] = (reason) => {
if (!closed && !errored) {
errored = true;
error = reason;
if (drainWakeup != null) {
drainWakeup.reject(error);
drainWakeup = null;
}
}
FunctionPrototypeCall(onStopSending, stream, reason);
};
// A note on backpressure handling: per the stream/iter spec, the default
// backpressure policy for writers is strict, meaning that if the stream
// signals backpressure additional writes are rejected until the buffer has
@@ -2543,6 +2586,7 @@ class QuicStream {
inner.pendingClose.resolve = undefined;
inner.onblocked = undefined;
inner.onreset = undefined;
inner.onstopsending = undefined;
inner.onheaders = undefined;
inner.onerror = undefined;
inner.ontrailers = undefined;
@@ -2596,6 +2640,12 @@ class QuicStream {
safeCallbackInvoke(inner.onreset, this, error);
}
[kStopSending](error) {
const inner = this.#inner;
assert(inner.onstopsending, 'Unexpected stop sending event');
safeCallbackInvoke(inner.onstopsending, this, error);
}
[kHeaders](headers, kind) {
const block = parseHeaderPairs(headers);
const kindName = kHeadersKindName[kind] ?? kind;
+24
View File
@@ -101,6 +101,7 @@ const {
IDX_STATE_STREAM_WANTS_BLOCK,
IDX_STATE_STREAM_WANTS_HEADERS,
IDX_STATE_STREAM_WANTS_RESET,
IDX_STATE_STREAM_WANTS_STOP_SENDING,
IDX_STATE_STREAM_WANTS_TRAILERS,
IDX_STATE_STREAM_RECEIVED_EARLY_DATA,
IDX_STATE_STREAM_WRITE_DESIRED_SIZE,
@@ -142,6 +143,7 @@ assert(IDX_STATE_STREAM_HAS_READER !== undefined);
assert(IDX_STATE_STREAM_WANTS_BLOCK !== undefined);
assert(IDX_STATE_STREAM_WANTS_HEADERS !== undefined);
assert(IDX_STATE_STREAM_WANTS_RESET !== undefined);
assert(IDX_STATE_STREAM_WANTS_STOP_SENDING !== undefined);
assert(IDX_STATE_STREAM_WANTS_TRAILERS !== undefined);
assert(IDX_STATE_STREAM_WRITE_DESIRED_SIZE !== undefined);
assert(IDX_STATE_STREAM_RESET_CODE !== undefined);
@@ -826,6 +828,24 @@ class QuicStreamState {
DataViewPrototypeSetUint8(handle, this.#offset + IDX_STATE_STREAM_WANTS_RESET, val ? 1 : 0);
}
/** @type {boolean} */
get wantsStopSending() {
const handle = this.#handle;
if (handle === undefined) return undefined;
return DataViewPrototypeGetUint8(
handle, this.#offset + IDX_STATE_STREAM_WANTS_STOP_SENDING) !== 0;
}
/** @type {boolean} */
set wantsStopSending(val) {
const handle = this.#handle;
if (handle === undefined) return;
DataViewPrototypeSetUint8(
handle,
this.#offset + IDX_STATE_STREAM_WANTS_STOP_SENDING,
val ? 1 : 0);
}
/** @type {boolean} */
get wantsTrailers() {
const handle = this.#handle;
@@ -903,6 +923,7 @@ class QuicStreamState {
hasReader,
wantsBlock,
wantsReset,
wantsStopSending,
wantsHeaders,
wantsTrailers,
early,
@@ -923,6 +944,7 @@ class QuicStreamState {
hasReader,
wantsBlock,
wantsReset,
wantsStopSending,
wantsHeaders,
wantsTrailers,
early,
@@ -960,6 +982,7 @@ class QuicStreamState {
hasReader,
wantsBlock,
wantsReset,
wantsStopSending,
wantsHeaders,
wantsTrailers,
early,
@@ -980,6 +1003,7 @@ class QuicStreamState {
hasReader,
wantsBlock,
wantsReset,
wantsStopSending,
wantsHeaders,
wantsTrailers,
early,
+2
View File
@@ -57,6 +57,7 @@ const kReset = Symbol('kReset');
const kSendHeaders = Symbol('kSendHeaders');
const kSessionApplication = Symbol('kSessionApplication');
const kSessionTicket = Symbol('kSessionTicket');
const kStopSending = Symbol('kStopSending');
const kTrailers = Symbol('kTrailers');
const kVersionNegotiation = Symbol('kVersionNegotiation');
@@ -93,6 +94,7 @@ module.exports = {
kSendHeaders,
kSessionApplication,
kSessionTicket,
kStopSending,
kTrailers,
kVersionNegotiation,
};
+1
View File
@@ -61,6 +61,7 @@ class SessionManager;
V(stream_drain, StreamDrain) \
V(stream_headers, StreamHeaders) \
V(stream_reset, StreamReset) \
V(stream_stop_sending, StreamStopSending) \
V(stream_trailers, StreamTrailers)
// The various JS strings the implementation uses.
+11 -11
View File
@@ -1611,11 +1611,11 @@ struct Session::Impl final : public MemoryRetainer {
return NGTCP2_SUCCESS;
}
static int on_stream_stop_sending(ngtcp2_conn* conn,
stream_id stream_id,
error_code app_error_code,
void* user_data,
void* stream_user_data) {
static int on_receive_stream_stop_sending(ngtcp2_conn* conn,
stream_id stream_id,
error_code app_error_code,
void* user_data,
void* stream_user_data) {
NGTCP2_CALLBACK_SCOPE(session)
auto* stream = Stream::From(stream_user_data);
if (stream == nullptr) return NGTCP2_SUCCESS;
@@ -1652,7 +1652,7 @@ struct Session::Impl final : public MemoryRetainer {
static constexpr ngtcp2_callbacks CLIENT = {
ngtcp2_crypto_client_initial_cb,
nullptr,
nullptr, // stream_stop_sending
ngtcp2_crypto_recv_crypto_data_cb,
on_handshake_completed,
on_receive_version_negotiation,
@@ -1686,7 +1686,7 @@ struct Session::Impl final : public MemoryRetainer {
on_acknowledge_datagram,
on_lost_datagram,
nullptr, // get_path_challenge_data (deprecated, use v2 below)
on_stream_stop_sending,
nullptr, // stream_stop_sending
ngtcp2_crypto_version_negotiation_cb,
on_receive_rx_key,
on_receive_tx_key,
@@ -1697,12 +1697,12 @@ struct Session::Impl final : public MemoryRetainer {
on_cid_status,
ngtcp2_crypto_get_path_challenge_data2_cb,
#ifdef NGTCP2_CALLBACKS_V4
nullptr,
on_receive_stream_stop_sending,
#endif
};
static constexpr ngtcp2_callbacks SERVER = {
nullptr,
nullptr, // stream_stop_sending
ngtcp2_crypto_recv_client_initial_cb,
ngtcp2_crypto_recv_crypto_data_cb,
on_handshake_completed,
@@ -1737,7 +1737,7 @@ struct Session::Impl final : public MemoryRetainer {
on_acknowledge_datagram,
on_lost_datagram,
nullptr, // get_path_challenge_data (deprecated, use v2 below)
on_stream_stop_sending,
nullptr, // stream_stop_sending
ngtcp2_crypto_version_negotiation_cb,
nullptr,
on_receive_tx_key,
@@ -1748,7 +1748,7 @@ struct Session::Impl final : public MemoryRetainer {
on_cid_status,
ngtcp2_crypto_get_path_challenge_data2_cb,
#ifdef NGTCP2_CALLBACKS_V4
nullptr,
on_receive_stream_stop_sending,
#endif
};
};
+16 -11
View File
@@ -61,6 +61,8 @@ namespace quic {
V(WANTS_HEADERS, wants_headers, uint8_t) \
/* Set when the stream has a reset event handler */ \
V(WANTS_RESET, wants_reset, uint8_t) \
/* Set when the stream has a stop sending event handler */ \
V(WANTS_STOP_SENDING, wants_stop_sending, uint8_t) \
/* Set when the stream has a trailers event handler */ \
V(WANTS_TRAILERS, wants_trailers, uint8_t) \
/* True when 0-RTT early data was received */ \
@@ -1774,19 +1776,11 @@ void Stream::ReceiveData(const uint8_t* data,
}
void Stream::ReceiveStopSending(QuicError error) {
// STOP_SENDING from the peer asks us to stop sending. Per RFC 9000
// §3.5 the receiver SHOULD respond with RESET_STREAM, which is what
// ngtcp2_conn_shutdown_stream_write below schedules. If our
// writable side has already been shut down (e.g. we already sent
// RESET_STREAM ourselves or finished sending with FIN) there is
// nothing more to do here. The previous guard checked
// `state()->read_ended` which is unrelated to the writable side and
// suppressed STOP_SENDING handling whenever a sibling RESET_STREAM
// frame had been processed first within the same packet.
if (state()->write_ended) return;
// STOP_SENDING from the peer asks us to stop sending. The required
// RESET_STREAM response is scheduled automatically.
Debug(this, "Received stop sending with error %s", error);
ngtcp2_conn_shutdown_stream_write(session(), 0, id(), error.code());
EndWritable();
EmitStopSending(error);
}
void Stream::ReceiveStreamReset(uint64_t final_size, QuicError error) {
@@ -1958,6 +1952,17 @@ void Stream::EmitReset(const QuicError& error) {
MakeCallback(BindingData::Get(env()).stream_reset_callback(), 1, &err);
}
void Stream::EmitStopSending(const QuicError& error) {
if (!env()->can_call_into_js() || !state()->wants_stop_sending) {
return;
}
CallbackScope<Stream> cb_scope(this);
Local<Value> err;
if (!error.ToV8Value(env()).ToLocal(&err)) return;
MakeCallback(BindingData::Get(env()).stream_stop_sending_callback(), 1, &err);
}
void Stream::EmitWantTrailers() {
// state()->wants_trailers will be set from the javascript side if the
// stream object has a handler for the trailers event.
+3
View File
@@ -417,6 +417,9 @@ class Stream final : public AsyncWrap,
// Notifies the JavaScript side that the stream has been reset.
void EmitReset(const QuicError& error);
// Notifies the JavaScript side that the peer asked it to stop sending.
void EmitStopSending(const QuicError& error);
// Notifies the JavaScript side that the application is ready to receive
// trailing headers. Any trailing headers must be sent immediately, and
// synchronously when this callback is triggered.
@@ -156,6 +156,7 @@ assert.strictEqual(streamState.reset, false);
assert.strictEqual(streamState.hasReader, false);
assert.strictEqual(streamState.wantsBlock, false);
assert.strictEqual(streamState.wantsReset, false);
assert.strictEqual(streamState.wantsStopSending, false);
assert.strictEqual(sessionState.hasPathValidationListener, false);
assert.strictEqual(sessionState.hasDatagramListener, false);
@@ -31,6 +31,7 @@ const callbacks = {
onStreamClose() {},
onStreamDrain() {},
onStreamReset() {},
onStreamStopSending() {},
onStreamHeaders() {},
onStreamTrailers() {},
};
@@ -6,20 +6,7 @@
// from the peer's perspective and they would keep transmitting until
// the session-level idle timer fired.
//
// Verified via the server-side writer's `canWrite` getter, which
// returns `null` once `state.writeEnded` is set. STOP_SENDING from
// the client triggers `Stream::ReceiveStopSending -> EndWritable` on
// the server, which sets `state.writeEnded = 1`.
//
// ngtcp2 packs RESET_STREAM before STOP_SENDING in the same packet
// regardless of the order our JS code makes the calls. The server
// processes RESET_STREAM first (firing `onreset`) and then
// STOP_SENDING. The observation must therefore be deferred until
// after the `onreset` callback returns so ngtcp2 can finish
// processing the packet. Capturing `writer.canWrite` synchronously
// inside `onreset` would always see the pre-STOP_SENDING value.
// Throwing inside `onreset` would also crash the process before
// STOP_SENDING could be processed.
// Verified via the server's onstopsending callback.
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import assert from 'node:assert';
@@ -44,17 +31,9 @@ const serverEndpoint = await listen(mustCall((serverSession) => {
`got ${writer.canWrite}`));
return;
}
stream.onreset = mustCall(() => {
// Defer the writeEnded observation: ngtcp2 fires this callback
// synchronously while processing the RESET_STREAM frame, before
// it gets to the STOP_SENDING frame in the same packet. By the
// next event loop tick the rest of the packet has been
// processed and `Stream::ReceiveStopSending -> EndWritable` has
// flipped `state.writeEnded` to 1, which makes the writer's
// canWrite getter return null.
setImmediate(() => {
serverObservation.resolve(writer.canWrite);
});
stream.onreset = mustCall();
stream.onstopsending = mustCall(() => {
serverObservation.resolve(writer.canWrite);
});
// The peer's reset causes stream.closed to reject.
@@ -0,0 +1,58 @@
// Flags: --experimental-quic --no-warnings
// A STOP_SENDING callback is emitted only on the endpoint that receives
// the frame. It is distinct from the RESET_STREAM callback.
import {
hasQuic,
mustCall,
mustNotCall,
skip,
} from '../common/index.mjs';
import assert from 'node:assert';
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('../common/quic.mjs');
const stopCode = 99n;
const callbackReceived = Promise.withResolvers();
const resetReceived = Promise.withResolvers();
const serverEndpoint = await listen(mustCall((serverSession) => {
serverSession.onstream = mustCall(async (stream) => {
// Sending STOP_SENDING locally must not echo an onstopsending callback
// back to this stream.
stream.onstopsending = mustNotCall();
const closed = assert.rejects(stream.closed, {
code: 'ERR_QUIC_APPLICATION_ERROR',
});
stream.onreset = mustCall((error) => {
assert.strictEqual(error.errorCode, stopCode);
resetReceived.resolve();
});
stream.stopSending(stopCode);
await closed;
});
}));
const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;
const stream = await clientSession.createBidirectionalStream();
stream.onreset = mustNotCall();
stream.onstopsending = mustCall((error) => {
assert.strictEqual(error.errorCode, stopCode);
assert.strictEqual(stream.writer.canWrite, null);
assert.strictEqual(stream.writer.writeSync(new Uint8Array([2])), false);
callbackReceived.resolve();
});
stream.writer.writeSync(new Uint8Array([1]));
await Promise.all([callbackReceived.promise, resetReceived.promise]);
clientSession.destroy();
await clientSession.closed;
await serverEndpoint.close();
@@ -1,52 +0,0 @@
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
// Test: stopSending.
// Server calls stopSending(99n) on an incoming stream from the client.
// The server's stream.closed rejects with error code 99 (the stop
// sending code). The client's stream.closed resolves normally because
// the server's write side completed (endSync sent FIN).
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import * as assert from 'node:assert';
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('../common/quic.mjs');
const encoder = new TextEncoder();
const serverDone = Promise.withResolvers();
const serverEndpoint = await listen(mustCall((serverSession) => {
serverSession.onstream = mustCall(async (stream) => {
// Tell the client to stop sending with code 99.
stream.stopSending(99n);
stream.writer.endSync();
// The server's stream.closed rejects with the stop-sending code
// because the inbound side was reset by the peer in response.
await assert.rejects(stream.closed, (error) => {
assert.strictEqual(error.code, 'ERR_QUIC_APPLICATION_ERROR');
assert.ok(error.message.includes('99'));
return true;
});
serverSession.close();
serverDone.resolve();
});
}));
const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;
const stream = await clientSession.createBidirectionalStream({
body: encoder.encode('stop me'),
});
// The client's stream.closed resolves because the server sent FIN
// on its write side (endSync) and the read side completed normally.
await Promise.all([serverDone.promise, stream.closed]);
await clientSession.close();
await serverEndpoint.close();
@@ -1,57 +0,0 @@
// Flags: --experimental-quic --no-warnings
// Test: peer STOP_SENDING transitions writer to errored state.
// After the server calls stopSending(), the client's writer should
// become errored — canWrite is null, writeSync returns false.
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import * as assert from 'node:assert';
import { setTimeout } from 'node:timers/promises';
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('../common/quic.mjs');
const encoder = new TextEncoder();
const serverReady = Promise.withResolvers();
const serverDone = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(async (serverSession) => {
serverSession.onstream = mustCall(async (stream) => {
// Tell the client to stop sending.
stream.stopSending(1n);
serverReady.resolve();
stream.writer.endSync();
await assert.rejects(stream.closed, mustCall((err) => {
assert.ok(err);
return true;
}));
serverSession.close();
serverDone.resolve();
});
}));
const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;
const stream = await clientSession.createBidirectionalStream();
const w = stream.writer;
w.writeSync(encoder.encode('initial data'));
// Wait for the server to send STOP_SENDING.
await serverReady.promise;
// Give a moment for the STOP_SENDING to propagate.
await setTimeout(100);
// After STOP_SENDING, the writer should be in an errored state.
// writeSync returns false (refuses to accept data).
assert.strictEqual(w.writeSync(encoder.encode('rejected')), false);
// The stream closes after the server sends FIN.
await Promise.all([serverDone.promise, stream.closed]);
await clientSession.close();
await serverEndpoint.close();
+1
View File
@@ -22,6 +22,7 @@ interface QuicCallbacks {
onStreamBlocked: () => void;
onStreamClose: (error: [number,bigint,string]) => void;
onStreamReset: (error: [number,bigint,string]) => void;
onStreamStopSending: (error: [number,bigint,string]) => void;
onStreamHeaders: (headers: string[], kind: number) => void;
onStreamTrailers: () => void;
}