Skip to content

Commit

Permalink
Network.erl fix stop returning too early
Browse files Browse the repository at this point in the history
network:stop through terminate was using nonblocking call to stop network_port- so rapid stop/start would give the second network:start an old whereis(network_port) that was still shutting down, and lead to errors.

Now waits for port DOWN.

Signed-off-by: Peter M <petermm@gmail.com>
  • Loading branch information
petermm committed Jan 22, 2025
1 parent ca9e81b commit abac2d6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions libs/eavmlib/src/network.erl
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,25 @@ handle_info(Msg, State) ->
{noreply, State}.

%% @hidden
terminate(_Reason, _State) ->
%% Wait for port to be closed
terminate(_Reason, State) ->
Ref = make_ref(),
Pid = State#state.port,
PortMonitor = erlang:monitor(port, Pid),
network_port ! {?SERVER, Ref, stop},
ok.
wait_for_port_close(PortMonitor, Pid).

wait_for_port_close(PortMonitor, Pid) ->
receive
{'DOWN', PortMonitor, port, Pid, _DownReason} ->
ok;
_Other ->
% Handle unexpected messages if necessary
wait_for_port_close(PortMonitor, Pid)
% Timeout after 4000 milliseconds
after 4000 ->
{error, timeout}
end.

%%
%% Internal operations
Expand Down

0 comments on commit abac2d6

Please sign in to comment.