[llvm-bugs] [Bug 48560] New: this_thread::sleep_for(short duration) can sleep forever on Linux

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Dec 19 10:56:21 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48560

            Bug ID: 48560
           Summary: this_thread::sleep_for(short duration) can sleep
                    forever on Linux
           Product: libc++
           Version: 11.0
          Hardware: All
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: paul.groke at dynatrace.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

lib++ uses nanosleep in __libcpp_thread_sleep_for like this:

while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);

On Linux, such a nanosleep loop can run forever because of a Linux bug that's
documented here:
https://man7.org/linux/man-pages/man2/nanosleep.2.html#BUGS

For this to happen the thread needs to receive signals at a high frequency
which seems to be rather uncommon.

https://github.com/golang/proposal/blob/5b63da9579c3b19294be614dcad33e20a9a4ad22/design/24543-non-cooperative-preemption.md
https://github.com/golang/go/blob/go1.15.6/src/runtime/signal_unix.go#L347
https://github.com/golang/go/blob/go1.15.6/src/runtime/preempt.go#L223

This could lead to problems when on Linux, native code that uses libc++ is
called from a Go thread. The Go thread executing this_thread::sleep_for will
loop forever calling nanosleep and the other Go thread that tries to preempt it
will also loop forever, perpetually trying to preempt it.

I know this is a Linux bug, but since a lot of people are using Linux it might
be worth implementing a workaround in libc++.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201219/6a947f4a/attachment.html>


More information about the llvm-bugs mailing list