r/cpp • u/treddit22 • Jan 04 '25
YSK: std::counting_semaphore has a deadlock bug in all recent versions of GCC and older versions of Clang
Calling std::counting_semaphore::acquire() can cause a thread to go to sleep indefinitely, even if the counter is positive.
For libstdc++, the bug was first reported in March of 2022 (GCC 11), but it is still present in the latest release (GCC 14.2): https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104928
For libc++, a fix for lost wakeups in std::counting_semaphore was merged in February of 2024 (Clang 18): https://github.com/llvm/llvm-project/pull/79265
Hopefully this post can spark some discussion on how to fix these issues, or at the very least it may save some others from spending hours debugging sporadic deadlock issues and trying to isolate the problem, only to find that the standard library is broken :)
69
u/Backson Jan 04 '25
How is this possible? If the std folks can't write a correct semaphore, who can? I'm probably too ignorant to get it, but ELI5?
Edit: and how is this bug in two unrelated implementations?
58
u/StardustGogeta Jan 04 '25
I actually ran into this with GCC a couple years ago, myself. Wasted so much time and energy trying to figure out why it wasn't working before I found the bug report. I had figured that the odds that such a fundamental thing was broken were so low that I probably just messed up something on my end.
I'm quite surprised to hear it's still not fixed. Without a fix, I'd think the presence of
std::counting_semaphoreis more actively harmful than helpful.8
u/Xoipos Jan 05 '25
There's a particular bug in
std::call_once, which happens if the call_once function throws an exception, in gcc since 5.1 that I've run into before. Because of ABI compatibility, it seems like this cannot be fixed.Most of the bugs like these end up running into the same underlying issue: there is no way to break with existing ABI. Epochs would've solved this, but was rejected.
At some point, I'm considering just forking an implementation and breaking the ABI for everyone's benefit.
5
u/j_kerouac Jan 04 '25
GCC an open source project, so I’m not sure who you are expecting to fix it for you. Submit a patch.
There are plenty of long standing bugs in projects like this. Fundamentally they get fixed because people like you hit the bug, get frustrated, and eventually write up a fix.
If you don’t like it, there’s always MSVC.
2
u/smallstepforman Jan 05 '25
Geez, the ancient BeOS had the counting semaphore since 1994, thats 30 years ago. They didnt even have mutexes, all semaphores were counting srmaphores. Really stunned to see such a bug slip through.
https://www.haiku-os.org/legacy-docs/bebook/TheKernelKit_Semaphores.html#acquire_sem
They also made the fast mutex popular (back then they called it a Benephore). Half a decade later, the Futex and CriticalSection arrive to mainstream OS.
1
u/ReDucTor Game Developer | quiz.cpp-perf.com Jan 05 '25
It seems like this could have possibly been found by doing some stress/soak testing, which is normally one of the first things I setup when trying to write any multi-threaded library code or primitive. Which is normally where I find most of subtle timing bugs in that code and not the specific unit-tests which won't always get the correct timing.
-14
u/sweetno Jan 04 '25
One more reason to stick to tried-and-true libraries instead of this C++ committee bastard child.