Skip to content

this_thread::sleep_for(short duration) can sleep forever on Linux #47904

Open
@llvmbot

Description

@llvmbot
Bugzilla Link 48560
Version 11.0
OS Linux
Reporter LLVM Bugzilla Contributor
CC @mclow

Extended Description

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++.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillalibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.threadingissues related to threading

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions