diff --git a/lib/net.js b/lib/net.js index f379d8d3a6..1877e42153 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1080,6 +1080,17 @@ var createServerHandle = exports._createServerHandle = return err; } + if (process.platform === 'win32') { + // On Windows, we always listen to the socket before sending it to + // the worker (see uv_tcp_duplicate_socket). So we better do it here + // so that we can handle any bind-time or listen-time errors early. + err = _listen(handle); + if (err) { + handle.close(); + return err; + } + } + return handle; }; @@ -1088,6 +1099,8 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) { debug('listen2', address, port, addressType, backlog); var self = this; + var alreadyListening = false; + // If there is not yet a handle, we need to create one and bind. // In the case of a server sent via IPC, we don't need to do this. if (!self._handle) { @@ -1100,6 +1113,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) { }); return; } + alreadyListening = (process.platform === 'win32'); self._handle = rval; } else { debug('_listen2: have a handle already'); @@ -1108,7 +1122,9 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) { self._handle.onconnection = onconnection; self._handle.owner = self; - var err = _listen(self._handle, backlog); + var err = 0; + if (!alreadyListening) + err = _listen(self._handle, backlog); if (err) { var ex = errnoException(err, 'listen');