<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>let_async_scope.html</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

</head>

<body>

<h1 id="p3296r3-let_async_scope">P3296R3 <code>let_async_scope</code></h1>
<p>Date: 19th November 2024</p>
<p>Author: Anthony Williams <a href="mailto:anthony@justsoftwaresolutions.co.uk" class="email">anthony@justsoftwaresolutions.co.uk</a></p>
<p>Audience: SG1, LEWG</p>
<h2 id="background-and-motivation">Background and motivation</h2>
<p>This is intended to address concerns raised in LEWG about ensuring that a <code>counting_scope</code> (see <a href="https://isocpp.org/files/papers/P3149R3.html">P3149R3</a>) is joined: the scope provided by <code>let_async_scope</code> is always joined, irrespective of how the nested work completes, and whether or not the provided function throws an exception after spawning work.</p>
<p>Code with explicit <code>counting_scope</code>:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a>    <span class="dt">some_data_type</span> scoped_data = make_scoped_data();</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a>    counting_scope scope;</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a>    spawn(on(exec, [&amp;] {</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a>        spawn(on(exec, [&amp;] {</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a>            <span class="cf">if</span> (need_more_work(scoped_data)) {</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a>              spawn(on(exec, [&amp;] { do_more_work(scoped_data); }), scope);</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a>                spawn(on(exec, [&amp;] { do_more_other_work(scoped_data); }), scope);</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a>            }</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true"></a>        }),scope);</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true"></a>        spawn(on(exec, [&amp;] { do_something_else_with(scoped_data); }), scope);</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true"></a>    }),scope);</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true"></a></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true"></a>    maybe_throw();</span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true"></a></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true"></a>    this_thread::sync_wait(scope.join());</span></code></pre></div>
<p>Here, if <code>maybe_throw</code> throws an exception, then the scope is not joined, and the nested tasks can continue executing asynchronously, potentially accessing both the <code>scope</code> and <code>scoped_data</code> objects out of lifetime.</p>
<p>Using <code>let_async_scope</code> addresses this by encapsulating the scope object and the result of the previous sender. The returned sender does not complete until all tasks nested on the scope complete, even if the function passed to <code>let_async_scope</code> exits via an exception:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a>    <span class="kw">auto</span> scope_sender = just(make_scoped_data()) | let_async_scope([](<span class="kw">auto</span> scope_token,</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a>                                                                           <span class="kw">auto</span>&amp; scoped_data) {</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a>        spawn(on(exec, [scope_token, &amp;scoped_data] {</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a>            spawn(on(exec, [scope_token, &amp;scoped_data] {</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a>                <span class="cf">if</span> (need_more_work(scoped_data)) {</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a>                    spawn(on(exec, [&amp;scoped_data] { do_more_work(scoped_data); }),scope_token);</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a>                    spawn(on(exec, [&amp;scoped_data] { do_more_other_work(scoped_data); }),scope_token);</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a>                }</span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a>            }),scope_token);</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true"></a>            spawn(on(exec, [&amp;scoped_data] { do_something_else_with(scoped_data); }),scope_token);</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true"></a>        }),scope_token);</span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true"></a>        maybe_throw();</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true"></a>    });</span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true"></a></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true"></a>    this_thread::sync_wait(scope_sender);</span></code></pre></div>
<p>Here, even if <code>maybe_throw</code> throws an exception, then <code>scope_sender</code> doesn’t complete until all the nested tasks have completed. This prevents out-of-lifetime access to the <code>scoped_data</code> or the scope itself, unless references to the data or <code>scope_token</code> are stored outside the sender tree.</p>
<p>Stop requests are propagated to all senders nested in the async scope, but does not prevent those senders adding additional work to the scope. This allows senders to respond to stop requests by scheduling additional work to perform the necessary cleanup for cancellation.</p>
<p>If either the function passed to <code>let_async_scope</code> throws an exception, or any of the senders associated with the async scope complete with an error, then that exception or error completion is used as the completion of the sender returned from <code>let_async_scope</code>. This applies even if the function passed to <code>let_async_scope</code> returns normally; in that case the return value is discarded in favour of the error return. If multiple errors are raised, one of them is chosen to be used as the completion of the sender returned from <code>let_async_scope</code>; all other errors are discarded.</p>
<p>Given:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a>    <span class="kw">auto</span> scope_sender = just(make_scoped_data()) | let_async_scope([](<span class="kw">auto</span> scope_token,</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a>                                                                           <span class="kw">auto</span>&amp; scoped_data) {</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a>        spawn(just_error(foo{}),scope_token);</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a>        spawn(just_error(bar{}),scope_token);</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a>    });</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a>    this_thread::sync_wait(scope_sender);</span></code></pre></div>
<p>Then the <code>sync_wait</code> will throw either an exception of type <code>foo</code> or an exception of type <code>bar</code>, but it is not specified which.</p>
<p>In order to allow the error propagation, all senders associated with the scope must have a compatible error signature. The default error signature is <code>set_error_t(std::exception_ptr)</code>, and all raised errors are wrapped with <code>AS-EXCEPT-PTR</code> (See [exec.general/8). An explicit error signature can be specified by calling <code>let_async_scope_with_error</code>, in which case errors are <em>not</em> converted unless <code>std::exception_ptr</code> is the only permitted error type.</p>
<p>“Noexcept” error signature scopes:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a>    <span class="kw">auto</span> scope_sender = just(make_scoped_data()) | let_async_scope_with_error&lt;&gt;([](<span class="kw">auto</span> scope_token,</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a>                                                                           <span class="kw">auto</span>&amp; scoped_data) <span class="kw">noexcept</span> {</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a>        spawn(just_error(foo{}),scope_token); <span class="co">// error, sender may fail in &quot;noexcept&quot; scope</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a>    });</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true"></a></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true"></a>    this_thread::sync_wait(scope_sender);</span></code></pre></div>
<p>“Default” error signature scopes:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true"></a>    <span class="kw">auto</span> scope_sender = just(make_scoped_data()) | let_async_scope([](<span class="kw">auto</span> scope_token,</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true"></a>                                                                           <span class="kw">auto</span>&amp; scoped_data) <span class="kw">noexcept</span>(<span class="kw">false</span>) {</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true"></a>        spawn(just_error(foo{}),scope_token); <span class="co">// Coerced with AS-EXCEPT-PTR</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true"></a>    });</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true"></a>    this_thread::sync_wait(scope_sender); <span class="co">// throws foo{}</span></span></code></pre></div>
<p>“Explicit error signature” scopes:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true"></a>    <span class="kw">auto</span> scope_sender = just(make_scoped_data()) |</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true"></a>                                  let_async_scope_with_error&lt;foo,bar&gt;(</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true"></a>        [](<span class="kw">auto</span> scope_token, <span class="kw">auto</span>&amp; scoped_data) <span class="kw">noexcept</span> {</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true"></a>        spawn(just_error(foo{}),scope_token); <span class="co">// OK</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true"></a>        spawn(just_error(bar{}),scope_token); <span class="co">// OK</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true"></a>        spawn(just_error(baz{}),scope_token); <span class="co">// error</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true"></a>    });</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true"></a></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true"></a>    this_thread::sync_wait(scope_sender | upon_error([](<span class="kw">auto</span> e){</span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true"></a>    <span class="kw">static_assert</span>(is_same&lt;<span class="kw">decltype</span>(e),foo&gt; || is_same&lt;<span class="kw">decltype</span>(e),bar&gt;,</span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true"></a>      <span class="st">&quot;Error must be foo or bar&quot;</span>);</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true"></a>    }));</span></code></pre></div>
<h2 id="proposal">Proposal</h2>
<p><code>let_async_scope</code> provides a means of creating an async scope (see <a href="https://isocpp.org/files/papers/P3149R6.html">P3149R6</a>), which is associated with a set of tasks, and ensuring that they are all complete before the async scope sender completes.The previous sender’s result is passed to a user-specified invocable, along with an async scope token, which returns a new sender that is connected and started.</p>
<p>The sender returned by <code>let_async_scope</code> completes with the result of the completion of the sender returned from the supplied invocable. It does not complete until all tasks nested on the <code>scope_token</code> passed to the invocable have completed. Additional tasks may be nested on copies of the <code>scope_token</code>, even if the initial sender returned from the invocable has completed. The returned <code>scope_sender</code> will not complete while there are any nested tasks that have not completed.</p>
<p>If the callable supplied to <code>let_async_scope</code> does not return a sender, it must return <code>void</code>. The sender returned from <code>let_async_scope</code> will then have a <code>void</code> value completion.</p>
<p>Stop requests are propagated to all senders nested in the async scope.</p>
<p>The environment from the receiver connected to the sender returned from <code>let_async_scope</code> is propagated via the internal receiver to all senders spawned using the supplied scope token.</p>
<p>If the callable passed to <code>let_async_scope</code> throws an exception, or any of the senders associated with the scope complete with an error, then a stop request is propagated to all outstanding senders spawned using the scope token.</p>
<p>If either the function passed to <code>let_async_scope</code> throws an exception, or any of the senders associated with the async scope complete with an error, then that exception or error completion is used as the completion of the sender returned from <code>let_async_scope</code>. This applies even if the function passed to <code>let_async_scope</code> returns normally; in that case the return value is discarded in favour of the error return. If multiple errors are raised, one of them is chosen to be used as the completion of the sender returned from <code>let_async_scope</code>; all other errors are discarded.</p>
<p>If the error list specified for <code>let_async_scope_with_error</code> does not specify <code>std::exception_ptr</code>, then the function supplied must be declared <code>noexcept</code>, otherwise the program is ill-formed.</p>
<p>If the possible error completions of senders passed to <code>spawn</code> with a token from <code>let_async_scope_with_error</code> are not compatible with the error signatures then the program is ill-formed.</p>
<h2 id="wording">Wording</h2>
<p>Please note: this wording is incomplete, and needs review.</p>
<h3 id="executionlet_async_scope"><code>execution::let_async_scope</code></h3>
<ol type="1">
<li><p><code>let_async_scope</code> transforms a sender’s value completions into a new child asynchronous operation associated with an async scope, by passing the sender’s result datums to a user-specified callable, which returns a new sender that is connected and started.</p></li>
<li><p>For a subexpression <code>sndr</code>, let <code>let-async-scope-env(sndr)</code> be expression-equivalent to the first well-formed expression below:</p>
<ul>
<li><p><code>SCHED-ENV(get_completion_scheduler&lt;decayed-typeof&lt;set_value&gt;&gt;(get_env(sndr)))</code></p></li>
<li><p><code>MAKE-ENV(get_domain, get_domain(get_env(sndr)))</code></p></li>
<li><p><code>empty_env{}</code></p></li>
</ul></li>
<li><p>The expression <code>let_async_scope(sndr, f)</code> is expression-equivalent to:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true"></a>let_async_scope_with_error&lt;<span class="bu">std::</span>exception_ptr&gt;(sndr, f);</span></code></pre></div></li>
<li><p>The expression <code>let_async_scope_with_error&lt;Errors...&gt;(sndr, f)</code> is expression-equivalent to:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true"></a> transform_sender(</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true"></a>   get-domain-early(sndr),</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a>   make-sender(let_async_scope_with_error&lt;Errors...&gt;, f, sndr));</span></code></pre></div>
<p>Where <code>Errors</code> is the list of possible error completion types.</p></li>
<li><p>The exposition-only class template <code>impls-for</code> ([exec.snd.general]) is specialized for <code>let_async_scope</code> as follows:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true"></a> <span class="kw">namespace</span> <span class="bu">std::</span>execution {</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true"></a>   <span class="kw">template</span>&lt;<span class="kw">class</span> Errors, <span class="kw">class</span> State, <span class="kw">class</span> Rcvr, <span class="kw">class</span>... Args&gt;</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true"></a>   <span class="dt">void</span> let-async-scope-bind(State&amp; state, Rcvr&amp; rcvr, Args&amp;&amp;... args); <span class="co">// exposition only</span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true"></a></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true"></a>   <span class="kw">template</span>&lt;<span class="kw">typename</span> ... Errors&gt;</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true"></a>   <span class="kw">struct</span> impls-<span class="cf">for</span>&lt;decayed-<span class="ex">typeof</span>&lt;let_async_scope_with_error&lt;Errors...&gt;&gt;&gt; : <span class="cf">default</span>-impls {</span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true"></a>     <span class="at">static</span> <span class="kw">constexpr</span> <span class="kw">auto</span> get-state = see below;</span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true"></a>     <span class="at">static</span> <span class="kw">constexpr</span> <span class="kw">auto</span> complete = see below;</span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true"></a>   };</span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true"></a> }</span></code></pre></div>
<ol type="1">
<li><p>Let <code>receiver2</code> denote the following exposition-only class template:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true"></a> <span class="kw">namespace</span> <span class="bu">std::</span>execution {</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true"></a>   <span class="kw">template</span>&lt;<span class="kw">class</span> Rcvr, <span class="kw">class</span> Env&gt;</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true"></a>   <span class="kw">struct</span> receiver2 : Rcvr {</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true"></a>     <span class="kw">explicit</span> receiver2(Rcvr rcvr, Env env)</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true"></a>       : Rcvr(<span class="bu">std::</span>move(rcvr)), env(<span class="bu">std::</span>move(env)) {}</span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true"></a></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true"></a>     <span class="kw">auto</span> get_env() <span class="at">const</span> <span class="kw">noexcept</span> {</span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true"></a>       <span class="at">const</span> Rcvr&amp; rcvr = *<span class="kw">this</span>;</span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true"></a>       <span class="cf">return</span> JOIN-ENV(env, FWD-ENV(execution::get_env(rcvr)));</span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true"></a>     }</span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true"></a></span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true"></a>     Env env; <span class="co">// exposition only</span></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true"></a>   };</span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true"></a> }</span></code></pre></div></li>
<li><p><code>impls-for&lt;decayed-typeof&lt;let_async_scope_with_error&lt;Errors...&gt;&gt;&gt;::get-state</code> is is initialized with a callable object equivalent to the following:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true"></a> []&lt;<span class="kw">class</span> Sndr, <span class="kw">class</span> Rcvr&gt;(Sndr&amp;&amp; sndr, Rcvr&amp; rcvr) <span class="kw">requires</span> see below {</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true"></a>   <span class="kw">auto</span>&amp;&amp; [tag, data, child] = <span class="bu">std::</span>forward&lt;Sndr&gt;(sndr);</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true"></a>   <span class="cf">return</span> [&amp;]&lt;<span class="kw">class</span> Fn, <span class="kw">class</span> Env&gt;(Fn fn, Env env) {</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true"></a>     <span class="kw">using</span> args-variant-type = see below;</span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true"></a>     <span class="kw">using</span> ops2-variant-type = see below;</span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true"></a>     <span class="kw">using</span> scope-type = see below;</span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true"></a></span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true"></a>     <span class="kw">struct</span> state-type {</span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true"></a>       Fn fn;</span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true"></a>       Env env;</span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true"></a>       scope-type scope;</span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true"></a>       args-variant-type args;</span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true"></a>       ops2-variant-type ops2;</span>
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true"></a>       mutex error_mutex;</span>
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true"></a>       optional&lt;error-variant-type&gt; error;</span>
<span id="cb11-16"><a href="#cb11-16" aria-hidden="true"></a>     };</span>
<span id="cb11-17"><a href="#cb11-17" aria-hidden="true"></a>     <span class="cf">return</span> state-type{<span class="bu">std::</span>move(fn), <span class="bu">std::</span>move(env), {}, {}};</span>
<span id="cb11-18"><a href="#cb11-18" aria-hidden="true"></a>   }(<span class="bu">std::</span>forward_like&lt;Sndr&gt;(data), let-async-scope-env(child));</span>
<span id="cb11-19"><a href="#cb11-19" aria-hidden="true"></a> }</span></code></pre></div>
<ol type="1">
<li><p><code>scope-type</code> is a type that satisfies the <code>asynchronous-scope</code> concept.</p>
<ol type="1">
<li>Instances of <code>scope-type</code> maintains a count of the number of active senders passed to <code>nest</code> on that scope object or an <code>async-scope-token</code> obtained from that scope object.</li>
<li>The count of active senders is decremented by one when a sender passed to <code>nest</code> completes.</li>
<li>The sender returned from calling <code>join()</code> on an instance of the scope type will complete when the number of senders reaches zero.</li>
<li>Once the count of active senders has been decremented to zero, it is undefined behaviour to attempt to nest a sender into the scope. [[Note: this requires storing a scope token passed to a nested sender in storage that outlives that sender]]</li>
</ol></li>
<li><p><code>scope-token-type</code> is the type of the <code>async-scope-token</code> associated with this invocation of <code>let_async_scope_with_error</code>.</p></li>
<li><p>Let <code>Sigs</code> be a pack of the arguments to the <code>completion_signatures</code> specialization named by <code>completion_signatures_of_t&lt;child-type&lt;Sndr&gt;,       env_of_t&lt;Rcvr&gt;&gt;</code>. Let <code>LetSigs</code> be a pack of those types in <code>Sigs</code> with a return type of <code>decayed-typeof&lt;set_value&gt;</code>. Let <code>as-tuple</code> be an alias template such that <code>as-tuple&lt;Tag(Args...)&gt;</code> denotes the type <code>decayed-tuple&lt;Args...&gt;</code>. Then <code>args-variant-type</code> denotes the type <code>variant&lt;monostate,       as-tuple&lt;LetSigs&gt;...&gt;</code>.</p></li>
<li><p>Let <code>as-sndr2</code> be an alias template such that <code>as-sndr2&lt;Tag(Args...)&gt;</code> denotes the type <code>call-result-t&lt;Fn, scope-token-type, remove_cvref_t&lt;Args&gt;&amp;...&gt;</code>. Then <code>ops2-variant-type</code> denotes the type <code>variant&lt;monostate,       connect_result_t&lt;as-sndr2&lt;LetSigs&gt;, receiver2&lt;Rcvr,       Env&gt;&gt;...&gt;</code>.</p></li>
<li><p>The <em>requires-clause</em> constraining the above lambda is satisfied if and only if the types <code>args-variant-type</code> and <code>ops2-variant-type</code> are well-formed.</p></li>
<li><p><code>error-variant-type</code> is a <code>variant&lt;E...&gt;</code>, where the types <code>E...</code> are the corresponding <code>E</code> types from the <code>Errors...</code> parameter of the <code>let_async_scope_with_error&lt;Errors...&gt;</code> invocation.</p></li>
<li><p><code>scope-token-type</code> shall be a unique type, such that invoking <code>spawn(snd, token, env)</code> where <code>token</code> is an instance of <code>scope-token-type</code> invokes a distinct overload of <code>spawn</code>. Such an invocation is ill-formed if the completion signatures of <code>snd</code> include error completions that are not compatible with the <code>Errors...</code> list of the <code>let_async_scope_with_error&lt;Errors...&gt;</code> invocation. If the error list is compatible, then such an invocation of <code>spawn</code> is equivalent to</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true"></a>spawn(snd | upon_error(</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true"></a>        [&amp;state](<span class="kw">auto</span>&amp;&amp; error){</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true"></a>          {</span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true"></a>            lock_guard guard(state.error_mutex);</span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true"></a>            state.errors.emplace(TRANSFORM-ERROR(error));</span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true"></a>          }</span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true"></a>          state.scope.request_stop();</span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true"></a>                  }), state.scope.get_token(), env);</span></code></pre></div>
<p>Where <code>TRANSFORM-ERROR</code> is <code>AS-EXCEPT-PTR(error)</code> if <code>Errors...</code> is <code>std::exception_ptr</code>, and <code>std::forward&lt;decltype(error)&gt;(error)</code> otherwise.</p></li>
</ol></li>
<li><p>The exposition-only function template <code>let-async-scope-bind</code> is equal to:</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true"></a><span class="kw">auto</span>&amp; args = state.args.emplace&lt;decayed-tuple&lt;scope-token-type, Args...&gt;&gt;(</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true"></a>        create-scope-token(), <span class="bu">std::</span>forward&lt;Args&gt;(args)...);</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true"></a><span class="cf">try</span> {</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true"></a>    <span class="kw">auto</span> sndr2 = nest(apply(<span class="bu">std::</span>move(state.fn), args), state.scope);</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true"></a>    <span class="kw">auto</span> join_sender = state.scope.join();</span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true"></a>    <span class="kw">auto</span> result_sender = when_all_with_variant(<span class="bu">std::</span>move(sndr2), <span class="bu">std::</span>move(join_sender)) |</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true"></a>                         then([](<span class="kw">auto</span>&amp; result, <span class="kw">auto</span>&amp;) { <span class="cf">return</span> result; });</span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true"></a>    <span class="kw">auto</span> rcvr2 = receiver2{<span class="bu">std::</span>move(rcvr), <span class="bu">std::</span>move(state.env)};</span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true"></a>    <span class="kw">auto</span> mkop2 = [&amp;] { <span class="cf">return</span> <span class="fu">connect</span>(<span class="bu">std::</span>move(result_sender), <span class="bu">std::</span>move(rcvr2)); };</span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true"></a>    <span class="kw">auto</span>&amp; op2 = state.ops2.emplace&lt;<span class="kw">decltype</span>(mkop2())&gt;(emplace-from{mkop2});</span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true"></a>    start(op2);</span>
<span id="cb13-12"><a href="#cb13-12" aria-hidden="true"></a>} <span class="cf">catch</span> (...) {</span>
<span id="cb13-13"><a href="#cb13-13" aria-hidden="true"></a>    state.scope.request_stop();</span>
<span id="cb13-14"><a href="#cb13-14" aria-hidden="true"></a>    <span class="kw">auto</span> result_sender = when_all(just_error(<span class="bu">std::</span>current_exception()), state.scope.join());</span>
<span id="cb13-15"><a href="#cb13-15" aria-hidden="true"></a>    <span class="kw">auto</span> rcvr2 = receiver2{<span class="bu">std::</span>move(rcvr), <span class="bu">std::</span>move(state.env)};</span>
<span id="cb13-16"><a href="#cb13-16" aria-hidden="true"></a>    <span class="kw">auto</span> mkop2 = [&amp;] { <span class="cf">return</span> <span class="fu">connect</span>(<span class="bu">std::</span>move(result_sender), <span class="bu">std::</span>move(rcvr2)); };</span>
<span id="cb13-17"><a href="#cb13-17" aria-hidden="true"></a>    <span class="kw">auto</span>&amp; op2 = state.ops2.emplace&lt;<span class="kw">decltype</span>(mkop2())&gt;(emplace-from{mkop2});</span>
<span id="cb13-18"><a href="#cb13-18" aria-hidden="true"></a>    start(op2);</span>
<span id="cb13-19"><a href="#cb13-19" aria-hidden="true"></a>}</span></code></pre></div>
<p>where <code>create-scope-token()</code> creates an instance of the <code>scope-token-type</code> associated with the <code>state</code> for this invocation of <code>let_async_scope_with_error</code>.</p></li>
<li><p><code>impls-for&lt;decayed-typeof&lt;let_async_scope_with_error&lt;Errors...&gt;&gt;&gt;::complete</code> is is initialized with a callable object equivalent to the following:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true"></a>[]&lt;<span class="kw">class</span> Tag, <span class="kw">class</span>... Args&gt;</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true"></a>  (<span class="kw">auto</span>, <span class="kw">auto</span>&amp; state, <span class="kw">auto</span>&amp; rcvr, Tag, Args&amp;&amp;... args) <span class="kw">noexcept</span> -&gt; <span class="dt">void</span> {</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true"></a>    <span class="cf">if</span> <span class="kw">constexpr</span> (same_as&lt;Tag, decayed-<span class="ex">typeof</span>&lt;set_value&gt;&gt;) {</span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true"></a>      TRY-EVAL(<span class="bu">std::</span>move(rcvr), let-async-scope-bind(state, rcvr, <span class="bu">std::</span>forward&lt;Args&gt;(args)...));</span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true"></a>    } <span class="cf">else</span> {</span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true"></a>      Tag()(<span class="bu">std::</span>move(rcvr), <span class="bu">std::</span>forward&lt;Args&gt;(args)...);</span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true"></a>    }</span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true"></a>  }</span></code></pre></div></li>
</ol></li>
<li><p>Let <code>sndr</code> and <code>env</code> be subexpressions, and let <code>Sndr</code> be <code>decltype((sndr))</code>. If <code>sender-for&lt;Sndr,    decayed-typeof&lt;let_async_scope_with_error&lt;Errors...&gt;&gt;&gt;</code> is <code>false</code>, then the expression <code>let_async_scope_with_error&lt;Errors...&gt;.transform_env(sndr, env)</code> is ill-formed. Otherwise, it is equal to <code>JOIN-ENV(let-env(sndr),    FWD-ENV(env))</code>.</p></li>
<li><p>Let the subexpression <code>out_sndr</code> denote the result of the invocation <code>let_async_scope_with_error&lt;Errors...&gt;(sndr, f)</code> or an object copied or moved from such, and let the subexpression <code>rcvr</code> denote a receiver such that the expression <code>connect(out_sndr, rcvr)</code> is well-formed. The expression <code>connect(out_sndr, rcvr)</code> has undefined behavior unless it creates an asynchronous operation ([async.ops]) that, when started:</p>
<ul>
<li><p>invokes <code>f</code> when <code>set_value</code> is called with <code>sndr</code>’s result datums,</p></li>
<li><p>makes its completion dependent on the completion of a sender returned by <code>f</code>, and</p></li>
<li><p>propagates the other completion operations sent by <code>sndr</code>.</p></li>
</ul></li>
</ol>
<h2 id="revision-history">Revision History</h2>
<h3 id="r1">R1</h3>
<ul>
<li>Improved examples, added links</li>
</ul>
<h3 id="r2">R2</h3>
<ul>
<li>Propagate environment to spawned senders associated with the scope.</li>
<li>Errors lead to stop requests being sent to all senders associated with the scope.</li>
<li>An Error is stored in the scope and the scope sender completes with an error if there is one</li>
<li>Allowed errors can be specified</li>
</ul>
<h3 id="r3">R3</h3>
<ul>
<li>Fix specification of allowed errors</li>
<li>Make it clear that a new overload of <code>spawn</code> is expected.</li>
</ul>
<h2 id="acknowledgements">Acknowledgements</h2>
<p>Thanks to Ian Petersen, Lewis Baker, Inbal Levi, Kirk Shoop, Eric Niebler, Ruslan Arutyunyan, Maikel Nadolski, Lucian Radu Teodorescu, Robert Leahy, Dmitry Prokoptsev, and everyone else who contributed to discussions leading to this paper, and commented on early drafts.</p>

</body>
</html>
