Skip to content
Closed
19 changes: 19 additions & 0 deletions src/js/node/_http2_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,31 @@
tlsSocket.resume();
}

// ---------------------------------------------------------------------------
// Raw socket listeners bound to tlsSocket
// ---------------------------------------------------------------------------
Comment thread
robobun marked this conversation as resolved.
Resolved

// error: the transport under the TLS layer failed (reset, EPIPE, destroy(err)).
// Node's TLSSocket re-emits the wrapped socket's errors on itself, so they
// reach the session's socket error handling and the socket handed in never
// emits an unhandled 'error':
// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/tls/wrap.js#L977
// Stays attached for the raw socket's lifetime (it is still flushing after the
// session is gone), hence the destroyed check.
Comment thread
robobun marked this conversation as resolved.
Resolved
function onRawSocketError(this: TLSProxySocket, err: Error) {
if (!this.destroyed) {
this.destroy(err);
}
}

// ---------------------------------------------------------------------------
// Close-cleanup handler
// ---------------------------------------------------------------------------

// onTlsClose: when the TLS socket closes (e.g. H2 session destroyed), clean up
// the raw socket listeners to prevent memory leaks and stale callback references.
// EventEmitter calls 'close' handlers with `this` = emitter (tlsSocket).
// The 'error' listener is deliberately left in place (see onRawSocketError).
function onTlsClose(this: TLSProxySocket) {
const ctx = this._ctx;
const raw = ctx.rawSocket;
Expand Down Expand Up @@ -386,6 +404,7 @@
rawSocket.on("end", events[1]);
rawSocket.on("drain", events[2]);
rawSocket.on("close", events[3]);
rawSocket.on("error", onRawSocketError.bind(tlsSocket));

Check warning on line 407 in src/js/node/_http2_upgrade.ts

View check run for this annotation

Claude / Claude Code Review

Error listener attached after upgradeDuplexToTLS try/catch, so the throw path still leaves rawSocket 'error' unhandled

nit: this listener is attached after the try/catch, so the pre-existing catch path at L389 (`rawSocket.destroy(e as Error)`) still emits an unhandled `'error'` on rawSocket — the same class this PR fixes. Moving this line above the `try` is enough: `tlsSocket.destroy(e)` in the catch sets `tlsSocket.destroyed` synchronously, so `onRawSocketError` becomes a no-op absorber via its `destroyed` guard. The sibling site at `tls.ts:1491-1496` handles the identical failure with `socket.destroy()` (no ar
Comment thread
claude[bot] marked this conversation as resolved.
Resolved

// When the TLS socket closes (e.g. H2 session destroyed), clean up the raw socket
// listeners to prevent memory leaks and stale callback references.
Expand Down
45 changes: 45 additions & 0 deletions test/js/node/http2/node-http2-upgrade.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,51 @@ describe("HTTP/2 upgrade — socket close ordering", () => {
client.close();
netServer.close();
});

test("an error on rawSocket is the session's error, not an uncaught exception", async () => {
// Nothing but the upgrade listens on rawSocket once it has been handed
// over, so its error (a reset, EPIPE, a destroy(err) by its owner) has to
// be reported the way a TLSSocket reports its wrapped socket's errors: the
// session is destroyed with it and the server sees 'sessionError'.
let rawSocket: net.Socket | undefined;
const sessionReady = Promise.withResolvers<http2.Http2Session>();
const sessionErrored = Promise.withResolvers<{ err: Error; session: http2.Http2Session }>();

const h2Server = http2.createSecureServer(TLS, (_req, res) => {
res.writeHead(200);
res.end("done");
});
h2Server.on("error", () => {});
h2Server.on("session", s => sessionReady.resolve(s));
h2Server.on("sessionError", (err: Error, session: http2.Http2Session) => sessionErrored.resolve({ err, session }));

const netServer = net.createServer(socket => {
rawSocket = socket;
h2Server.emit("connection", socket);
});

const port = await new Promise<number>(resolve => {
netServer.listen(0, "127.0.0.1", () => resolve((netServer.address() as net.AddressInfo).port));
});

const client = connectClient(port);
await request(client, "GET", "/");
const h2Session = await sessionReady.promise;
// Not events.once(): the session emits 'error' on its way to 'close'.
const sessionClosed = new Promise<void>(resolve => h2Session.once("close", resolve));

rawSocket!.destroy(new Error("transport failed"));

await sessionClosed;
const { err, session } = await sessionErrored.promise;
assert.deepStrictEqual(
{ message: err.message, sameSession: session === h2Session, destroyed: h2Session.destroyed },
{ message: "transport failed", sameSession: true, destroyed: true },
);
Comment thread
robobun marked this conversation as resolved.
Resolved

client.close();
netServer.close();
});
});

describe("HTTP/2 upgrade — ALPN negotiation", () => {
Expand Down