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 :)
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_semaphore is 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/bitzap_sr Jan 05 '25
GCC has abitags. It was used when c++11 changed semantics of std::string and std::list. Presumably could be used here.
2
u/Xoipos Jan 05 '25
Hey Thanks! I didn't know that gcc did ABI changes (see this page for more info, for anyone curious).
However, the changes listed on aforementioned page are mostly minor or the benefit is so big that it's considered worth it by the gcc people. For bigger changes, recompilation is usually necessary. This can range from just recompiling your own software to having to recompile your own software, all its dependencies and/or the entire OS. In those cases, it is unlikely that a vendor (e.g. gcc) will break the ABI.
Whether that is the case for the bug I linked, I don't quite know.
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.
6
u/Compux72 Jan 05 '25
Gnu projects are famous for being too difficult on the bureaucracy side. Good look trying to fix/report anything
1
u/j_kerouac Jan 06 '25
I actually think FSF projects are famous for producing high quality software, such as GCC, glibc, and much of the core linux ecosystem that the entire world runs on...
I think if you have code in a FSF project, it probably would make a nice line item on your resume...
11
u/bebuch Jan 05 '25
Good joke. Anyone here who tried to report a bug in MSVC? GCC and clang often react within a few hours or days. MS needs months in the most cases, even if you provide s minimal reproducible example. (Only MSVC, MS STL is another thing with excellent support.)
2
u/j_kerouac Jan 06 '25
Exactly. GCC isn't perfect, but it's free. So you can't really complain that this thing you are getting for free, that's better than the paid product in many cases, has a few bugs.
GCC is probably the most widely used C++ compiler, and it's gotten this far based on volunteer contributions.
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.
3
u/tialaramex Jan 05 '25 edited Jan 05 '25
No, the Benaphore is not a futex. The Benaphore is a userspace optimisation named for Benoit Schilling at Be (I would guess others have invented this optimisation, but that's not important here), we still allocate a Semaphore (these are an expensive OS resource, I believe BeOS had 65536 total for the entire system) but we mostly avoid asking the OS to wait on the semaphore by using an atomic counter to count how many want the lock. When there's contention we'll know there are other lock users and we use our OS semaphore to handle that contention.
The futex is cleverer. We do not allocate any special OS resource at all - Linux literally doesn't care if you make ten million futexes - it's no worse than if you loaded a big file or something. We use an atomic counter again, but this time if we detect contention we use a special OS futex API to tell the OS that we're waiting on this specific futex and we want it to wake us up when the futex can be taken. There's nothing special about our counter, and the OS doesn't actually watch over it for us, instead other users of the futex signal the OS to say that this particular futex was contended and now it is free for somebody to take.
Edited: Apparently there were 65536 semaphores? It's too long ago to find out for sure.
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.
2
-14
u/sweetno Jan 04 '25
One more reason to stick to tried-and-true libraries instead of this C++ committee bastard child.
16
u/Jannik2099 Jan 04 '25
The committee has nothing to do with implementation bugs
-8
u/sweetno Jan 04 '25
The committee has nothing, but I have something to do with them. And the track record is such that the simplest way is to limit your relations with
std::to the minimum.I haven't learned the subtle ways how the standard committee manages to influence the quality of possible implementations, but the resulting experience consistently leaves me (and other people) disappointed.
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?