lwIP sockets: socket:close/1 does not release the processes selecting on the socket

Aperta Adatta ai principianti
#2,474 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, erlang

Direzione di ricerca

Leggi src/libAtomVM/otp_socket.c, concentrandoti sul ramo OTP_SOCKET_LWIP di nif_socket_close; confronta la pulizia suggerita con il percorso BSD di socket_stop. Verifica come vengono gestiti socket_select_disown e ref_ticks sia per le selezioni di lettura sia per quelle di scrittura. Il lavoro è completato quando la chiusura di un socket lwIP libera i processi in attesa di selezione e consente l’esecuzione del distruttore della risorsa mentre tali processi sono ancora attivi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Affected: release-0.7 at ca1583068, lwIP socket backend (RP2 port; ESP32 uses BSD sockets).
Not fixed by #2345 (c4a2824f4), which extends the problem to the write direction.

What happens

On BSD sockets, socket:close/1 stops the select event and the stop callback
demonitors the selecting process, so the resource can be freed.

On lwIP, nif_socket_close only sends the closed notification. The selecting
process stays registered as owner: its pid, its monitor and the reference the
monitor holds on the resource are never released.

The socket resource (struct, rwlock, unread pbufs in received_list) is therefore
kept alive until the owning process exits, or selects again on that same socket.

A long-lived process that opens, uses and closes many sockets leaks one resource
per connection for its whole lifetime. With #2345, a process that waited for write
readiness in socket:send/2 leaks the same way.

How to reproduce (Pico W)

In one process, loop:

{ok, S} = socket:open(inet, stream, tcp),
ok = socket:connect(S, Peer),
{ok, _} = socket:recv(S, 0, 1000),   % arms a read select: this process becomes owner
ok = socket:close(S)

Add a trace to socket_dtor: it does not run while the process lives.
Free heap shrinks on every iteration.

Where

src/libAtomVM/otp_socket.c, nif_socket_close, #elif OTP_SOCKET_LWIP branch.

Suggested fix

After sending the closed notifications, disown both directions
(socket_select_disown on read_select and write_select, reset their
ref_ticks), as socket_stop does on BSD sockets.

Lingua principale
Erlang
Stelle
2.3k
Fork
171
Merge medio
4g 11h
PR unite (30g)
20

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di atomvm/AtomVM

Tutte le issue di atomvm/AtomVM

Issue simili

Altre issue su Embedded & IoT

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.