From a59007a7ca66061e7c9d77b8f0f54b9d4b6f5164 Mon Sep 17 00:00:00 2001 From: Christian Aurich Zanettini Martins Date: Thu, 19 Nov 2026 08:52:29 -0300 Subject: [PATCH] quic: fix abort on handshake failure in connect() Endpoint::Connect() marked the session as wrapped before sending the first flight, while JavaScript had no QuicSession for it yet. If the session closed during that send, for example with ciphers: '', nothing called destroy() and the CHECK in Session::EmitClose() aborted the process. Send the first flight before marking the session as wrapped, so connect() rejects with ERR_QUIC_CONNECTION_FAILED instead. Signed-off-by: Christian Aurich Zanettini Martins --- src/quic/endpoint.cc | 11 ++++++++-- ...test-quic-connect-first-flight-failure.mjs | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-quic-connect-first-flight-failure.mjs diff --git a/src/quic/endpoint.cc b/src/quic/endpoint.cc index 13ca955fa2a4..04c9b9d3b49f 100644 --- a/src/quic/endpoint.cc +++ b/src/quic/endpoint.cc @@ -1268,11 +1268,18 @@ BaseObjectPtr Endpoint::Connect( return {}; } + AddSession(config.scid, session); + + // Send the first flight before marking the session as wrapped. If the + // session closes during that send (no usable cipher suite or group, an + // expired handshake timeout), FinishClose() has to destroy it directly + // instead of calling into JavaScript, which has no QuicSession for it yet. + { Session::SendPendingDataScope send_scope(session); } + if (session->is_destroyed()) return {}; + // Marking a session as "wrapped" means that the reference has been // (or will be) passed out to JavaScript. - Session::SendPendingDataScope send_scope(session); session->set_wrapped(); - AddSession(config.scid, session); return session; } diff --git a/test/parallel/test-quic-connect-first-flight-failure.mjs b/test/parallel/test-quic-connect-first-flight-failure.mjs new file mode 100644 index 000000000000..aab291bb72bb --- /dev/null +++ b/test/parallel/test-quic-connect-first-flight-failure.mjs @@ -0,0 +1,21 @@ +// Flags: --experimental-quic --no-warnings + +// Test: connect() rejects when the session closes during the first flight. + +import { hasQuic, skip } from '../common/index.mjs'; +import assert from 'node:assert'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { connect } = await import('../common/quic.mjs'); + +// An empty list leaves the ClientHello without a cipher suite or a group, and +// a handshakeTimeout of 0 has already expired once the first flight is out. +const cases = [{ ciphers: '' }, { groups: '' }, { handshakeTimeout: 0 }]; +for (const options of cases) { + await assert.rejects(connect('127.0.0.1:1', options), { + code: 'ERR_QUIC_CONNECTION_FAILED', + }); +}