callback of failed listen call wrongly invoked after successful call #2325
Comments
What happens is that both calls to var http = require('http');
var port = 12345;
var s1 = http.createServer();
s1.listen(port, function(){
console.log('s1 listen successful:', s1.address().port);
});
var s2 = http.createServer();
// REGISTER HANDLER
s2.on('listening', function() {
console.log('s2 listen # 1 successful:', s2.address().port);
});
s2.on('error', function(err) {
if (err.code !== 'EADDRINUSE') throw err;
//retry next port
s2.listen(port + 1);
});
s2.listen(port); I agree it's somewhat confusing behaviour, we should probably fix it. |
thanks for the workaround. |
btw i would like to point out that this is mostly a documentation issue. the doc for net.listen makes clear what is happening and, if i had read it, i would not have thought of this behavior as a bug. however, the doc for http.listen (which is what i read) is more ambiguous. also it's not mentioned that http extends net (however obvious this seems once you know), and so that http.listen is in fact net.listen. |
@vincentcr: I agree. @bnoordhuis: Can you review koichik/node@4d954b8? |
@koichik: LGTM |
@bnoordhuis: Thanks! |
with node 0.6.5 on a mac:
it seems that if one retries a listen on a server after the first one failed (because, eg, we're trying to find a free port), the callbacks will be invoked both for the first, failed, call, and the second, successful call.
here's a sample that explains it better, i hope:
this outputs:
's2 listen # 1' should not happen because it wasn't, in fact, sucessful as the port was busy with s1.
The text was updated successfully, but these errors were encountered: