Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

who uses a TCP port?

Tags:

tcp

erlang

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?

like image 793
dijxtra Avatar asked Nov 16 '11 15:11

dijxtra


1 Answers

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

like image 83
antlersoft Avatar answered Oct 17 '22 01:10

antlersoft