2

I'm fairly sure that this is a bug in node v0.10.18, but it has created a pollution on my machine which I don't know how to clear.

I have this simple tcp server (coffee) script:

net = require 'net'
server = net.createServer ->
server.listen 'localhost:4545'

when I run it using coffee z.coffee and then press Ctrl+C to interrupt it, I am unable to run it again on the same port due to EADDRINUSE exception. The process repeats on different ports with the same results.

I am aware of other answers about a similar issue, but they aren't able to solve mine because even restarting the machine (osx ml) still leaves the port blocked. Obviously, ps -A | grep node shows nothing as well.

What can I do to free up the stuck ports again?

Edit

Here is an abstract of the comments below. It seems that node uses SO_REUSEADDR be default, so TIME_WAIT should not be the issue, especially since the ports have been stuck for over an hour. Neither netstat nor lsof as root show anything using the ports, and neither multiple reboots, nor killing all but essential programs helped the issue resolve. There is no VPN or firewall.

Community
  • 1
  • 1
Bijou Trouvaille
  • 8,794
  • 4
  • 39
  • 42
  • 1
    Without SO_REUSEADDR (or is it SO_REUSEPORT?) ports are not immediately reusable, but are "freed" after a little while. Have you waited a bit? – zneak Sep 12 '13 at 19:57
  • Also consider [the answer of this question](http://stackoverflow.com/q/4421633/) to see who is using the port. – zneak Sep 12 '13 at 20:00
  • @zneak perhaps not long enough? I'll try waiting if nothing else helps. Thanks – Bijou Trouvaille Sep 12 '13 at 20:05
  • It shouldn't take several *minutes*. If it still doesn't work, then something else is probably at play, and you should check which process is using the port. – zneak Sep 12 '13 at 20:12
  • @zneak I have tried your other suggestion and I do not see any processes using the ports that are blocked. Besides, this happens on any port that I try with this script. – Bijou Trouvaille Sep 12 '13 at 20:15
  • Was that with `sudo`? – zneak Sep 12 '13 at 20:21
  • @zneak, tried it again with sudo, still nothing – Bijou Trouvaille Sep 12 '13 at 20:25
  • Your socket is in the `WAIT_CLOSE_2` TCP state. Verify by running `netstat`. Using `SO_REUSEADDR` as @zneak said should do it. Did you try? (btw -- 2 minutes should be enough to wait, that's what the RFC says *IIRC*). – Nitzan Shaked Sep 12 '13 at 20:36
  • @NitzanShaked I've already waited over an hour. Also I'm not familiar with SO_REUSEADDR, so I'm still researching its usage. – Bijou Trouvaille Sep 12 '13 at 20:42
  • 1
    The port is still blocked after a reboot? If so, then it's not this code .. it's something else on your system. Firewall? VPN client ... something interfering. – WiredPrairie Sep 12 '13 at 20:43
  • @BijouTrouvaille -- the docs seem to say that node already uses `SO_REUSEADDR`, so I am forced to conclude it's something else. What does the output of `netstat -nlatp` say, when you run it as root? – Nitzan Shaked Sep 12 '13 at 20:48
  • @NitzanShaked `netstat -nlatp tcp` shows nothing connected to port 4545 – Bijou Trouvaille Sep 12 '13 at 20:53
  • @WiredPrairie neither a VPN nor a firewall are active. There aren't that many programs running either, with none that would do anything crazy to a network. Maybe I'm missing something, though. – Bijou Trouvaille Sep 12 '13 at 20:55

1 Answers1

1

https://github.com/joyent/node/blob/3d4c663ee68326990e0732a4aa76445688e1064e/lib/net.js#L1159

You are passing invalid arguments to server.listen. It is interpreting your string as a unix domain socket filesystem path.

This program works fine and can be killed and restarted immediately.

net = require "net"
server = net.createServer ->
  console.log "connection"
server.listen 1337, "127.0.0.1"

Pass correct arguments to server.listen and all is well.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274