One of gen_servers in my app call gen_tcp:listen(Port, [{active, true}]). First time I run unit test, it returns {ok, Socket}, but second time I run the same unit test, it returns an {error, eaddrinuse}, but
lsof -i TCP
returns nothing. Also, when the same unit_test is run twice on another machine (WinXP), it works as expected (that is, returns {ok, Socket} both times). Therefore, my gen_server obviously releases the port, but Erlang somehow doesn't know that.
So, how can I figure out who does Erlang think uses this address?
This is because of details of the implementation of TCP on Unix systems-- when a socket is opened for listening, it will stay unavailable for a few minutes in the CLOSE_WAIT state after the listening process shuts down.
From Lukas' comment above: you can use reuseaddr flag to gen_tcp:listen to avoid this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With