Skip to content

Commit 6920c66

Browse files
committed
Network.erl fix stop returning too early
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>
1 parent fc4f392 commit 6920c66

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ certain VM instructions are used.
3838
shallow comparison was performed, so it was working just for plain values such as atoms and small
3939
integers
4040
- Fixed support for setting esp32 boot_path in NVS.
41+
- Fixed race conditions in network:start/stop.
42+
- Fixed crash calling network:sta_rssi(), when network not up.
4143

4244
## [0.6.5] - 2024-10-15
4345

libs/eavmlib/src/network.erl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,25 @@ handle_info(Msg, State) ->
370370
{noreply, State}.
371371

372372
%% @hidden
373-
terminate(_Reason, _State) ->
373+
%% Wait for port to be closed
374+
terminate(_Reason, State) ->
374375
Ref = make_ref(),
376+
Port = State#state.port,
377+
PortMonitor = erlang:monitor(port, Port),
375378
network_port ! {?SERVER, Ref, stop},
376-
ok.
379+
wait_for_port_close(PortMonitor, Port).
380+
381+
wait_for_port_close(PortMonitor, Port) ->
382+
receive
383+
{'DOWN', PortMonitor, port, Port, _DownReason} ->
384+
ok;
385+
_Other ->
386+
% Handle unexpected messages if necessary
387+
wait_for_port_close(PortMonitor, Port)
388+
% Timeout after 1 second just in case.
389+
after 1000 ->
390+
{error, timeout}
391+
end.
377392

378393
%%
379394
%% Internal operations

0 commit comments

Comments
 (0)