-
-
Save dshaw/9f93cdcd3a77b9142e51 to your computer and use it in GitHub Desktop.
net.js UNIX domain socket issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
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);
});
});
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);
});
});
And now?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Here are the steps to reproduce:
nc -U /tmp/echo.sock
.)Error: listen EADDRINUSE