diff --git a/lib/net.js b/lib/net.js index 38ee1128e5..3f75d16b47 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1189,10 +1189,13 @@ Server.prototype._startWatcher = function () { }; Server.prototype._doListen = function () { - listen(this.fd, this._backlog || 128); - this._startWatcher(); -}; - + try { + listen(this.fd, this._backlog || 128); + this._startWatcher(); + } catch (err) { + this.emit('error', err); + } +} Server.prototype.address = function () { diff --git a/test/simple/test-net-eaddrinuse.js b/test/simple/test-net-eaddrinuse.js new file mode 100644 index 0000000000..7a46f6292c --- /dev/null +++ b/test/simple/test-net-eaddrinuse.js @@ -0,0 +1,14 @@ +common = require('../common'); +assert = common.assert +net = require('net'); + +var server1 = net.createServer(function (socket) { +}); +var server2 = net.createServer(function (socket) { +}); +server1.listen(31337); +server2.addListener('error', function(error) { + assert.equal(true, error.message.indexOf('EADDRINUSE') >= 0); + server1.close(); +}); +server2.listen(31337);