gen_nb_server
gen_nb_server copied to clipboard
Race condition in example?
I think that there might be a (low probability) race condition in your example in the interaction between handle_info/3 and the worker/3 function. If the commands execute in the order indicated by the numbers below, the wrong process owns the socket after sending the response data.
handle_info({tcp, Sock, Data}, State) ->
- Me = self(),
- P = spawn(fun() -> worker(Me, Sock, Data) end),
- gen_tcp:controlling_process(Sock, P),
- {noreply, State};
worker(Owner, Sock, Data) -> 3 . gen_tcp:send(Sock, Data), 4 . inet:setopts(Sock, [{active, once}]), 5. gen_tcp:controlling_process(Sock, Owner).
I simulated the issue by inserting a timer:sleep(1000) between instructions 2 and 6.