gen_nb_server icon indicating copy to clipboard operation
gen_nb_server copied to clipboard

Race condition in example?

Open hammingweight opened this issue 13 years ago • 0 comments

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) ->

  1. Me = self(),
  2. P = spawn(fun() -> worker(Me, Sock, Data) end),
  3. gen_tcp:controlling_process(Sock, P),
  4. {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.

hammingweight avatar Dec 14 '12 05:12 hammingweight