This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 61d0e75d09e libc/netdb: Push the cleanup handlers only with
cancellation points.
61d0e75d09e is described below
commit 61d0e75d09ebc868bb5a0d4e83c1fb6c595a5528
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 08:48:47 2026 +0100
libc/netdb: Push the cleanup handlers only with cancellation points.
Without CONFIG_CANCELLATION_POINTS, pthread_cancel() cancels a thread at
once: the canceling thread runs the handlers on the cleanup stack of the
target, and then ends the target. The kernel uses the same stack. For
example, udp_recvfrom() pushes a handler that removes the receive
callback of the socket while the thread waits.
The DNS lookup pushes up to three handlers (getaddrinfo(), dns_query()
and dns_foreach_nameserver()) before the kernel pushes its handler.
When CONFIG_TLS_NCLEANUP is smaller than four, tls_cleanup_push() does
not keep the handler of the kernel. A cancel during the lookup then did
not remove the receive callback, and the callback stayed registered
after the thread ended.
Push the DNS handlers only with CONFIG_CANCELLATION_POINTS. Then a
cancel during the lookup 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. Without cancellation points a
cancelled lookup does not free its buffer and socket, as before. Its
handler could not release the DNS lock in that case anyway: the
canceling thread does not hold the lock.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
libs/libc/netdb/lib_dnsforeach.c | 6 +++---
libs/libc/netdb/lib_dnsquery.c | 6 +++---
libs/libc/netdb/lib_getaddrinfo.c | 9 ++++++++-
3 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/libs/libc/netdb/lib_dnsforeach.c b/libs/libc/netdb/lib_dnsforeach.c
index 42c31b3cfef..3878a577e08 100644
--- a/libs/libc/netdb/lib_dnsforeach.c
+++ b/libs/libc/netdb/lib_dnsforeach.c
@@ -72,7 +72,7 @@ struct dns_foreach_resolvconf_s
*
****************************************************************************/
-#if CONFIG_TLS_NCLEANUP > 0
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
static void dns_foreach_cleanup(FAR void *arg)
{
FAR struct dns_foreach_resolvconf_s *state = arg;
@@ -151,7 +151,7 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR
void *arg)
}
state.locked = false;
-#if CONFIG_TLS_NCLEANUP > 0
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
tls_cleanup_push(tls_get_info(), dns_foreach_cleanup, &state);
#endif
@@ -296,7 +296,7 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR
void *arg)
state.locked = false;
dns_unlock();
-#if CONFIG_TLS_NCLEANUP > 0
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
tls_cleanup_pop(tls_get_info(), 0);
#endif
fclose(state.stream);
diff --git a/libs/libc/netdb/lib_dnsquery.c b/libs/libc/netdb/lib_dnsquery.c
index 334618fe731..0ee27b523af 100644
--- a/libs/libc/netdb/lib_dnsquery.c
+++ b/libs/libc/netdb/lib_dnsquery.c
@@ -849,7 +849,7 @@ static void dns_query_error(FAR const char *prompt, int ret,
*
****************************************************************************/
-#if CONFIG_TLS_NCLEANUP > 0
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
static void dns_query_cleanup(FAR void *arg)
{
FAR struct dns_query_data_s *qdata = arg;
@@ -1087,7 +1087,7 @@ int dns_query(FAR const char *hostname, FAR union
dns_addr_u *addr,
}
qdata->sd = -1;
-#if CONFIG_TLS_NCLEANUP > 0
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
tls_cleanup_push(tls_get_info(), dns_query_cleanup, qdata);
#endif
@@ -1119,7 +1119,7 @@ int dns_query(FAR const char *hostname, FAR union
dns_addr_u *addr,
/* Free the query data */
-#if CONFIG_TLS_NCLEANUP > 0
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
tls_cleanup_pop(tls_get_info(), 0);
#endif
lib_free(qdata);
diff --git a/libs/libc/netdb/lib_getaddrinfo.c
b/libs/libc/netdb/lib_getaddrinfo.c
index aa0b816bd32..155101e7085 100644
--- a/libs/libc/netdb/lib_getaddrinfo.c
+++ b/libs/libc/netdb/lib_getaddrinfo.c
@@ -87,10 +87,12 @@ struct transport_s
*
****************************************************************************/
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
static void getaddrinfo_free(FAR void *arg)
{
lib_free(arg);
}
+#endif
/****************************************************************************
* Name: add_transports
@@ -493,14 +495,19 @@ int getaddrinfo(FAR const char *hostname, FAR const char
*servname,
}
/* The DNS lookup is a cancellation point: free the buffer if the
- * thread is cancelled there.
+ * thread is cancelled there. Without cancellation points the cancel
+ * is asynchronous, and the kernel needs the cleanup entries.
*/
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
tls_cleanup_push(tls_get_info(), getaddrinfo_free, hostbuffer);
+#endif
ret = gethostentbyname_r(hostname, &host,
hostbuffer, CONFIG_NETDB_BUFSIZE,
&herrno, flags);
+#if defined(CONFIG_CANCELLATION_POINTS) && CONFIG_TLS_NCLEANUP > 0
tls_cleanup_pop(tls_get_info(), 0);
+#endif
if (ret != OK)
{
lib_free(hostbuffer);