Commit 237d19297d5 for nodejs

commit 237d19297d5c536fa0be3845b40694d4acf429d0
Author: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Date:   Mon Nov 23 05:47:15 2026 -0300

    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 <christian.aurichzm@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67617
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Tim Perry <pimterry@gmail.com>

diff --git a/src/quic/endpoint.cc b/src/quic/endpoint.cc
index 13ca955fa2a..04c9b9d3b49 100644
--- a/src/quic/endpoint.cc
+++ b/src/quic/endpoint.cc
@@ -1268,11 +1268,18 @@ BaseObjectPtr<Session> 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 00000000000..aab291bb72b
--- /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',
+  });
+}