joyent/node — Issue #2194: UDP4 socket fails to bind with clustering
Author: jedsmallwood
Created: 2011-11-26 04:23:41+00:00
State: open

It appears that there is a bug with dgram in that subsequent workers fail to bind to the specified port. I did a search and it appears that a similar bug existed in the LearnBoost/cluster project in 0.6.6; https://github.com/LearnBoost/cluster/issues/129

This bug exists in 0.6.2 on OSX at least. The following is some sample code that demonstrates the problem.

```javascript
var util = require("util"),
dgram = require("dgram"),
cluster = require('cluster');

var udp = dgram.createSocket("udp4");
var port = 1190;

if (cluster.isMaster) {
for (i = 0; i < 2; i++) {
cluster.fork();
}
} else {
util.log("starting udp server on port " + port);
udp.on("error", function (error) {
util.log("failed to bind to UDP port - " + error)
});
udp.bind(port);
}
```

Running this code you see the following:

$ node udp-cluster.js
25 Nov 23:05:53 - starting udp server on port 1190
25 Nov 23:05:53 - starting udp server on port 1190
25 Nov 23:05:53 - failed to bind to UDP port - Error: bind EADDRINUSE