title: Port messages returning to an unexpected process
body: <p>I have a simple ports app (literally the example from the <a href="http://erlang.org/doc/tutorial/c_port.html" rel="nofollow">Erlang -- Ports documentation</a>) and a GenServer controlling its use.</p>
  
  <p>The GenServer can communicate with the C app just fine but it doesn't receive the responses, iex or its supervisor does.  If I call <code>flush</code> from iex, I see the expected message.</p>
  
  <p>If I create a separate module and spawn a receive loop from it, it still doesn't receive the port response messages.</p>
  
  <p>I have a feeling I'm incorrectly opening the Port but cannot prove it.  Is there anything obvious I'm screwing up?</p>
  
  <pre><code>port = Port.open({:spawn, "./extprg"}, [{:packet, 2}, :exit_status])
  collect = fun () -&gt; collect_results(port) end
  spawn(collect)
  
  
  def collect_results(port) do
     receive do
        {^port, {:data, data}} -&gt; 
            #never gets called despite matching messages in flush
        {^port, {:exit_status, status}} -&gt;
            #never gets called...
  
        {:increment, value} -&gt;
            Port.command(port, [1, value])
            collect_results(port)
     end
  end
  </code></pre>