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) { fs.stat(path, function (err, r) {
if (err) { if (err) {
if (err.errno == ENOENT) { if (err.errno == ENOENT) {
bind(self.fd, path); self._doListen(path);
self._doListen();
} else { } else {
throw r; throw r;
} }
@ -1136,12 +1135,8 @@ Server.prototype.listen = function () {
throw new Error("Non-socket exists at " + path); throw new Error("Non-socket exists at " + path);
} else { } else {
fs.unlink(path, function (err) { fs.unlink(path, function (err) {
if (err) { if (err) throw err;
throw err; self._doListen(path);
} else {
bind(self.fd, path);
self._doListen();
}
}); });
} }
} }
@ -1151,10 +1146,7 @@ Server.prototype.listen = function () {
// The port can be found with server.address() // The port can be found with server.address()
self.type = 'tcp4'; self.type = 'tcp4';
self.fd = socket(self.type); self.fd = socket(self.type);
bind(self.fd, port); self._doListen(port);
process.nextTick(function () {
self._doListen();
});
} else { } else {
// the first argument is the port, the second an IP // the first argument is the port, the second an IP
dns.lookup(arguments[1], function (err, ip, addressType) { dns.lookup(arguments[1], function (err, ip, addressType) {
@ -1163,8 +1155,7 @@ Server.prototype.listen = function () {
} else { } else {
self.type = addressType == 4 ? 'tcp4' : 'tcp6'; self.type = addressType == 4 ? 'tcp4' : 'tcp6';
self.fd = socket(self.type); self.fd = socket(self.type);
bind(self.fd, port, ip); self._doListen(port, ip);
self._doListen();
} }
}); });
} }
@ -1187,12 +1178,32 @@ Server.prototype._startWatcher = function () {
}; };
Server.prototype._doListen = function () { Server.prototype._doListen = function () {
var self = this;
try { try {
listen(this.fd, this._backlog || 128); bind(self.fd, arguments[0], arguments[1]);
this._startWatcher();
} catch (err) { } 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) { var server2 = net.createServer(function (socket) {
}); });
server1.listen(31337); server1.listen(common.PORT);
server2.addListener('error', function(error) { server2.addListener('error', function(error) {
assert.equal(true, error.message.indexOf('EADDRINUSE') >= 0); assert.equal(true, error.message.indexOf('EADDRINUSE') >= 0);
server1.close(); server1.close();
}); });
server2.listen(31337); server2.listen(common.PORT);

Loading…
Cancel
Save