Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: net.Server.listen() avoid operations on null when fail #23920

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,13 @@ Server.prototype.listen = function(...args) {
backlog = options.backlog || backlogFromArgs;
listenInCluster(this, pipeName, -1, -1,
backlog, undefined, options.exclusive);

if (!this._handle) {
// Failed and an error shall be emitted in the next tick.
// Therefore, we directly return.
return this;
}

let mode = 0;
if (options.readableAll === true)
mode |= PipeConstants.UV_READABLE;
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-net-server-listen-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,23 @@ function randomPipePath() {
srv.close();
}));
}

// Test should emit "error" events when listening fails.
{
const handlePath = randomPipePath();
const srv1 = net.createServer().listen({ path: handlePath }, () => {
// As the handlePath is in use, binding to the same address again should
// make the server emit an 'EADDRINUSE' error.
const srv2 = net.createServer()
.listen({
path: handlePath,
writableAll: true,
}, common.mustNotCall());

srv2.on('error', common.mustCall((err) => {
srv1.close();
assert.strictEqual(err.code, 'EADDRINUSE');
assert(/^listen EADDRINUSE: address already in use/.test(err.message));
}));
});
}