diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2012-02-09 06:22:50 +0100 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-03-05 15:23:55 +0100 |
commit | 532d9929c7e6eba8c35943f45dffc93926e34cb9 (patch) | |
tree | 6720a7818170671f2272b4cea44b0a324e727706 /lib | |
parent | ecf9f606c9540f12ba539ddfe7b7827e34388fc7 (diff) | |
download | android-node-v8-532d9929c7e6eba8c35943f45dffc93926e34cb9.tar.gz android-node-v8-532d9929c7e6eba8c35943f45dffc93926e34cb9.tar.bz2 android-node-v8-532d9929c7e6eba8c35943f45dffc93926e34cb9.zip |
cluster: propagate bind errors
This commit fixes a bug where the cluster module fails to propagate
EADDRINUSE errors.
When a worker starts a (net, http) server, it requests the listen socket
from its master who then creates and binds the socket.
Now, OS X and Windows don't always signal EADDRINUSE from bind() but
instead defer the error until a later syscall. libuv mimics this
behaviour to provide consistent behaviour across platforms but that
means the worker could end up with a socket that is not actually bound
to the requested addresss.
That's why the worker now checks if the socket is bound, raising
EADDRINUSE if that's not the case.
Fixes #2721.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/net.js | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/net.js b/lib/net.js index c8d7f60913..38985214d6 100644 --- a/lib/net.js +++ b/lib/net.js @@ -928,14 +928,26 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) { function listen(self, address, port, addressType, backlog, fd) { if (!cluster) cluster = require('cluster'); - if (cluster.isWorker) { - cluster._getServer(self, address, port, addressType, fd, function(handle) { - self._handle = handle; - self._listen2(address, port, addressType, backlog, fd); - }); - } else { + if (cluster.isMaster) { self._listen2(address, port, addressType, backlog, fd); + return; } + + cluster._getServer(self, address, port, addressType, fd, function(handle) { + // Some operating systems (notably OS X and Solaris) don't report EADDRINUSE + // errors right away. libuv mimics that behavior for the sake of platform + // consistency but that means we have have a socket on our hands that is + // not actually bound. That's why we check if the actual port matches what + // we requested and if not, raise an error. The exception is when port == 0 + // because that means "any random port". + if (port && handle.getsockname && port != handle.getsockname().port) { + self.emit('error', errnoException('EADDRINUSE', 'bind')); + return; + } + + self._handle = handle; + self._listen2(address, port, addressType, backlog, fd); + }); } |