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

Abierto Apto para principiantes
#2,474 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c, erlang

Línea de trabajo

Lee src/libAtomVM/otp_socket.c, centrándote en la rama OTP_SOCKET_LWIP de nif_socket_close; compara la limpieza sugerida con la ruta BSD de socket_stop. Comprueba cómo se gestionan socket_select_disown y ref_ticks tanto para las selecciones de lectura como para las de escritura. Se considera terminado cuando cerrar un socket lwIP libera los procesos que están seleccionando y permite que se ejecute el destructor del recurso mientras esos procesos siguen vivos.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Erlang
Estrellas
2.3k
Forks
171
Merge medio
4 d 11 h
PR fusionados (30 d)
20

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de atomvm/AtomVM

Todos los issues de atomvm/AtomVM

Issues similares

Más issues de Embedded & IoT

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.