This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 91259d9671b75c9de231812c24cf56a9b525b711 Author: ganjing <[email protected]> AuthorDate: Tue Nov 18 10:25:58 2025 +0800 sched/task: Wake a blocked thread on asynchronous cancellation. nxnotify_cancellation() only woke a thread that waited at a cancellation point with deferred cancellation. For a thread with asynchronous cancellation it returned false: pthread_cancel() then ran the cleanup handlers of the target in the context of the caller and terminated the target with nxtask_terminate(), also while the target was blocked. When a thread with asynchronous cancellation is blocked, mark the cancellation pending and wake it with ECANCELED as for deferred cancellation, so that it exits through its own cancellation path. A thread with asynchronous cancellation that is not blocked is still terminated by the caller. Assisted-by: Kiro:auto Signed-off-by: ganjing <[email protected]> --- sched/task/task_cancelpt.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/sched/task/task_cancelpt.c b/sched/task/task_cancelpt.c index 4f6d44a57c7..ea51269e1da 100644 --- a/sched/task/task_cancelpt.c +++ b/sched/task/task_cancelpt.c @@ -93,7 +93,7 @@ bool nxnotify_cancellation(FAR struct tcb_s *tcb) { FAR struct tls_info_s *tls = nxsched_get_tls(tcb); irqstate_t flags; - bool ret = false; + bool ret = true; /* We need perform the following operations from within a critical section * because it can compete with interrupt level activity. @@ -103,7 +103,8 @@ bool nxnotify_cancellation(FAR struct tcb_s *tcb) /* We only notify the cancellation if (1) the thread has not disabled * cancellation, (2) the thread uses the deferred cancellation mode, - * (3) the thread is waiting within a cancellation point. + * and is waiting within a cancellation point, (3) the thread uses the + * asynchronous cancellation mode and currently blocked. */ /* Check to see if this task has the non-cancelable bit set. */ @@ -125,24 +126,33 @@ bool nxnotify_cancellation(FAR struct tcb_s *tcb) */ tls->tl_cpstate |= CANCEL_FLAG_CANCEL_PENDING; - ret = true; } #ifdef CONFIG_CANCELLATION_POINTS - /* Check if this task supports deferred cancellation */ + /* Handle deferred cancellation, and asynchronous cancellation of a + * blocked thread. The caller cancels a thread with asynchronous + * cancellation that is not blocked. + */ - if (!ret && (tls->tl_cpstate & CANCEL_FLAG_CANCEL_ASYNC) == 0u) + else if (((tls->tl_cpstate & CANCEL_FLAG_CANCEL_ASYNC) == 0u || + (tcb->task_state >= FIRST_BLOCKED_STATE && + tcb->task_state <= LAST_BLOCKED_STATE))) { - /* Then we cannot cancel the task asynchronously. */ - - ret = true; - /* Mark the cancellation as pending. */ tls->tl_cpstate |= CANCEL_FLAG_CANCEL_PENDING; - /* If the task is waiting at a cancellation point, then notify of the - * cancellation thereby waking the task up with an ECANCELED error. + /* Treat a blocked thread with asynchronous cancellation as if it + * waited at a cancellation point, so that it is woken below. + */ + + if ((tls->tl_cpstate & CANCEL_FLAG_CANCEL_ASYNC) != 0u) + { + tls->tl_cpcount = 1; + } + + /* If the task is within a cancellation point, wake it up with + * ECANCELED. */ if (tls->tl_cpcount > 0) @@ -200,6 +210,19 @@ bool nxnotify_cancellation(FAR struct tcb_s *tcb) } #endif + else + { + /* None of the conditions are met: The thread is not non-cancelable, + * and it's not in deferred cancellation mode (or in asynchronous + * cancellation mode but not blocked). + * We don't need to deliver the cancellation notification at this time. + * Return false to indicate that the cancellation notification + * was not needed. + */ + + ret = false; + } + leave_critical_section(flags); return ret; }
