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', + }); +}