Anonymous committed e2697e9

Try to avoid race condition in socket server.

Comments (0)

Files changed (1)


 -export([open/0, connect/1, listen/0, accept/1]).
 open() ->
     {LS, Port} = listen(),
     {LS, Port}.
 accept(LS) ->
-    spawn_link(?MODULE, do_accept, [LS]).
+    Pid = spawn_link(?MODULE, do_accept, [LS, self()]),
+    receive in_accept -> ok end,
+    Pid.
-do_accept(LS) ->
+do_accept(LS, Parent) ->
+    erlang:send_after(50, Parent, in_accept),
     {ok, S} = gen_tcp:accept(LS),
     {error, closed} = gen_tcp:recv(S, 0).
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.