diff --git a/lib/net.js b/lib/net.js index 6802b66a980..2b1f020be65 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 2d11d0d4ba8..0400dc45a5e 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(); + })); +}