]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-process: Terminate underlying thread only if added to sched
authorSergiu Moga <sergiu@unikraft.io>
Fri, 2 May 2025 09:38:02 +0000 (12:38 +0300)
committerUnikraft Bot <monkey@unikraft.io>
Fri, 2 May 2025 16:08:14 +0000 (16:08 +0000)
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 <sergiu@unikraft.io>
Approved-by: Michalis Pappas <michalis@unikraft.io>
Reviewed-by: Michalis Pappas <michalis@unikraft.io>
GitHub-Closes: #1640

lib/posix-process/exit.c

index d07bb3a2a1480fdf1c0f6a8e9ecffc431693484f..12dbb011f08dc9624cb5406182ec874e0cddfc16 100644 (file)
@@ -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);
 }