[libc-commits] [libc] [libc] Return EBUSY from pthread_mutex_destroy on a locked mutex (PR #239986)
Mike Lothian via libc-commits
libc-commits at lists.llvm.org
Wed Nov 11 19:00:27 PST 2026
https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/239986
>From 7144bf7421ae0405ec3edf7f4a434fd538d7ba1b Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sat, 7 Nov 2026 14:22:25 +0000
Subject: [PATCH 1/3] [libc] Return EBUSY from pthread_mutex_destroy on a
locked mutex
glibc leaves a locked mutex as it was and returns EBUSY, except for a robust
one, whose owner may have exited. Here it hit LIBC_ASSERT, so a program that
destroys a mutex it still holds aborted inside the library where it carries
on with glibc.
Assisted-by: Claude
---
libc/src/__support/threads/unix_mutex.h | 8 ++++++--
libc/src/pthread/CMakeLists.txt | 1 +
libc/src/pthread/pthread_mutex_destroy.cpp | 7 +++----
libc/test/integration/src/pthread/pthread_mutex_test.cpp | 1 +
4 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/libc/src/__support/threads/unix_mutex.h b/libc/src/__support/threads/unix_mutex.h
index 272a1dfe6dbba3..59d61b497d88b0 100644
--- a/libc/src/__support/threads/unix_mutex.h
+++ b/libc/src/__support/threads/unix_mutex.h
@@ -73,8 +73,12 @@ class Mutex final : private RawMutex {
error_checking(is_error_checking), owner(0), lock_count(0) {}
LIBC_INLINE static MutexError destroy(Mutex *lock) {
- LIBC_ASSERT(lock->owner == 0 && lock->lock_count == 0 &&
- "Mutex destroyed while being locked.");
+ // glibc says EBUSY and leaves a locked mutex as it was, except a robust
+ // one, whose owner may have exited. Only recursive and error checking
+ // mutexes record an owner, so the futex word is what tells a locked one.
+ if (!lock->robust && (lock->owner != 0 || lock->lock_count != 0 ||
+ lock->futex.load(cpp::MemoryOrder::RELAXED) != 0))
+ return MutexError::BUSY;
RawMutex::destroy(lock);
return MutexError::NONE;
}
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 5190c0ceacaa74..a00e31094eaca2 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -558,6 +558,7 @@ add_entrypoint_object(
HDRS
pthread_mutex_destroy.h
DEPENDS
+ libc.hdr.errno_macros
libc.include.pthread
libc.src.__support.threads.mutex
)
diff --git a/libc/src/pthread/pthread_mutex_destroy.cpp b/libc/src/pthread/pthread_mutex_destroy.cpp
index 4603ebed394ad8..e914fa4a48d47f 100644
--- a/libc/src/pthread/pthread_mutex_destroy.cpp
+++ b/libc/src/pthread/pthread_mutex_destroy.cpp
@@ -8,9 +8,11 @@
#include "pthread_mutex_destroy.h"
+#include "hdr/errno_macros.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/threads/mutex.h"
+#include "src/__support/threads/mutex_common.h"
#include <pthread.h>
@@ -18,10 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, pthread_mutex_destroy, (pthread_mutex_t * mutex)) {
auto *m = reinterpret_cast<Mutex *>(mutex);
- Mutex::destroy(m);
- // TODO: When the Mutex class supports all the possible error conditions
- // return the appropriate error value here.
- return 0;
+ return Mutex::destroy(m) == MutexError::BUSY ? EBUSY : 0;
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
index 1a27fec139f982..b5bc9c776f0dfd 100644
--- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
@@ -229,6 +229,7 @@ void initializer_acts_the_same_as_null_attr() {
ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_lock(&mutex), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_trylock(&mutex), EBUSY);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_destroy(&mutex), EBUSY);
ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&mutex), 0);
ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_destroy(&mutex), 0);
>From ae1ea8db0b62c0680c652df0fdd58c8ae1f2e734 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Wed, 11 Nov 2026 15:06:20 +0000
Subject: [PATCH 2/3] [libc] Check only the futex word when destroying a mutex
The owner and the lock count are only set while the futex word is held, so
the word alone tells a locked mutex. Document the best effort EBUSY in the
undefined behavior notes.
Assisted-by: Claude
---
libc/docs/dev/undefined_behavior.md | 10 ++++++++++
libc/src/__support/threads/unix_mutex.h | 10 +++++-----
2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/libc/docs/dev/undefined_behavior.md b/libc/docs/dev/undefined_behavior.md
index 9a2fb7ac8b495b..1316b9d17322b2 100644
--- a/libc/docs/dev/undefined_behavior.md
+++ b/libc/docs/dev/undefined_behavior.md
@@ -110,6 +110,16 @@ POSIX.1-2024, where EINVAL is returned if the spinlock is invalid (here we only
EBUSY is returned if the spinlock is currently locked. The lock is poisoned after a successful destroy. That is,
subsequent operations on the lock object without any reinitialization will return EINVAL.
+### PThread Mutex Destroy
+
+POSIX.1-2024 moved `EBUSY` from `pthread_mutex_destroy` to its rationale: an
+implementation may detect a locked mutex, and the detection is best effort. We
+return `EBUSY` and leave the mutex untouched when the futex word shows it held.
+The check can miss a mutex that another thread takes right afterwards and can
+report one that is being released, so it only catches a mutex that is held when
+the call starts. A robust mutex is destroyed regardless, since its owner may
+have exited.
+
### Strftime
In the C Standard, it provides a list of modifiers, and the conversions these
diff --git a/libc/src/__support/threads/unix_mutex.h b/libc/src/__support/threads/unix_mutex.h
index 59d61b497d88b0..9e652e947447f6 100644
--- a/libc/src/__support/threads/unix_mutex.h
+++ b/libc/src/__support/threads/unix_mutex.h
@@ -73,11 +73,11 @@ class Mutex final : private RawMutex {
error_checking(is_error_checking), owner(0), lock_count(0) {}
LIBC_INLINE static MutexError destroy(Mutex *lock) {
- // glibc says EBUSY and leaves a locked mutex as it was, except a robust
- // one, whose owner may have exited. Only recursive and error checking
- // mutexes record an owner, so the futex word is what tells a locked one.
- if (!lock->robust && (lock->owner != 0 || lock->lock_count != 0 ||
- lock->futex.load(cpp::MemoryOrder::RELAXED) != 0))
+ // Best effort, as POSIX leaves it: glibc says EBUSY and leaves a locked
+ // mutex as it was, except a robust one, whose owner may have exited. The
+ // owner and the lock count are only set while the futex word is held, so
+ // the word alone tells a locked mutex.
+ if (!lock->robust && lock->futex.load(cpp::MemoryOrder::RELAXED) != 0)
return MutexError::BUSY;
RawMutex::destroy(lock);
return MutexError::NONE;
>From f52bd148793d22d8fef33099b99f1b5f8fe22cc7 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Thu, 12 Nov 2026 03:00:15 +0000
Subject: [PATCH 3/3] [libc] Do not name glibc in the mutex destroy comment
Assisted-by: Claude
---
libc/src/__support/threads/unix_mutex.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/threads/unix_mutex.h b/libc/src/__support/threads/unix_mutex.h
index 9e652e947447f6..aa2945cce34627 100644
--- a/libc/src/__support/threads/unix_mutex.h
+++ b/libc/src/__support/threads/unix_mutex.h
@@ -73,10 +73,10 @@ class Mutex final : private RawMutex {
error_checking(is_error_checking), owner(0), lock_count(0) {}
LIBC_INLINE static MutexError destroy(Mutex *lock) {
- // Best effort, as POSIX leaves it: glibc says EBUSY and leaves a locked
- // mutex as it was, except a robust one, whose owner may have exited. The
- // owner and the lock count are only set while the futex word is held, so
- // the word alone tells a locked mutex.
+ // Best effort, as POSIX leaves it: other implementations say EBUSY and
+ // leave a locked mutex as it was, except a robust one, whose owner may have
+ // exited. The owner and the lock count are only set while the futex word is
+ // held, so the word alone tells a locked mutex.
if (!lock->robust && lock->futex.load(cpp::MemoryOrder::RELAXED) != 0)
return MutexError::BUSY;
RawMutex::destroy(lock);
More information about the libc-commits
mailing list