Skip to content

Commit e4aacfd

Browse files
efekrskladuh95
authored andcommitted
quic: handle application-specific no-error codes
Signed-off-by: Efe Karasakal <hi@efe.dev> PR-URL: #67094 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent a4928b2 commit e4aacfd

15 files changed

Lines changed: 326 additions & 127 deletions

‎doc/api/quic.md‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,10 +1959,10 @@ added: v23.8.0
19591959
* Type: {Promise}
19601960

19611961
A promise that is fulfilled when the stream is fully closed. It resolves
1962-
when the stream closes cleanly (including idle timeout). It rejects with
1962+
when the stream closes without a QUIC error, including idle timeout and a
1963+
`RESET_STREAM` whose application code represents no error. It rejects with
19631964
an `ERR_QUIC_APPLICATION_ERROR` or `ERR_QUIC_TRANSPORT_ERROR` when the
1964-
stream is closed due to a QUIC error (e.g., stream reset by the peer,
1965-
CONNECTION\_CLOSE with a non-zero error code).
1965+
stream is closed due to a QUIC error.
19661966

19671967
### `stream.destroy([error[, options]])`
19681968

@@ -3496,10 +3496,11 @@ selects how the stream's async iterator reports this:
34963496

34973497
* `'ignore'` - The truncation itself is ignored: only a stream or connection
34983498
error is reported, and any clean abort/cancellation or similar simply ends
3499-
the stream. A non-zero peer reset, non-zero local stop-sending or connection
3500-
error still fails, but a truncation with no error at all (an idle timeout,
3501-
a graceful close, or a plain `stopSending()`) ends the read cleanly with the
3502-
data received. This matches `stream.closed`, which rejects only on an error.
3499+
the stream. A peer reset or local stop-sending with an application code that
3500+
represents an error, or a connection error, still fails, but a truncation
3501+
with no error at all (an idle timeout, a graceful close, or a plain
3502+
`stopSending()`) ends the read cleanly with the data received. This matches
3503+
`stream.closed`, which rejects only on an error.
35033504

35043505
#### `sessionOptions.verifyPeer` (client only)
35053506

@@ -4096,7 +4097,7 @@ added: v23.8.0
40964097
-->
40974098

40984099
* `this` {quic.QuicStream}
4099-
* `error` {any}
4100+
* `error` {Error}
41004101

41014102
### Callback: `OnHeadersCallback`
41024103

@@ -4790,7 +4791,7 @@ added:
47904791
47914792
* `stream` {quic.QuicStream}
47924793
* `session` {quic.QuicSession}
4793-
* `error` {any} The QUIC error associated with the reset.
4794+
* `error` {Error} The QUIC error associated with the reset.
47944795
47954796
Published when a stream receives a RESET\_STREAM frame from the peer,
47964797
indicating the peer has aborted its sending direction. This is a key signal

‎lib/internal/quic/quic.js‎

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ const endpointRegistry = new SafeSet();
694694
/**
695695
* @callback OnStreamErrorCallback
696696
* @this {QuicStream}
697-
* @param {any} error
697+
* @param {Error} error
698698
* @returns {void}
699699
*/
700700

