Skip to content

@dshaw /domainsocket.js secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
net.js UNIX domain socket issue
var net = require('net');
var path = '/tmp/echo.sock';
var server = net.createServer(function(c) {
console.log('server connected');
c.on('end', function() {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(path, function() {
console.log('server bound on %s', path);
});
@dshaw
Owner

Damn myself. I saw this issue in v0.7 with my REPLs, but I thought it was me and the REPLs I was working with weren't key to the modules I was writing, so I just commented out that code. :rage2:

Here are the steps to reproduce:
1) Run the above.
2) (Optionally verify your server works with nc -U /tmp/echo.sock.)
3) Exit.
4) Run again.... Error: listen EADDRINUSE

@dshaw
Owner

This issue effects every REPL that's attached to my production code! A workaround for now would be to unlink (remove) the "file":

var net = require('net');
var fs = require('fs');
var path = '/tmp/echo.sock';
fs.stat(path, function (err) {
  if (!err) fs.unlinkSync(path);
  var server = net.createServer(function(c) {
    console.log('server connected');
    c.on('end', function() {
      console.log('server disconnected');
    });
    c.write('hello\r\n');
    c.pipe(c);
  });
  server.listen(path, function() {
    console.log('server bound on %s', path);
  });
});
@dshaw
Owner

Here's a nice little optimization from @kitcambridge, removing the stat call and sync:

var net = require('net');
var fs = require('fs');
var path = '/tmp/echo.sock';
fs.unlink(path, function () {
  var server = net.createServer(function(c) {
    console.log('server connected');
    c.on('end', function() {
      console.log('server disconnected');
    });
    c.write('hello\r\n');
    c.pipe(c);
  });
  server.listen(path, function() {
    console.log('server bound on %s', path);
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.