Skip to content

Conversation

@david-cermak
Copy link
Collaborator

mDNS socket layer cleanup (mdns_networking_socket.c)

This note documents follow-up fixes applied to the BSD-socket based mDNS networking backend (components/mdns/mdns_networking_socket.c).

1. Deterministic RX task shutdown (remove fixed delay)

Problem: mdns_priv_if_deinit() previously stopped the RX task with a fixed vTaskDelay(500ms), which is not a deterministic synchronization mechanism (task may still be running; rapid re-init can race).

Correction:

  • Track the RX task using s_sock_recv_task_handle.
  • Add a binary semaphore s_sock_recv_task_exit_sem.
  • On last-interface deinit:
    • set s_run_sock_recv_task = false
    • block on s_sock_recv_task_exit_sem (bounded wait)
    • clear the task handle
  • In the RX task: xSemaphoreGive(s_sock_recv_task_exit_sem) immediately before vTaskDelete(NULL).

2. Improve select()/recvfrom() unblocking during socket close

Problem: Closing a socket from another task does not reliably and immediately unblock a thread blocked in select()/recvfrom() on all stacks/ports.

Correction:

  • delete_socket() now performs a best-effort shutdown(sock, SHUT_RDWR) prior to close(sock) to encourage prompt unblocking.

3. recvfrom() error handling (avoid repeated error/log-spin)

Problem: A recvfrom() error only broke out of the inner per-interface loop, leaving the RX task alive and potentially re-hitting the same invalid socket each select cycle.

Correction:

  • On recvfrom() failure:
    • log at WARN
    • close the socket (delete_socket(sock))
    • set s_interfaces[tcpip_if].sock = -1
    • clear s_interfaces[tcpip_if].proto = 0
    • continue with other interfaces
  • select() now ignores EINTR by retrying the loop.

4. Multicast group join failure must not mark protocol as ready

Problem: create_pcb() set the per-interface protocol bit even when join_mdns_multicast_group() failed, resulting in an interface being treated as ready for that protocol while multicast RX might be non-functional.

Correction:

  • If join_mdns_multicast_group() fails, create_pcb() returns false and does not set the protocol bit.
  • If the socket was newly created for this interface and no other protocol is active, it is closed to avoid leaking a socket with no valid membership.

5. Normalize socket close paths

Problem: Socket cleanup was split between direct close() calls and delete_socket(), risking divergence if delete_socket() gains additional behavior.

Correction:

  • create_socket() error path now calls delete_socket(sock) instead of close(sock).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants