From 11f44d0025d18f84018e19bf9181ca74b98b2dd8 Mon Sep 17 00:00:00 2001 From: Sergiu Moga Date: Fri, 2 May 2025 12:38:02 +0300 Subject: [PATCH] lib/posix-process: Terminate underlying thread only if added to sched The pthread termination path may try to terminate the underlying thread if it's different from the current one. However this implies that it has been added to the scheduler at some point and this may not be true if, for example, it is simply being released on the error/cleanup path of some function that created said thread and hasn't gotten to adding it to a scheduler yet. Signed-off-by: Sergiu Moga Approved-by: Michalis Pappas Reviewed-by: Michalis Pappas GitHub-Closes: #1640 --- lib/posix-process/exit.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/posix-process/exit.c b/lib/posix-process/exit.c index d07bb3a2a..12dbb011f 100644 --- a/lib/posix-process/exit.c +++ b/lib/posix-process/exit.c @@ -131,12 +131,16 @@ void pprocess_exit_pthread(struct posix_thread *pthread, parent_pthread->state = POSIX_THREAD_RUNNING; } - /* Release pthread and terminate the undelying uk_thread - * unless it's the current one. + /* Release pthread and terminate the underlying uk_thread + * unless it's the current one or if it hasn't been associated + * with a scheduler yet (may happen if a thread is released + * before being added to the scheduler, e.g. on some error path + * that cleans up created threads that didn't get the chance to + * be added). */ thread = pthread->thread; pprocess_release_pthread(pthread); - if (thread != uk_thread_current()) + if (thread != uk_thread_current() && thread->sched) uk_sched_thread_terminate(thread); } -- 2.39.5