@@ -762,16 +762,16 @@ setCallbacks({
762762
/**
763763
* Called when the underlying session C++ handle is closed either normally
764764
* or with an error.
765-
* @param {number} errorType
766-
* @param {bigint} code
767-
* @param {string} [reason]
768-
* @param {string} [errorName] Symbolic name for a recognized transport
769-
* error code, including a decoded TLS alert for CRYPTO_ERROR; otherwise
770-
* undefined.
765+
* @param {Array} [error] Raw `[type, code, reason, errorName]` data.
766+
* `errorName` is the symbolic name for a recognized transport error,
767+
* including a decoded TLS alert for CRYPTO_ERROR; otherwise undefined.
771768
*/
772-
onSessionClose(errorType, code, reason, errorName) {
773-
debug('session close callback', errorType, code, reason, errorName);
774-
this[kOwner][kFinishClose](errorType, code, reason, errorName);
769+
onSessionClose(error) {
770+
if (error !== undefined) {
771+
error = convertQuicError(error);
772+
}
773+
debug('session close callback', error);
774+
this[kOwner][kFinishClose](error);
775775
},
776776

777777
/**
@@ -963,20 +963,16 @@ setCallbacks({
963963
},
964964

965965
onStreamClose(error) {
966-
// Called when the stream C++ handle has been closed. The error is
967-
// either undefined (clean close) or a raw array [type, code, reason]
968-
// from QuicError::ToV8Value. Convert to a proper Node.js Error.
966+
// Called when the stream C++ handle has been closed. The error is either
967+
// undefined (clean close) or a raw [type, code, reason, errorName] array
968+
// from the session-aware native error conversion.
969969
if (!this[kOwner]) return;
970970
if (error !== undefined) {
971971
error = convertQuicError(error);
972972
} else if (!this[kOwner].destroyed) {
973-
// The stream is closing cleanly, but it may have been reset by the
974-
// peer (ReceiveStreamReset) or locally (resetStream). The C++ side
975-
// records the reset code in state.resetCode. If set, surface the
976-
// reset as the close error so stream.closed rejects -- the reset
977-
// was an abnormal termination even if the session closed cleanly.
978-
const resetCode = getQuicStreamState(this[kOwner]).resetCode;
979-
if (resetCode !== undefined && resetCode > 0n) {
973+
const state = getQuicStreamState(this[kOwner]);
974+
if (state.resetCodeIsError) {
975+
const resetCode = state.resetCode;
980976
error = makeQuicError(
981977
'ERR_QUIC_APPLICATION_ERROR',
982978
'QUIC application error',
@@ -1616,6 +1612,7 @@ class QuicStream {
16161612
pendingClose: undefined,
16171613
destroyError: undefined,
16181614
stopSendingCode: undefined,
1615+
stopSendingCodeIsError: undefined,
16191616
truncatedReads: undefined,
16201617
reader: undefined,
16211618
destroying: false,
@@ -1728,15 +1725,15 @@ class QuicStream {
17281725

17291726
// If we cancelled ourselves, check our own stop-sending code (not the
17301727
// result mirrored by the remote peer)
1731-
if (inner.stopSendingCode > 0n) {
1728+
if (inner.stopSendingCodeIsError) {
17321729
throw new QuicError('Stream aborted before FIN was received',
17331730
{ __proto__: null,
17341731
errorCode: inner.stopSendingCode });
17351732
}
17361733

1737-
// Non-zero reset is always an error:
1734+
// A reset carrying an application error always fails the read.
17381735
const peerResetCode = inner.state.resetCode;
1739-
if (peerResetCode > 0n) {
1736+
if (inner.state.resetCodeIsError) {
17401737
throw new QuicError(
17411738
`The QUIC stream was reset by the peer with error code ${peerResetCode}`,
17421739
{ __proto__: null,
@@ -2326,7 +2323,7 @@ class QuicStream {
23262323
if (this.destroyed) return;
23272324
const abortCode = validateQuicErrorCode(code, 'code');
23282325
this.#inner.stopSendingCode = abortCode;
2329-
this.#handle.stopSending(abortCode);
2326+
this.#inner.stopSendingCodeIsError = this.#handle.stopSending(abortCode);
23302327
}
23312328

23322329
/**
@@ -3654,22 +3651,18 @@ class QuicSession {
36543651
}
36553652

36563653
/**
3657-
* @param {number} errorType
3658-
* @param {bigint} code
3659-
* @param {string} [reason]
3660-
* @param {string} [errorName]
3654+
* @param {Error} [error]
36613655
*/
3662-
[kFinishClose](errorType, code, reason, errorName) {
3663-
// If code is zero, then we closed without an error. Yay! We can destroy
3656+
[kFinishClose](error) {
3657+
// If the error is undefined, then we closed without an error. Yay! We can destroy
36643658
// safely without specifying an error.
3665-
if (code === 0n) {
3659+
if (error === undefined) {
36663660
debug('finishing closing the session with no error');
36673661
this.destroy();
36683662
return;
36693663
}
36703664

3671-
debug('finishing closing the session with an error',
3672-
errorType, code, reason, errorName);
3665+
debug('finishing closing the session with an error', error);
36733666

36743667
// If the local side initiated this close with an error code (via
36753668
// close({ code })), this is an intentional shutdown; not an error.
@@ -3679,44 +3672,7 @@ class QuicSession {
36793672
return;
36803673
}
36813674

3682-
// Otherwise, errorType indicates the type of error that occurred, code indicates
3683-
// the specific error, and reason is an optional string describing the error.
3684-
// code !== 0n here (the early return above handles code === 0n).
3685-
// The errorType values map to ngtcp2_ccerr_type:
3686-
// 0 = NGTCP2_CCERR_TYPE_TRANSPORT
3687-
// 1 = NGTCP2_CCERR_TYPE_APPLICATION
3688-
// 2 = NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION
3689-
// 3 = NGTCP2_CCERR_TYPE_IDLE_CLOSE
3690-
// 4 = NGTCP2_CCERR_TYPE_DROP_CONN
3691-
// 5 = NGTCP2_CCERR_TYPE_RETRY
3692-
// The DROP_CONN/RETRY cases are typically intercepted before reaching
3693-
// here (DROP_CONN tears the connection down without notifying us, RETRY
3694-
// is server-only). The default branch is a safety net so any
3695-
// unexpected value still completes the close path - without it the
3696-
// session would leak with `closed` hanging forever.
3697-
switch (errorType) {
3698-
case 0: /* Transport Error */
3699-
this.destroy(makeQuicError('ERR_QUIC_TRANSPORT_ERROR',
3700-
'QUIC transport error',
3701-
'transport', code, reason, errorName));
3702-
break;
3703-
case 1: /* Application Error */
3704-
this.destroy(makeQuicError('ERR_QUIC_APPLICATION_ERROR',
3705-
'QUIC application error',
3706-
'application', code, reason, errorName));
3707-
break;
3708-
case 2: /* Version Negotiation Error */
3709-
this.destroy(new ERR_QUIC_VERSION_NEGOTIATION_ERROR());
3710-
break;
3711-
case 3: /* Idle close */
3712-
this.destroy();
3713-
break;
3714-
default:
3715-
this.destroy(makeQuicError('ERR_QUIC_TRANSPORT_ERROR',
3716-
'QUIC transport error',
3717-
'transport', code, reason, errorName));
3718-
break;
3719-
}
3675+
this.destroy(error);
37203676
}
37213677

37223678
[kKeylog](line) {

‎lib/internal/quic/state.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const {
112112
IDX_STATE_STREAM_WRITE_DESIRED_SIZE,
113113
IDX_STATE_STREAM_BUDGET,
114114
IDX_STATE_STREAM_RESET_CODE,
115+
IDX_STATE_STREAM_RESET_CODE_IS_ERROR,
115116
IDX_STATE_STREAM_SIZE,
116117
} = internalBinding('quic');
117118

@@ -153,6 +154,7 @@ assert(IDX_STATE_STREAM_WANTS_RESET !== undefined);
153154
assert(IDX_STATE_STREAM_WANTS_STOP_SENDING !== undefined);
154155
assert(IDX_STATE_STREAM_WRITE_DESIRED_SIZE !== undefined);
155156
assert(IDX_STATE_STREAM_RESET_CODE !== undefined);
157+
assert(IDX_STATE_STREAM_RESET_CODE_IS_ERROR !== undefined);
156158

157159
const kEmptyObject = { __proto__: null };
158160

@@ -873,6 +875,14 @@ class QuicStreamState {
873875
handle, this.#offset + IDX_STATE_STREAM_RESET_CODE, kIsLittleEndian);
874876
}
875877

878+
/** @type {boolean} */
879+
get resetCodeIsError() {
880+
const handle = this.#handle;
881+
if (handle === undefined) return undefined;
882+
return DataViewPrototypeGetUint8(
883+
handle, this.#offset + IDX_STATE_STREAM_RESET_CODE_IS_ERROR) !== 0;
884+
}
885+
876886
/** @type {bigint} */
877887
get writeDesiredSize() {
878888
const handle = this.#handle;
@@ -924,6 +934,7 @@ class QuicStreamState {
924934
wantsStopSending,
925935
early,
926936
resetCode,
937+
resetCodeIsError,
927938
writeDesiredSize,
928939
budget,
929940
} = this;
@@ -943,6 +954,7 @@ class QuicStreamState {
943954
wantsStopSending,
944955
early,
945956
resetCode: `${resetCode}`,
957+
resetCodeIsError,
946958
writeDesiredSize,
947959
budget,
948960
};
@@ -979,6 +991,7 @@ class QuicStreamState {
979991
wantsStopSending,
980992
early,
981993
resetCode,
994+
resetCodeIsError,
982995
writeDesiredSize,
983996
budget,
984997
} = this;
@@ -998,6 +1011,7 @@ class QuicStreamState {
9981011
wantsStopSending,
9991012
early,
10001013
resetCode,
1014+
resetCodeIsError,
10011015
writeDesiredSize,
10021016
budget,
10031017
}, opts)}`;

‎src/quic/application.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class Session::Application : public MemoryRetainer {
6060
// and is not necessarily 0.
6161
virtual error_code GetNoErrorCode() const = 0;
6262

63+
// Returns whether an application error code means "no error".
64+
// Applications may override this when unknown codes have special semantics.
65+
virtual bool IsNoErrorCode(error_code code) const {
66+
return code == GetNoErrorCode();
67+
}
68+
6369
// The "internal error code" is the application-level error code used
6470
// to signal a non-specific failure when no more specific code has
6571
// been provided by the caller. For example, `writer.fail(reason)` on

‎src/quic/data.cc‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,6 @@ const char* QuicError::name() const {
442442
}
443443

444444
MaybeLocal<Value> QuicError::ToV8Value(Environment* env) const {
445-
if ((type() == Type::TRANSPORT && code() == NGTCP2_NO_ERROR) ||
446-
(type() == Type::APPLICATION &&
447-
(code() == 0 || code() == NGHTTP3_H3_NO_ERROR)) ||
448-
type() == Type::IDLE_CLOSE) {
449-
// Application code 0 is the default no-error code for raw QUIC
450-
// applications (DefaultApplication::GetNoErrorCode() returns 0).
451-
// NGHTTP3_H3_NO_ERROR (0x100) is the HTTP/3 no-error code.
452-
// Idle close is always clean — the session timed out normally.
453-
return Undefined(env->isolate());
454-
}
455-
456445
Local<Value> type_str;
457446
if (!node::ToV8Value(env->context(), TypeName(type())).ToLocal(&type_str)) {
458447
return {};
@@ -467,7 +456,7 @@ MaybeLocal<Value> QuicError::ToV8Value(Environment* env) const {
467456

468457
// Note that per the QUIC specification, the reason, if present, is
469458
// expected to be UTF-8 encoded. The spec uses the term "SHOULD" here,
470-
// which means that is is entirely possible that some QUIC implementation
459+
// which means that is entirely possible that some QUIC implementation
471460
// could choose a different encoding, in which case the conversion here
472461
// will produce garbage. That's ok though, we're going to use the default
473462
// assumption that the impl is following the guidelines.

‎src/quic/data.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,13 @@ class QuicError final : public MemoryRetainer {
285285

286286
std::string ToString() const;
287287

288-
// Returns an array containing [type, code, reason, error_name], where type
289-
// is a string representation of the error type, code is a bigint
290-
// representation of the error code, and reason is a string representation
291-
// of the error reason, or undefined if the reason is empty. error_name is
292-
// the symbolic name of a recognized transport error, or undefined. This is
293-
// expected to be used to construct a JS error object.
288+
// Returns an array containing [type, code, reason, error_name]. type is a
289+
// string representation of the error type, code is a bigint representation
290+
// of the error code, reason is a string or undefined, and error_name is the
291+
// symbolic name of a recognized transport error or undefined. This lossless
292+
// representation is used to construct a JS error object; whether the code
293+
// means "no error" depends on the negotiated application and is determined
294+
// by Session.
294295
v8::MaybeLocal<v8::Value> ToV8Value(Environment* env) const;
295296
inline v8::MaybeLocal<v8::Value> ToV8Value(Realm* realm) const {
296297
return ToV8Value(realm->env());

‎src/quic/http3.cc‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ constexpr size_t kSessionTicketAppDataHeaderSize = 6; // type + version + crc
3737
constexpr size_t kSessionTicketAppDataPayloadSize =
3838
kSessionTicketAppDataSize - kSessionTicketAppDataHeaderSize;
3939

40+
// RFC 9297 defines H3_DATAGRAM_ERROR, but nghttp3 does not expose it.
41+
constexpr error_code kH3DatagramError = 0x33;
42+
4043
inline void WriteBE32(uint8_t* buf, uint32_t val) {
4144
buf[0] = static_cast<uint8_t>((val >> 24) & 0xff);
4245
buf[1] = static_cast<uint8_t>((val >> 16) & 0xff);
@@ -186,6 +189,22 @@ class Http3ApplicationImpl final : public Session::Application {
186189

187190
error_code GetNoErrorCode() const override { return NGHTTP3_H3_NO_ERROR; }
188191

192+
bool IsNoErrorCode(error_code code) const override {
193+
if (code == NGHTTP3_H3_NO_ERROR) return true;
194+
if (code == kH3DatagramError) return false;
195+
196+
switch (code) {
197+
#define V(name) case NGHTTP3_##name:
198+
HTTP3_APPLICATION_ERRORS(V)
199+
#undef V
200+
return false;
201+
default:
202+
// RFC 9114 Section 8 requires unknown HTTP/3 error codes to be
203+
// treated as equivalent to H3_NO_ERROR.
204+
return true;
205+
}
206+
}
207+
189208
// HTTP/3 defines H3_INTERNAL_ERROR (0x102) for non-specific failures
190209
// initiated by the implementation; this is the right code to send
191210
// on RESET_STREAM when a stream is being aborted without an

0 commit comments

Comments
 (0)