<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/125428>125428</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
std::async in future does not throw system_error as required (libc++)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc++
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
slotosch
</td>
</tr>
</table>
<pre>
During the qualification of the C++ STL for safety we run into a corner case, which seam to be a clear, safety relevant bug (violation of requriements) and error handling is very safety critical. We found it in C++14, but it is also present in current version.
The C++14 requirement in 30.6.8 states for
Functions `std::async`
template <class F, class... Args>
future<result_of_t<decay_t<F>(decay_t<Args>...)>> async(F&& f, Args&&... args);
template <class F, class... Args>
future<result_of_t<decay_t<F>(decay_t<Args>...)>> async(launch policy, F&& f, Args&&... args);
Requirement 30.6.8-6,7
6 Throws: system_error if policy == launch::async and the implementation is unable to start a new thread.
7 Error conditions:
— **resource_unavailable_try_again — if policy == launch::async and the system is unable to start a new thread.**
**Steps to reproduce:**
Set limit of threads using
Linux, MacOS: setrlimit(RLIMIT_NPROC, lim)
QNX: setrlimit(RLIMIT_NTHR, lim)
Note 1: Use RLIMIT_NTHR for the number of threads per process on QNX. Linux and MacOS (pthread) does not support RLIMIT_NTHR and control the total number of Linux threads/processes with RLIMIT_NPROC. QNX has different threading model.
Note 2. Use reasonable limit numbers. Request the current limits first and do not exceed the current rlim_cur value.
Call async function with std::launch::async policy multiple times, more then rlim_cur value.
**Expected behavior:**
The function throws std::system_error exception which application may catch.
**Actual behavior:**
Application hangs.
**Issue analysis:**
In gcc (linux) the exception message is reported correctly.
Also a related bug report is found in another project. However it seems not reported to the LLVM development.
https://github.com/emscripten-core/emscripten/issues/8988
**Workaround (big pain!):**
Control the number of active threads created with `async` call and do not exceed the limits.
** Example to reproduce (using attached async_ex.txt)
[async_ex.txt](https://github.com/user-attachments/files/18633380/async_ex.txt)
See the difference in GNU libstdc++ and LLVM libc++ using Linux:
clang++ -stdlib=libc++ -o async_ex async_ex.txt
./async_ex
This hangs
clang++ -stdlib=libstdc++ -o async_ex async_ex.txt
./async_ex
Output:
Expected exception: Resource temporarily unavailable
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVsty67gR_Rpo02UWBUoytdCClq2Mq3x9E9uTyU4Fgk0RCQhw8JCtv08BoCRqMneSWWVlGurGOf06DWatOCjEDVk-kOXjjHnXabOxUjtteTerdXPaPHoj1AFch_CrZ1K0gjMntALdxsMtoQ-EPsD7xwu02oBlLboTfCIYr0Aop4EB10ahAc4sErqFz07wDiyyHpyGGoOFRGbCb6O_QYlHphzU_gCElkeh5QXX4K_eCOxROUvoGphqAI3RBjqmGhn4CgtHNKfzddwIJziTGfyC0GqvGhAOhDrTny8Cdu1dPLbApNUwGLSoohn3xoTPIxortMpIXn1cg58vIiVhIqVgX-TZKivBOubQxryQvNp5xUMIFsgqt64hRUWKitmT4mSVk7xy2A-SOQRSbLlk1sIu0IqfWZZBZQ6WFE8kr1rvvEFSbA1aL91et3tHim2DnJ3i1y7Y0fJ6MPpmWUboOvxYPEGCpuWO0BWhK2gDWjSM_wdIFv9bk-KB5NX_kaJkXvEOBi0FPwXE_5Xz26QwqSp3K0K39ymcFXx0Rn9aUlRgT9Zhv0-NJNoRC0jxSIpHSAQmJYtdFyZA9IOM96f-FBa8YrXE0NvWMeOAgcJPcJ1B1mQJ-B6eIg7XqhGxKcLVeUWeKClzsl4AoRWhlUGrveG494odmZDh4r0zpz07MKHgav4nCKdA_zvRSCDRTd_vDgcbrA0ORjeeY7h-YgYA8I4OpOiFSwoRrrLgbRjK0eJFKP8VyvaN8e_vMfXoTPQhtHx7ef72_LF__evb920wkqIPxUyuf3v9x4_sP356uzEnefWqHcI8OPxsESaGcSJDKpTvazRTpgMaGIzmaC1oFQCzRDimLzIOejQk-6A-jUYLSjuwfhi0cTdAwYlr5YyWEdBpx-QENl09ghO6G6HRwqdwHUyTkQ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