Skip to content

Commit 4e502a0

Browse files
committed
quic: add stream idle timeout
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6
1 parent 44c665d commit 4e502a0

18 files changed

Lines changed: 516 additions & 34 deletions

doc/api/quic.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,11 @@ added: v23.8.0
16791679

16801680
* Type: {bigint}
16811681

1682+
### `sessionStats.streamsIdleTimedOut`
1683+
1684+
* Type: {bigint} The total number of peer-initiated streams destroyed by the
1685+
stream idle timeout. Read only.
1686+
16821687
## Class: `QuicError`
16831688

16841689
<!-- YAML
@@ -3050,6 +3055,23 @@ reported as lost via the `ondatagramstatus` callback.
30503055

30513056
This option is immutable after session creation.
30523057

3058+
#### `sessionOptions.streamIdleTimeout`
3059+
3060+
* Type: {bigint|number}
3061+
* **Default:** `30000` (30 seconds)
3062+
3063+
The maximum time in milliseconds that a peer-initiated stream can be idle
3064+
(no data received) before it is automatically destroyed. This protects
3065+
against slowloris-style attacks where a remote peer opens streams but never
3066+
sends data, holding server resources indefinitely. Only peer-initiated
3067+
streams are checked — locally-initiated streams are the application's
3068+
responsibility. Set to `0` to disable.
3069+
3070+
The idle check runs as part of the normal send processing loop, so it adds
3071+
no additional timers or event loop overhead. The
3072+
`session.stats.streamsIdleTimedOut` counter tracks how many streams have been
3073+
destroyed by this mechanism.
3074+
30533075
#### `sessionOptions.maxDatagramSendAttempts`
30543076

30553077
* Type: {number}

