Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition in example? #2

Open
hammingweight opened this issue Dec 14, 2012 · 0 comments
Open

Race condition in example? #2

hammingweight opened this issue Dec 14, 2012 · 0 comments

Comments

@hammingweight
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant