<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - this_thread::sleep_for(short duration) can sleep forever on Linux"
href="https://bugs.llvm.org/show_bug.cgi?id=48560">48560</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>this_thread::sleep_for(short duration) can sleep forever on Linux
</td>
</tr>
<tr>
<th>Product</th>
<td>libc++
</td>
</tr>
<tr>
<th>Version</th>
<td>11.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>All Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>paul.groke@dynatrace.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
</td>
</tr></table>
<p>
<div>
<pre>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:
<a href="https://man7.org/linux/man-pages/man2/nanosleep.2.html#BUGS">https://man7.org/linux/man-pages/man2/nanosleep.2.html#BUGS</a>
For this to happen the thread needs to receive signals at a high frequency
which seems to be rather uncommon.
<a href="https://github.com/golang/proposal/blob/5b63da9579c3b19294be614dcad33e20a9a4ad22/design/24543-non-cooperative-preemption.md">https://github.com/golang/proposal/blob/5b63da9579c3b19294be614dcad33e20a9a4ad22/design/24543-non-cooperative-preemption.md</a>
<a href="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/signal_unix.go#L347</a>
<a href="https://github.com/golang/go/blob/go1.15.6/src/runtime/preempt.go#L223">https://github.com/golang/go/blob/go1.15.6/src/runtime/preempt.go#L223</a>
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++.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>