| Issue |
241092
|
| Summary |
[tsan] `pthread_once` hangs forever after the init routine exits by an exception.
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
Lynth-KR
|
When the init routine passed to `pthread_once` exits by throwing a C++ exception, ThreadSanitizer leaves the once
control in its "running" state. The next `pthread_once` on the same control then waits forever. Without TSan,
glibc resets the control, and the next call runs the init routine again.
This also hangs `std::call_once` with libstdc++, which is implemented on `pthread_once`. In the C++ standard
([thread.once.callonce]), a call whose callable throws is an exceptional execution, and there are passive executions
only if there is a returning one, so the next call on the flag must run its callable again. libc++'s
`std::call_once` does not use `pthread_once` and is not affected.
## Reproducer
```cpp
#include <pthread.h>
#include <cstdio>
#include <stdexcept>
static pthread_once_t _once_ = PTHREAD_ONCE_INIT;
static int calls = 0;
static void init() {
if (++calls == 1) throw std::runtime_error{"first"};
std::fprintf(stderr, "second ran\n");
}
int main() {
try {
pthread_once(&once, init);
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
}
pthread_once(&once, init); // hangs under TSan
std::fprintf(stderr, "done\n");
}
```
```console
$ clang++ -g pthread_once.cpp -o plain && ./plain
first
second ran
done
$ clang++ -g -fsanitize=thread pthread_once.cpp -o tsan && ./tsan
first
(hangs; a single thread waiting in __sanitizer::FutexWait)
```
The same with `std::call_once` and libstdc++:
```cpp
#include <cstdio>
#include <mutex>
#include <stdexcept>
int main() {
std::once_flag flag;
try {
std::call_once(flag, [] { throw std::runtime_error{"first"}; });
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
}
std::call_once(flag, [] { std::fprintf(stderr, "second ran\n"); }); // hangs under TSan
std::fprintf(stderr, "done\n");
}
```
Stack of the hung thread (GCC 13's libsanitizer; the same frames in compiler-rt):
```
#0 __sanitizer::FutexWait (p=0x721c00000450, cmp=196608) at sanitizer_common/sanitizer_linux.cpp:734
#1 guard_acquire (thr=..., pc=..., g=0x721c00000450, blocking_hooks=true) at tsan/tsan_interceptors_posix.cpp:897
#2 ___interceptor_pthread_once (o=0x721c00000450, f=0x7ffff6aeb420 <__once_proxy>) at tsan/tsan_interceptors_posix.cpp:1586
#3 __gthread_once (...) at bits/gthr-default.h:700
#4 std::call_once<...> (...) at mutex:907
```
## Tested
x86_64 Linux, glibc 2.39 (Ubuntu 24.04):
| Compiler | Standard library | `pthread_once` under TSan | `std::call_once` under TSan |
|---|---|---|---|
| Clang 23.1.5 (apt.llvm.org) | libstdc++ 13 | hangs | hangs |
| Clang 18.1.3 | libstdc++ 13 | hangs | hangs |
| GCC 13.3.0 (its own libsanitizer) | libstdc++ 13 | hangs | hangs |
| Clang 18.1.3 | libc++ 18 | - | runs to the end |
Without `-fsanitize=thread`, each of these runs to the end.
## Cause
The interceptor (`compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp`, main at c628d639fc66):
```cpp
if (guard_acquire(thr, pc, a, !SANITIZER_APPLE)) {
(*f)();
guard_release(thr, pc, a, kGuardDone);
}
```
`guard_acquire` moves the control from `kGuardInit` to `kGuardRunning`. If `f` unwinds, `guard_release` is never
called, so the control stays `kGuardRunning`, and the next call's `guard_acquire` sets `kGuardWaiter` and waits in
`FutexWait` for a wake that never comes.
The function-local static path already handles this case: the `__cxa_guard_abort` interceptor calls
`guard_release(thr, pc, g, kGuardInit)`, which resets the guard and wakes the waiters (D113713). `pthread_once` has
no such path.
glibc supports an init routine that exits by an exception: it resets the control during unwinding, so that a later
call runs the routine again (see glibc BZ #18435, which fixed the same hang on arm and ppc64 in glibc itself).
A fix would reset the control to `kGuardInit` and wake the waiters when `f` unwinds, as `__cxa_guard_abort` does.
I have not looked into how best to run that during unwinding, since the runtime is built without exceptions.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs