<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3582: Unclear where std::async exceptions are handled</title>
<meta property="og:title" content="Issue 3582: Unclear where std::async exceptions are handled">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3582.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#New">New</a> status.</em></p>
<h3 id="3582"><a href="lwg-active.html#3582">3582</a>. Unclear where <code>std::async</code> exceptions are handled</h3>
<p><b>Section:</b> 32.10.9 <a href="https://wg21.link/futures.async">[futures.async]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2021-08-23 <b>Last modified:</b> 2021-09-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#futures.async">active issues</a> in [futures.async].</p>
<p><b>View all other</b> <a href="lwg-index.html#futures.async">issues</a> in [futures.async].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
32.10.9 <a href="https://wg21.link/futures.async">[futures.async]</a> (3.1) says:
</p>
<blockquote>
<p>
Any exception propagated from the execution of <code>invoke(<i>decay-copy</i>(std::forward&lt;F&gt;(f)), 
<i>decay-copy</i>(std::forward&lt;Args&gt;(args))...)</code> is stored as the exceptional result in the 
shared state.
</p>
</blockquote>
<p>
It's not clear whether this includes the evaluation of the <code><i>decay-copy</i></code> calls in the calling 
thread, or only the invocation of invoke with the results of those <code><i>decay-copy</i></code> calls.
<p/>
A literal reading suggests that any exceptions from any part of that expression should be stored in the 
shared state. All of libstdc++, libc++ and MSVC only store exceptions from the call to <code>invoke</code>, not the 
calls to <code><i>decay-copy</i></code>. Exceptions from the <code><i>decay-copy</i></code> calls are propagated 
to the caller of <code>std::async</code>. We should clarify that that's what the standard means.
</p>

<p><i>[2021-09-20; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>

<p><i>[2021-09-20;
Jonathan updates wording to change the <i>Throws:</i> and attempt to
align the <i>Effects:</i> with the deferred function case.
]</i></p>


<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/n4892">N4892</a>.
</p>

<ol>
<li><p>Modify 32.10.9 <a href="https://wg21.link/futures.async">[futures.async]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class F, class... Args&gt;
  [[nodiscard]] future&lt;invoke_result_t&lt;decay_t&lt;F&gt;, decay_t&lt;Args&gt;...&gt;&gt;
    async(F&amp;&amp; f, Args&amp;&amp;... args);
template&lt;class F, class... Args&gt;
  [[nodiscard]] future&lt;invoke_result_t&lt;decay_t&lt;F&gt;, decay_t&lt;Args&gt;...&gt;&gt;
    async(launch policy, F&amp;&amp; f, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-2- <i>Mandates</i>: [&hellip;]
<p/>
-3- <i>Effects</i>: The first function behaves the same as a call to the second function with a <code>policy</code> argument of
<code>launch::async | launch::deferred</code> and the same arguments for <code>F</code> and <code>Args</code>. The second function
creates a shared state that is associated with the returned <code>future</code> object. The further behavior of the second function 
depends on the <code>policy</code> argument as follows (if more than one of these conditions applies, the implementation may 
choose any of the corresponding policies):
</p>
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; If <code>launch::async</code> is set in <code>policy</code>, calls 
<code>invoke(<i>decay-copy</i>(std::forward&lt;F&gt;(f)), <i>decay-copy</i>(std::forward&lt;Args&gt;(args))...)</code> 
(22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>, 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a>) as if in a new thread of execution
represented by a <code>thread</code> object with the calls to <code><i>decay-copy</i></code> being evaluated 
in the thread that called <code>async</code>. Any return value is stored as the result in the shared state. Any 
exception propagated from the <del>execution of <code>invoke(<i>decay-copy</i>(std::forward&lt;F&gt;(f)), 
<i>decay-copy</i>(std::forward&lt;Args&gt;(args))...)</code></del><ins>call to <code>invoke</code></ins> is stored as 
the exceptional result in the shared state. <ins>[<i>Note ?:</i> Exceptions from the <code><i>decay-copy</i></code> 
calls are propagated to the caller. &mdash; <i>end note</i>]</ins> The <code>thread</code> object is stored in 
the shared state and affects the behavior of any asynchronous return objects that reference that state.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
</blockquote>
</li>

</ol>
</blockquote>



<p id="res-3582"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4892">N4892</a>.
</p>

<ol>
<li><p>Modify 32.10.9 <a href="https://wg21.link/futures.async">[futures.async]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class F, class... Args&gt;
  [[nodiscard]] future&lt;invoke_result_t&lt;decay_t&lt;F&gt;, decay_t&lt;Args&gt;...&gt;&gt;
    async(F&amp;&amp; f, Args&amp;&amp;... args);
template&lt;class F, class... Args&gt;
  [[nodiscard]] future&lt;invoke_result_t&lt;decay_t&lt;F&gt;, decay_t&lt;Args&gt;...&gt;&gt;
    async(launch policy, F&amp;&amp; f, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-2- <i>Mandates</i>: [&hellip;]
<p/>
-3- <i>Effects</i>: The first function behaves the same as a call to the second function with a <code>policy</code> argument of
<code>launch::async | launch::deferred</code> and the same arguments for <code>F</code> and <code>Args</code>. The second function
creates a shared state that is associated with the returned <code>future</code> object. The further behavior of the second function 
depends on the <code>policy</code> argument as follows (if more than one of these conditions applies, the implementation may 
choose any of the corresponding policies):
</p>
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; If <code>launch::async</code> is set in <code>policy</code>, calls 
<code>invoke(<i>decay-copy</i>(std::forward&lt;F&gt;(f)), <i>decay-copy</i>(std::forward&lt;Args&gt;(args))...)</code> 
(22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>, 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a>) as if in a new thread of execution
represented by a <code>thread</code> object with the calls to <code><i>decay-copy</i></code> being evaluated 
in the thread that called <code>async</code>. Any return value is stored as the result in the shared state. Any 
exception propagated from the execution of <code>invoke(<del><i>decay-copy</i>(std::forward&lt;F&gt;(f)), 
<i>decay-copy</i>(std::forward&lt;Args&gt;(args)...)</del><ins>std::move(g), std::move(xyz)</ins>)</code> is stored as 
the exceptional result in the shared state<ins>,
where <code>g</code> is the result of
<code><i>decay-copy</i>(std::forward&lt;F&gt;(f))</code>
and <code>xyz</code> is the result of
<code><i>decay-copy</i>(std::forward&lt;Args&gt;(args))...</code>
</ins>.
<ins>[<i>Note ?:</i> Exceptions from the <code><i>decay-copy</i></code> 
calls are propagated to the caller. &mdash; <i>end note</i>]</ins> The <code>thread</code> object is stored in 
the shared state and affects the behavior of any asynchronous return objects that reference that state.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
<p>[&hellip;]</p>
<p>-6- <i>Throws:</i>
<code>system_error</code> if <code>policy == launch::async</code>
and the implementation is unable to start a new thread;
<code><del>std::</del>bad_alloc</code> if memory for the internal data structures
  cannot be allocated<ins>;
or any exception thrown by the initialization of the objects returned by the
  <code><i>decay-copy</i></code> calls</ins>.</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
