Create a gist now

Instantly share code, notes, and snippets.

@dshaw /domainsocket.js Secret
Created Jun 26, 2012

Embed
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

This comment has been minimized.

Show comment
Hide comment
@dshaw

dshaw 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
Owner

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

This comment has been minimized.

Show comment
Hide comment
@dshaw

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

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

This comment has been minimized.

Show comment
Hide comment
@dshaw

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

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

This comment has been minimized.

Show comment
Hide comment
@Globik

Globik May 17, 2018

And now?

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