Browse Source

Fix test-net-eaddrinuse

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
d9a5edb2b0
  1. 45
      lib/net.js
  2. 4
      test/simple/test-net-eaddrinuse.js

45
lib/net.js

@ -1126,8 +1126,7 @@ Server.prototype.listen = function () {
fs.stat(path, function (err, r) {
if (err) {
if (err.errno == ENOENT) {
bind(self.fd, path);
self._doListen();
self._doListen(path);
} else {
throw r;
}
@ -1136,12 +1135,8 @@ Server.prototype.listen = function () {
throw new Error("Non-socket exists at " + path);
} else {
fs.unlink(path, function (err) {
if (err) {
throw err;
} else {
bind(self.fd, path);
self._doListen();
}
if (err) throw err;
self._doListen(path);
});
}
}
@ -1151,10 +1146,7 @@ Server.prototype.listen = function () {
// The port can be found with server.address()
self.type = 'tcp4';
self.fd = socket(self.type);
bind(self.fd, port);
process.nextTick(function () {
self._doListen();
});
self._doListen(port);
} else {
// the first argument is the port, the second an IP
dns.lookup(arguments[1], function (err, ip, addressType) {
@ -1163,8 +1155,7 @@ Server.prototype.listen = function () {
} else {
self.type = addressType == 4 ? 'tcp4' : 'tcp6';
self.fd = socket(self.type);
bind(self.fd, port, ip);
self._doListen();
self._doListen(port, ip);
}
});
}
@ -1187,12 +1178,32 @@ Server.prototype._startWatcher = function () {
};
Server.prototype._doListen = function () {
var self = this;
try {
listen(this.fd, this._backlog || 128);
this._startWatcher();
bind(self.fd, arguments[0], arguments[1]);
} catch (err) {
this.emit('error', err);
self.emit('error', err);
return;
}
// Need to the listening in the nextTick so that people potentially have
// time to register 'listening' listeners.
process.nextTick(function () {
// It could be that server.close() was called between the time the
// original listen command was issued and this. Bail if that's the case.
// See test/simple/test-net-eaddrinuse.js
if (typeof self.fd != 'number') return;
try {
listen(self.fd, self._backlog || 128);
} catch (err) {
self.emit('error', err);
return;
}
self._startWatcher();
});
}

4
test/simple/test-net-eaddrinuse.js

@ -6,9 +6,9 @@ var server1 = net.createServer(function (socket) {
});
var server2 = net.createServer(function (socket) {
});
server1.listen(31337);
server1.listen(common.PORT);
server2.addListener('error', function(error) {
assert.equal(true, error.message.indexOf('EADDRINUSE') >= 0);
server1.close();
});
server2.listen(31337);
server2.listen(common.PORT);

Loading…
Cancel
Save