Skip to content

Commit cd227eb

Browse files
oyydtargos
authored andcommitted
net: net.Server.listen() avoid operations on null when fail
When `net.Server` fails to create a new handle, an error shall be emitted in the next tick. Therefore, we make `net.Server.listen()` directly return to avoid following operations on `null` `this._handle`. Fixes: #23917 PR-URL: #23920 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 879c0f1 commit cd227eb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/net.js

+7
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,13 @@ Server.prototype.listen = function(...args) {
14351435
backlog = options.backlog || backlogFromArgs;
14361436
listenInCluster(this, pipeName, -1, -1,
14371437
backlog, undefined, options.exclusive);
1438+
1439+
if (!this._handle) {
1440+
// Failed and an error shall be emitted in the next tick.
1441+
// Therefore, we directly return.
1442+
return this;
1443+
}
1444+
14381445
let mode = 0;
14391446
if (options.readableAll === true)
14401447
mode |= PipeConstants.UV_READABLE;

test/parallel/test-net-server-listen-path.js

+20
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,23 @@ function randomPipePath() {
6969
srv.close();
7070
}));
7171
}
72+
73+
// Test should emit "error" events when listening fails.
74+
{
75+
const handlePath = randomPipePath();
76+
const srv1 = net.createServer().listen({ path: handlePath }, () => {
77+
// As the handlePath is in use, binding to the same address again should
78+
// make the server emit an 'EADDRINUSE' error.
79+
const srv2 = net.createServer()
80+
.listen({
81+
path: handlePath,
82+
writableAll: true,
83+
}, common.mustNotCall());
84+
85+
srv2.on('error', common.mustCall((err) => {
86+
srv1.close();
87+
assert.strictEqual(err.code, 'EADDRINUSE');
88+
assert(/^listen EADDRINUSE: address already in use/.test(err.message));
89+
}));
90+
});
91+
}

0 commit comments

Comments
 (0)