From d9694912a791bd1c667da8a418cc8e0f510ce3c6 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Fri, 13 May 2022 17:30:57 -0400 Subject: [PATCH] kernel: Have wait_for_thread fail with EDEADLK when the current thread is specified. This behavior is not required by POSIX (for pthread_join), but it is recommended and other OSes seem to implement it. This constitutes a behavioral change from BeOS, which just deadlocked as we do before this commit (I checked.) Preserving this behavior does not seem very useful, though if in the future we need to, adding a check to libroot is easy enough. This fixes a deadlock encountered when the usb_rndis driver is unplugged; the network stack has a removal path that invokes wait_for_thread on the receive thread, but the function can itself be called from the receive thread. Change-Id: Id3639d98a513f2b0f0bacc90d8d27abb5078857b Reviewed-on: https://review.haiku-os.org/c/haiku/+/5310 Reviewed-by: waddlesplash --- src/system/kernel/thread.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/system/kernel/thread.cpp b/src/system/kernel/thread.cpp index db6f4fe3c3..c0851d3cb4 100644 --- a/src/system/kernel/thread.cpp +++ b/src/system/kernel/thread.cpp @@ -2470,6 +2470,8 @@ wait_for_thread_etc(thread_id id, uint32 flags, bigtime_t timeout, { if (id < 0) return B_BAD_THREAD_ID; + if (id == thread_get_current_thread_id()) + return EDEADLK; // get the thread, queue our death entry, and fetch the semaphore we have to // wait on