From 96dfa8c038ba38f0ea7f4a36410687bf6b831b9c Mon Sep 17 00:00:00 2001 From: Adewole oluwanifemi Date: Sat, 5 Dec 2026 00:06:07 +0100 Subject: [PATCH] net: support abort signal with BoundSocket Signed-off-by: Adewole oluwanifemi --- lib/net.js | 1 + test/parallel/test-net-boundsocket.js | 82 +++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/lib/net.js b/lib/net.js index 6802b66a9801..2b1f020be659 100644 --- a/lib/net.js +++ b/lib/net.js @@ -2707,6 +2707,7 @@ Server.prototype.listen = function(...args) { boundSocket = options.handle; } if (boundSocket !== null) { + addServerAbortSignalOption(this, options); const boundPath = boundSocket[kBoundSocketPath]; this._handle = boundSocket[kBoundSocketConsume](); // A pipe-backed handle reports its path via Server.address(). diff --git a/test/parallel/test-net-boundsocket.js b/test/parallel/test-net-boundsocket.js index 2d11d0d4ba83..0400dc45a5ec 100644 --- a/test/parallel/test-net-boundsocket.js +++ b/test/parallel/test-net-boundsocket.js @@ -450,3 +450,85 @@ if (common.hasIPv6) { bound.close(); } } + +// Server adoption with AbortSignal: aborting inside the listening callback +// closes the server and emits 'close'. +{ + const controller = new AbortController(); + const bound = new net.BoundSocket({ host: '127.0.0.1', port: 0 }); + const server = net.createServer(); + + server.on('close', common.mustCall()); + + server.listen({ + handle: bound, + signal: controller.signal, + }, common.mustCall(() => { + controller.abort(); + })); +} + +// Server adoption with AbortSignal: aborting immediately after listen() +// closes the server without leaking the handle. +{ + const controller = new AbortController(); + const bound = new net.BoundSocket({ host: '127.0.0.1', port: 0 }); + const server = net.createServer(); + + server.on('close', common.mustCall()); + + server.listen({ + handle: bound, + signal: controller.signal, + }); + controller.abort(); +} + +// Server adoption with a pre-aborted AbortSignal closes the server. +{ + const bound = new net.BoundSocket({ host: '127.0.0.1', port: 0 }); + const server = net.createServer(); + + server.on('close', common.mustCall()); + + server.listen({ + handle: bound, + signal: AbortSignal.abort(), + }); +} + +// Server adoption with an invalid signal throws before consuming BoundSocket. +{ + const bound = new net.BoundSocket({ host: '127.0.0.1', port: 0 }); + const server = net.createServer(); + + assert.throws(() => { + server.listen({ + handle: bound, + signal: 'INVALID_SIGNAL', + }); + }, { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + }); + + // Ownership was not transferred, so address() and close() still work. + assert.strictEqual(typeof bound.address().port, 'number'); + bound.close(); +} + +// Pipe BoundSocket adoption with AbortSignal closes the server on abort. +{ + const controller = new AbortController(); + const bound = new net.BoundSocket({ path: `${common.PIPE}-signal` }); + const server = net.createServer(); + + server.on('close', common.mustCall()); + + server.listen({ + handle: bound, + signal: controller.signal, + }, common.mustCall(() => { + controller.abort(); + })); +}