[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
Sat Nov 7 06:45:25 PST 2026
https://github.com/FireBurn created https://github.com/llvm/llvm-project/pull/239986
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
>From 5d3251a8dc421e0fd92f35eddde29c0d28510185 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] [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 | 9 +++++++--
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, 12 insertions(+), 6 deletions(-)
diff --git a/libc/src/__support/threads/unix_mutex.h b/libc/src/__support/threads/unix_mutex.h
index 272a1dfe6dbba3..c330aba5b4ec50 100644
--- a/libc/src/__support/threads/unix_mutex.h
+++ b/libc/src/__support/threads/unix_mutex.h
@@ -73,8 +73,13 @@ 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);
More information about the libc-commits
mailing list