casaroli opened a new pull request, #22425: URL: https://github.com/apache/nuttx/pull/22425
## Summary Without `CONFIG_CANCELLATION_POINTS`, `pthread_cancel()` cancels a thread at once: the canceling thread runs the handlers on the cleanup stack of the target, then ends the target. The kernel uses the same stack for its own handlers: for example `udp_recvfrom()` pushes a handler that removes the receive callback of the socket while the thread waits. Since c86e7f6188a and 68163a6b3d8 the DNS lookup pushes up to three handlers (`getaddrinfo()`, `dns_query()`, `dns_foreach_nameserver()`) before the kernel pushes its handler. With `CONFIG_TLS_NCLEANUP` smaller than four, `tls_cleanup_push()` does not keep the handler of the kernel (6093cc100b3). A cancel during the lookup then does not remove the receive callback of the DNS socket, and the callback stays registered after the thread ended. This PR pushes the DNS handlers only with `CONFIG_CANCELLATION_POINTS`. Then the cancel ends the wait at the cancellation point in the thread itself: the kernel removes its callback when the wait returns, and the DNS handlers run in the thread, as intended. Without cancellation points the lookup does not free its buffer and socket on a cancel, as before c86e7f6188a. (Its DNS lock handler could not work in that case anyway: `dns_breaklock()` in the canceling thread does nothing, as that thread does not hold the lock.) ## Impact `libs/libc/netdb` only: three files, the handlers are now under `CONFIG_CANCELLATION_POINTS && CONFIG_TLS_NCLEANUP > 0`. No API, ABI or Kconfig change. `getaddrinfo_a()` already depends on `CONFIG_CANCELLATION_POINTS` and does not change. In-tree configurations with `CONFIG_TLS_NCLEANUP=1`, a DNS client and no cancellation points: `same70-xplained:pysim`, `nucleo-f746zg:pysim`, `nucleo-f746zg:note`, `nucleo-h743zi2:pysim`, `nucleo-h745zi:pysim_cm7`, `nucleo-h753zi:pysim`. ## Testing Code path, with `CONFIG_TLS_NCLEANUP=1` and no cancellation points: `getaddrinfo()` pushes its handler into the only entry (`libs/libc/netdb/lib_getaddrinfo.c`). `dns_query()` and then `udp_recvfrom()` (`net/udp/udp_recvfrom.c`) push beyond `CONFIG_TLS_NCLEANUP`, and `tls_cleanup_push()` does not keep their handlers. `pthread_cancel()` then runs `tls_cleanup_popall()` (`sched/pthread/pthread_cancel.c`) with only the handler of `getaddrinfo()`, so `udp_callback_cleanup()` does not run and the receive callback is not removed. With this change the handler of `udp_recvfrom()` is the first entry, and `udp_callback_cleanup()` removes the callback. Builds with `-Werror` (CI image): `qemu-armv8a:netnsh_smp` (flat), `qemu-armv8a:knsh` with networking and virtio-net added (kernel), `rv-virt:netnsh64` (flat), `rv-virt:knetnsh64` (kernel), each with packet sockets, netlink route and netfilter, and the DNS client enabled. No new warnings (the only warning is the RWX linker warning of `rv-virt:netnsh64`, also on master). checkpatch (`-c -u -m -g`) passes. Also `rv-virt:netnsh64` with `CONFIG_NETDB_RESOLVCONF=y`, `CONFIG_CANCELLATION_POINTS=y` and `CONFIG_TLS_NCLEANUP=4` (the handlers compiled in). The kernel builds above use `CONFIG_CANCELLATION_POINTS=y`, `CONFIG_TLS_NCLEANUP=4`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
