|
|
Message-ID: <20260728044930.33864-1-aokblast@FreeBSD.org>
Date: Tue, 28 Jul 2026 12:49:30 +0800
From: ShengYi Hung <aokblast@...eBSD.org>
To: musl@...ts.openwall.com
Cc: ShengYi Hung <aokblast@...eBSD.org>
Subject: [PATCH] detect deadlock when waiting itself
It is possible to call pthread_join(pthread_self()) from user. In the
original implementation, it causes deadlock and never returns. In both
glibc and FreeBSD libc implementation. It returns a EDEADLK immediately.
This is found when running libcxx, which is built upon musl, testsuite.
In thread.jthread/join.deadlock test, jthread::join will throw an
exception if EDEADLK is returned. Thus can jump out from the deadlock.
---
src/thread/pthread_join.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/src/thread/pthread_join.c b/src/thread/pthread_join.c
index 17dae85d..b66c318c 100644
--- a/src/thread/pthread_join.c
+++ b/src/thread/pthread_join.c
@@ -10,9 +10,14 @@ weak_alias(dummy1, __tl_sync);
static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at)
{
int state, cs, r = 0;
+ pthread_t self = __pthread_self();
__pthread_testcancel();
__pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0);
+ if (self == t) {
+ __pthread_setcancelstate(cs, 0);
+ return EDEADLK;
+ }
while ((state = t->detach_state) && r != ETIMEDOUT && r != EINVAL) {
if (state >= DT_DETACHED) a_crash();
r = __timedwait_cp(&t->detach_state, state, CLOCK_REALTIME, at, 1);
--
2.55.0
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.