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 <riclau@uk.ibm.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent 879c0f1 commit cd227eb

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎lib/net.js‎

Lines changed: 7 additions & 0 deletions
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+
returnthis;
1443+
}
1444+
14381445
letmode=0;
14391446
if(options.readableAll===true)
14401447
mode|=PipeConstants.UV_READABLE;

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

Lines changed: 20 additions & 0 deletions
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+
consthandlePath=randomPipePath();
76+
constsrv1=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+
constsrv2=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(/^listenEADDRINUSE:addressalreadyinuse/.test(err.message));
89+
}));
90+
});
91+
}

0 commit comments

Comments
 (0)