Skip to content

Race condition in example? #2

@hammingweight

Description

@hammingweight

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions