Skip to content

Instantly share code, notes, and snippets.

@dshaw
Created June 26, 2012 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshaw/9f93cdcd3a77b9142e51 to your computer and use it in GitHub Desktop.
Save dshaw/9f93cdcd3a77b9142e51 to your computer and use it in GitHub Desktop.
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
Copy link
Author

dshaw commented Jun 26, 2012

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
Copy link
Author

dshaw commented Jun 26, 2012

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
Copy link
Author

dshaw commented Jun 26, 2012

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

@Globik
Copy link

Globik commented May 17, 2018

And now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment