Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
82 changes: 82 additions & 0 deletions test/parallel/test-net-boundsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));
}