lib/internal/quic/quic.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ const endpointRegistry = new SafeSet();
436436
* @property {number} [drainingPeriodMultiplier] Multiplier applied to the
437437
* draining period (3 * PTO) used by ngtcp2. Range `3..255`.
438438
* **Default:** `3`.
439+
* @property {bigint|number} [streamIdleTimeout] Time in ms before idle peer-initiated streams are destroyed
439440
* @property {number} [maxDatagramSendAttempts] Maximum number of times a
440441
* datagram is retried before being abandoned. Range `1..255`.
441442
* **Default:** `5`.
@@ -922,6 +923,17 @@ setCallbacks({
922923
// from QuicError::ToV8Value. Convert to a proper Node.js Error.
923924
if (error !== undefined) {
924925
error = convertQuicError(error);
926+
} else if (this[kOwner] && !this[kOwner].destroyed) {
927+
// The stream is closing cleanly, but it may have been reset by the
928+
// peer (ReceiveStreamReset) or locally (resetStream). The C++ side
929+
// records the reset code in state.resetCode. If set, surface the
930+
// reset as the close error so stream.closed rejects -- the reset
931+
// was an abnormal termination even if the session closed cleanly.
932+
const resetCode = getQuicStreamState(this[kOwner]).resetCode;
933+
if (resetCode !== undefined && resetCode > 0n) {
934+
error = new ERR_QUIC_APPLICATION_ERROR(
935+
resetCode, `stream reset with code ${resetCode}`);
936+
}
925937
}
926938
debug(`stream ${this[kOwner].id} closed callback with error: ${error}`);
927939
this[kOwner][kFinishClose](error);
@@ -5015,6 +5027,7 @@ function processSessionOptions(options, config = kEmptyObject) {
50155027
datagramDropPolicy = 'drop-oldest',
50165028
drainingPeriodMultiplier = 3,
50175029
maxDatagramSendAttempts = 5,
5030+
streamIdleTimeout,
50185031
verifyPeer = 'auto',
50195032
// HTTP/3 application-specific options. Nested under `application`
50205033
// to separate protocol-specific settings from transport-level ones.
@@ -5136,6 +5149,7 @@ function processSessionOptions(options, config = kEmptyObject) {
51365149
datagramDropPolicy,
51375150
drainingPeriodMultiplier,
51385151
maxDatagramSendAttempts,
5152+
streamIdleTimeout,
51395153
application,
51405154
onerror,
51415155
onstream,

lib/internal/quic/stats.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const {
101101
IDX_STATS_SESSION_DATAGRAMS_SENT,
102102
IDX_STATS_SESSION_DATAGRAMS_ACKNOWLEDGED,
103103
IDX_STATS_SESSION_DATAGRAMS_LOST,
104+
IDX_STATS_SESSION_STREAMS_IDLE_TIMED_OUT,
104105
IDX_STATS_SESSION_COUNT,
105106

106107
IDX_STATS_STREAM_CREATED_AT,
@@ -169,6 +170,7 @@ assert(IDX_STATS_SESSION_DATAGRAMS_RECEIVED !== undefined);
169170
assert(IDX_STATS_SESSION_DATAGRAMS_SENT !== undefined);
170171
assert(IDX_STATS_SESSION_DATAGRAMS_ACKNOWLEDGED !== undefined);
171172
assert(IDX_STATS_SESSION_DATAGRAMS_LOST !== undefined);
173+
assert(IDX_STATS_SESSION_STREAMS_IDLE_TIMED_OUT !== undefined);
172174
assert(IDX_STATS_STREAM_CREATED_AT !== undefined);
173175
assert(IDX_STATS_STREAM_OPENED_AT !== undefined);
174176
assert(IDX_STATS_STREAM_RECEIVED_AT !== undefined);
@@ -689,6 +691,13 @@ class QuicSessionStats {
689691
return this.#handle[this.#offset + IDX_STATS_SESSION_DATAGRAMS_LOST];
690692
}
691693

694+
/** @type {bigint} */
695+
get streamsIdleTimedOut() {
696+
assertIsQuicSessionStats(this);
697+
return this.#handle[this.#offset +
698+
IDX_STATS_SESSION_STREAMS_IDLE_TIMED_OUT];
699+
}
700+
692701
toString() {
693702
return JSONStringify(this.toJSON());
694703
}
@@ -726,6 +735,7 @@ class QuicSessionStats {
726735
datagramsSent,
727736
datagramsAcknowledged,
728737
datagramsLost,
738+
streamsIdleTimedOut,
729739
} = this;
730740
return {
731741
__proto__: null,
@@ -762,6 +772,7 @@ class QuicSessionStats {
762772
datagramsSent: `${datagramsSent}`,
763773
datagramsAcknowledged: `${datagramsAcknowledged}`,
764774
datagramsLost: `${datagramsLost}`,
775+
streamsIdleTimedOut: `${streamsIdleTimedOut}`,
765776
};
766777
}
767778

@@ -807,6 +818,7 @@ class QuicSessionStats {
807818
datagramsSent,
808819
datagramsAcknowledged,
809820
datagramsLost,
821+
streamsIdleTimedOut,
810822
} = this;
811823

812824
return `QuicSessionStats ${inspect({
@@ -841,6 +853,7 @@ class QuicSessionStats {
841853
datagramsSent,
842854
datagramsAcknowledged,
843855
datagramsLost,
856+
streamsIdleTimedOut,
844857
}, opts)}`;
845858
}
846859

src/quic/application.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,11 @@ class DefaultApplication final : public Session::Application {
724724

725725
void EarlyDataRejected() override {
726726
// Destroy all open streams — ngtcp2 has already discarded their
727-
// internal state when it rejected the early data.
728-
session().DestroyAllStreams(QuicError::ForApplication(0));
727+
// internal state when it rejected the early data. Use the
728+
// application's internal error code since this is an error
729+
// condition (code 0 would be treated as a clean close).
730+
session().DestroyAllStreams(
731+
QuicError::ForApplication(GetInternalErrorCode()));
729732
if (!session().is_destroyed()) {
730733
session().EmitEarlyDataRejected();
731734
}

src/quic/bindingdata.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class SessionManager;
113113
V(max_connections_total, "maxConnectionsTotal") \
114114
V(max_datagram_frame_size, "maxDatagramFrameSize") \
115115
V(max_datagram_send_attempts, "maxDatagramSendAttempts") \
116+
V(stream_idle_timeout, "streamIdleTimeout") \
116117
V(max_field_section_size, "maxFieldSectionSize") \
117118
V(max_header_length, "maxHeaderLength") \
118119
V(max_header_pairs, "maxHeaderPairs") \

src/quic/data.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ std::optional<int> QuicError::get_crypto_error() const {
365365

366366
MaybeLocal<Value> QuicError::ToV8Value(Environment* env) const {
367367
if ((type() == Type::TRANSPORT && code() == NGTCP2_NO_ERROR) ||
368-
(type() == Type::APPLICATION && code() == NGHTTP3_H3_NO_ERROR) ||
368+
(type() == Type::APPLICATION &&
369+
(code() == 0 || code() == NGHTTP3_H3_NO_ERROR)) ||
369370
type() == Type::IDLE_CLOSE) {
370-
// Note that we only return undefined for *known* no-error application
371-
// codes. It is possible that other application types use other specific
372-
// no-error codes, but since we don't know which application is being used,
373-
// we'll just return the error code value for those below.
371+
// Application code 0 is the default no-error code for raw QUIC
372+
// applications (DefaultApplication::GetNoErrorCode() returns 0).
373+
// NGHTTP3_H3_NO_ERROR (0x100) is the HTTP/3 no-error code.
374374
// Idle close is always clean — the session timed out normally.
375375
return Undefined(env->isolate());
376376
}

src/quic/http3.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,13 @@ class Http3ApplicationImpl final : public Session::Application {
177177
// When 0-RTT is rejected, destroy the nghttp3 connection and all
178178
// open streams — ngtcp2 has discarded their internal state.
179179
// Reset started_ so Start() is called again via on_receive_rx_key
180-
// at 1RTT to recreate the nghttp3 connection.
180+
// at 1RTT to recreate the nghttp3 connection. Use the
181+
// application's internal error code since this is an error
182+
// condition (code 0 would be treated as a clean close).
181183
conn_.reset();
182184
started_ = false;
183-
session().DestroyAllStreams(QuicError::ForApplication(0));
185+
session().DestroyAllStreams(
186+
QuicError::ForApplication(GetInternalErrorCode()));
184187
if (!session().is_destroyed()) {
185188
session().EmitEarlyDataRejected();
186189
}

src/quic/session.cc

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ uint64_t MaxDatagramPayload(uint64_t max_frame_size) {
174174
V(DATAGRAMS_RECEIVED, datagrams_received) \
175175
V(DATAGRAMS_SENT, datagrams_sent) \
176176
V(DATAGRAMS_ACKNOWLEDGED, datagrams_acknowledged) \
177-
V(DATAGRAMS_LOST, datagrams_lost)
177+
V(DATAGRAMS_LOST, datagrams_lost) \
178+
V(STREAMS_IDLE_TIMED_OUT, streams_idle_timed_out)
178179

179180
#define NO_SIDE_EFFECT true
180181
#define SIDE_EFFECT false
@@ -617,7 +618,8 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
617618
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
618619
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
619620
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
620-
!SET(max_datagram_send_attempts)) {
621+
!SET(max_datagram_send_attempts) ||
622+
!SET(stream_idle_timeout)) {
621623
return Nothing<Options>();
622624
}
623625

@@ -2811,24 +2813,36 @@ void Session::ShutdownStream(stream_id id, QuicError error) {
28112813
DCHECK(!is_destroyed());
28122814
Debug(this, "Shutting down stream %" PRIi64 " with error %s", id, error);
28132815
SendPendingDataScope send_scope(this);
2814-
ngtcp2_conn_shutdown_stream(*this,
2815-
0,
2816-
id,
2817-
error.type() == QuicError::Type::APPLICATION
2818-
? error.code()
2819-
: application().GetNoErrorCode());
2816+
// STOP_SENDING and RESET_STREAM frames carry application-level error
2817+
// codes (RFC 9000 §19.4, §19.5). Map the QuicError to an appropriate
2818+
// application code: APPLICATION errors pass through directly; transport
2819+
// no-error maps to the application's no-error code; any other error
2820+
// maps to the application's internal error code.
2821+
error_code code;
2822+
if (error.type() == QuicError::Type::APPLICATION) {
2823+
code = error.code();
2824+
} else if (error.code() == NGTCP2_NO_ERROR) {
2825+
code = application().GetNoErrorCode();
2826+
} else {
2827+
code = application().GetInternalErrorCode();
2828+
}
2829+
ngtcp2_conn_shutdown_stream(*this, 0, id, code);
28202830
}
28212831

2822-
void Session::ShutdownStreamWrite(stream_id id, QuicError code) {
2832+
void Session::ShutdownStreamWrite(stream_id id, QuicError error) {
28232833
DCHECK(!is_destroyed());
2824-
Debug(this, "Shutting down stream %" PRIi64 " write with error %s", id, code);
2834+
Debug(this, "Shutting down stream %" PRIi64 " write with error %s",
2835+
id, error);
28252836
SendPendingDataScope send_scope(this);
2826-
ngtcp2_conn_shutdown_stream_write(*this,
2827-
0,
2828-
id,
2829-
code.type() == QuicError::Type::APPLICATION
2830-
? code.code()
2831-
: application().GetNoErrorCode());
2837+
error_code code;
2838+
if (error.type() == QuicError::Type::APPLICATION) {
2839+
code = error.code();
2840+
} else if (error.code() == NGTCP2_NO_ERROR) {
2841+
code = application().GetNoErrorCode();
2842+
} else {
2843+
code = application().GetInternalErrorCode();
2844+
}
2845+
ngtcp2_conn_shutdown_stream_write(*this, 0, id, code);
28322846
}
28332847

28342848
void Session::StreamDataBlocked(stream_id id) {
@@ -3027,6 +3041,41 @@ void Session::UpdateDataStats() {
30273041
std::max(STAT_GET(Stats, max_bytes_in_flight), info.bytes_in_flight));
30283042
}
30293043

3044+
void Session::CheckStreamIdleTimeout(uint64_t now) {
3045+
if (is_destroyed()) return;
3046+
uint64_t timeout = options().stream_idle_timeout;
3047+
if (timeout == 0) return;
3048+
3049+
uint64_t timeout_ns = timeout * NGTCP2_MILLISECONDS;
3050+
auto all_streams = streams();
3051+
3052+
for (const auto& [id, stream] : all_streams) {
3053+
if (!stream) continue;
3054+
3055+
// Only check peer-initiated streams. Locally-initiated streams
3056+
// that haven't been written to are the application's concern.
3057+
if (ngtcp2_conn_is_local_stream(*this, id)) continue;
3058+
3059+
uint64_t last_activity = stream->last_activity_timestamp();
3060+
if (last_activity > 0 && (now - last_activity) > timeout_ns) {
3061+
Debug(this,
3062+
"Stream %" PRId64 " idle timeout exceeded, destroying",
3063+
id);
3064+
// Notify the peer before destroying. ShutdownStream sends both
3065+
// STOP_SENDING and RESET_STREAM as appropriate, using the
3066+
// application's no-error code for non-APPLICATION errors (since
3067+
// these frames carry application-level error codes per RFC 9000).
3068+
// Without this, the peer's stream sits orphaned until the
3069+
// session closes.
3070+
auto error = QuicError::ForTransport(NGTCP2_ERR_PROTO,
3071+
"stream idle timeout");
3072+
ShutdownStream(id, error);
3073+
stream->Destroy(error);
3074+
STAT_INCREMENT(Stats, streams_idle_timed_out);
3075+
}
3076+
}
3077+
}
3078+
30303079
void Session::SendConnectionClose() {
30313080
// Method is a non-op if the session is already destroyed or the
30323081
// endpoint cannot send. Note: we intentionally do NOT check
@@ -3111,6 +3160,8 @@ void Session::OnTimeout() {
31113160
if (is_destroyed()) return;
31123161
if (NGTCP2_OK(ret) && !is_in_closing_period() && !is_in_draining_period()) {
31133162
application().SendPendingData();
3163+
if (is_destroyed()) return;
3164+
CheckStreamIdleTimeout(uv_hrtime());
31143165
return;
31153166
}
31163167
if (is_destroyed()) return;
@@ -3157,6 +3208,15 @@ void Session::UpdateTimer() {
31573208
auto timeout = (expiry - now) / NGTCP2_MILLISECONDS;
31583209
Debug(this, "Updating timeout to %zu milliseconds", timeout);
31593210

3211+
// If a stream idle timeout is configured, ensure the timer fires at
3212+
// least that often so CheckStreamIdleTimeout runs. Without this, an
3213+
// idle session with idle streams might not fire the timer until the
3214+
// connection idle timeout, which could be much longer.
3215+
uint64_t stream_idle = options().stream_idle_timeout;
3216+
if (stream_idle > 0 && timeout > stream_idle) {
3217+
timeout = stream_idle;
3218+
}
3219+
31603220
// If timeout is zero here, it means our timer is less than a millisecond
31613221
// off from expiry. Let's bump the timer to 1.
31623222
impl_->timer_.Update(timeout == 0 ? 1 : timeout);

src/quic/session.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
227227
// 10.2 requires at least 3x PTO. Range: 3-255. Default: 3.
228228
uint8_t draining_period_multiplier = 3;
229229

230+
// The amount of time (in milliseconds) that a stream can be idle
231+
// (no data received) before it is automatically destroyed. This
232+
// protects against slowloris-style attacks where a peer opens streams
233+
// but never sends data, holding server resources indefinitely.
234+
// Only applies to peer-initiated streams. Set to 0 to disable.
235+
static constexpr uint64_t DEFAULT_STREAM_IDLE_TIMEOUT = 30'000;
236+
uint64_t stream_idle_timeout = DEFAULT_STREAM_IDLE_TIMEOUT;
237+
230238
// An optional NEW_TOKEN from a previous connection to the same
231239
// server. When set, the token is included in the Initial packet
232240
// to skip address validation. Client-side only.
@@ -569,6 +577,7 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
569577
// Has to be called after certain operations that generate packets.
570578
void UpdatePacketTxTime();
571579
void UpdateDataStats();
580+
void CheckStreamIdleTimeout(uint64_t now);
572581
void UpdatePath(const PathStorage& path);
573582

574583
void ProcessPendingBidiStreams();

src/quic/streams.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,8 @@ void Stream::NotifyStreamOpened(stream_id id) {
12701270
// Headers were enqueued while the application was not yet known
12711271
// (headers_supported == 0), and the negotiated application does
12721272
// not support headers. This is a fatal mismatch.
1273-
Destroy(QuicError::ForApplication(0));
1273+
Destroy(QuicError::ForApplication(
1274+
session().application().GetInternalErrorCode()));
12741275
return;
12751276
}
12761277
decltype(pending_headers_queue_) queue;
@@ -1347,6 +1348,11 @@ Session& Stream::session() const {
13471348
return *session_;
13481349
}
13491350

1351+
uint64_t Stream::last_activity_timestamp() const {
1352+
uint64_t ts = stats()->received_at;
1353+
return ts != 0 ? ts : stats()->created_at;
1354+
}
1355+
13501356
bool Stream::is_local_unidirectional() const {
13511357
return direction() == Direction::UNIDIRECTIONAL &&
13521358
ngtcp2_conn_is_local_stream(*session_, id());
@@ -1625,6 +1631,7 @@ void Stream::EndReadable(std::optional<uint64_t> maybe_final_size) {
16251631

16261632
void Stream::Destroy(QuicError error) {
16271633
if (stats()->destroyed_at != 0) return;
1634+
16281635
// Record the destroyed at timestamp before notifying the JavaScript side
16291636
// that the stream is being destroyed.
16301637
STAT_RECORD_TIMESTAMP(Stats, destroyed_at);

0 commit comments

Comments
 (0)