joyent/node — Issue #2325: callback of failed listen call wrongly invoked after successful call
Author: vincentcr
Created: 2011-12-14 00:05:20+00:00
State: open

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:

```

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();

s2.on('error', function(err) {
if (err.code !== 'EADDRINUSE') throw err;

//retry next port
s2.listen(port + 1, function(){
console.log('s2 listen # 2 successful:', s2.address().port);
});
});

s2.listen(port, function(){
console.log('s2 listen # 1 successful:', s2.address().port);
});

```

this outputs:

```

s1 listen successful: 12345
s2 listen # 1 successful: 12346
s2 listen # 2 successful: 12346

```

's2 listen # 1' should not happen because it wasn't, in fact, sucessful as the port was busy with s1.