<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4198: schedule_from isn't starting the schedule sender if decay-copying results throws</title>
<meta property="og:title" content="Issue 4198: schedule_from isn't starting the schedule sender if decay-copying results throws">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4198.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#WP">WP</a> status.</em></p>
<h3 id="4198"><a href="lwg-defects.html#4198">4198</a>. <code class='backtick'>schedule_from</code> isn't starting the schedule sender if decay-copying results throws</h3>
<p><b>Section:</b> 33.9.12.7 <a href="https://wg21.link/exec.schedule.from">[exec.schedule.from]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Eric Niebler <b>Opened:</b> 2025-02-03 <b>Last modified:</b> 2025-06-23</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#exec.schedule.from">issues</a> in [exec.schedule.from].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Imported from <a href="https://github.com/cplusplus/sender-receiver/issues/304">cplusplus/sender-receiver #304</a>.
</p>
<p>
33.9.12.7 <a href="https://wg21.link/exec.schedule.from">[exec.schedule.from]</a>/p11 specifies <code class='backtick'>schedule_from</code>'s completion operation as follows:

<pre><code>
[]&lt;class Tag, class... Args&gt;(auto, auto&amp; state, auto&amp; rcvr, Tag, Args&amp;&amp;... args) noexcept
    -> void {
  using result_t = <i>decayed-tuple</i>&lt;Tag, Args...&gt;;
  constexpr bool nothrow = is_nothrow_constructible_v&lt;result_t, Tag, Args...&gt;;

  try {
    state.<i>async-result</i>.template emplace&lt;result_t&gt;(Tag(), std::forward&lt;Args&gt;(args)...);
  } catch (...) {
    if constexpr (!nothrow) {
      set_error(std::move(rcvr), current_exception());
      return;
    }
  }
  start(state.<i>op-state</i>);
};
</code></pre>

so if emplacing the result tuple throws, <code class='backtick'>set_error</code> is immediately called on the downstream receiver. no attempt is made to post the completion to the specified scheduler. this is probably not the right behavior.
</p>
<p><b>Suggested resolution</b></p>
<p>
The right thing, i think, is to catch the exception, emplace the <code class='backtick'>exception_ptr</code> into the <i><code class='backtick'>async-result</code></i> variant, and then start the schedule operation, as shown below:

<pre><code>
[]&lt;class Tag, class... Args&gt;(auto, auto&amp; state, auto&amp; rcvr, Tag, Args&amp;&amp;... args) noexcept
    -> void {
  using result_t = <i>decayed-tuple</i>&lt;Tag, Args...&gt;;
  constexpr bool nothrow = is_nothrow_constructible_v&lt;result_t, Tag, Args...&gt;;

  try {
    state.<i>async-result</i>.template emplace&lt;result_t&gt;(Tag(), std::forward&lt;Args&gt;(args)...);
  } catch(...) {
    if constexpr (!nothrow)
      state.<i>async-result</i>.template emplace&lt;tuple&lt;set_error_t, exception_ptr&gt;&gt;(set_error, current_exception());
  }

  start(state.<i>op-state</i>);
}
</code></pre>

we also need to change how we specify the variant type of
<code>state.<i>async-result</i></code>:

<blockquote>
Let <code class='backtick'>Sigs</code> be a pack of the arguments to the <code class='backtick'>completion_signatures</code>
specialization named by
<code>completion_signatures_of_t&lt;<i>child-type</i>&lt;Sndr&gt;, env_of_t&lt;Rcvr&gt;&gt;</code>.
Let <i><code>as-tuple</code></i> be an alias template
<del>
that transforms a completion signature <code class='backtick'>Tag(Args...)</code> into
the tuple specialization <code><i>decayed-tuple</i>&lt;Tag, Args...&gt;</code>.
</del>
<ins>
such that <code><i>as-tuple</i>&lt;Tag(Args...)&gt;</code> denotes
the tuple specialization <code><i>decayed-tuple</i>&lt;Tag, Args...&gt;</code>,
and let <code><i>is-nothrow-decay-copy-sig</i></code>
be a variable template such that
<code><i>is-nothrow-decay-copy-sig</i>&lt;Tag(Args...)&gt;</code>
is a core constant expression of type <code class='backtick'>bool const</code> and whose value
is <code class='backtick'>true</code> if the types <code class='backtick'>Args...</code> are all nothrow decay-copyable, and <code class='backtick'>false</code>
otherwise.
Let <i><code>error-completion</code></i> be a pack consisting of the type
<code class='backtick'>set_error_t(exception_ptr)</code> if
<code>(<i>is-nothrow-decay-copy-sig</i>&lt;Sigs&gt; &amp;&amp;...)</code>
is <code class='backtick'>false</code>, and an empty pack otherwise.
</ins>
Then <code class='backtick'>variant_t</code> denotes the type
<code>variant&lt;monostate, <i>as-tuple</i>&lt;Sigs&gt;...<ins>, <i>error-completion</i>...</ins>&gt;</code>,
except with duplicate types removed.
</blockquote>

</p>

<p><i>[This touches the same text as LWG <a href="lwg-defects.html#4203" title="Constraints on get-state functions are incorrect (Status: WP)">4203</a><sup><a href="https://cplusplus.github.io/LWG/issue4203" title="Latest snapshot">(i)</a></sup>.]</i></p>


<p><i>[2025-02-07; Reflector poll]</i></p>

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

<p><i>[Hagenberg 2025-02-11; LWG]</i></p>

<p>
Direction seems right. Decay-copyable is not a defined term.
</p>

<p><i>[2025-02-12 Tim adds PR]</i></p>

<p>This also corrects the issue that <code class='backtick'>nothrow</code> is currently relying
on the unspecified exception specification of <code class='backtick'>tuple</code>'s constructor.</p>

<p><i>[Hagenberg 2025-02-12; move to Ready]</i></p>


<p><i>[Sofia 2025-06-21; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-4198"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N5001" title=" Working Draft, Programming Languages — C++">N5001</a>.
</p>

<ol>
<li>
Modify 33.9.12.7 <a href="https://wg21.link/exec.schedule.from">[exec.schedule.from]</a> p8 as indicated:
<blockquote>
<p>
-8- Let <code class='backtick'>Sigs</code> be a pack of the arguments to the <code class='backtick'>completion_signatures</code>
specialization named by
<code>completion_signatures_of_t&lt;<i>child-type</i>&lt;Sndr&gt;, env_of_t&lt;Rcvr&gt;&gt;</code>.
Let <i><code>as-tuple</code></i> be an alias template
<del>
that transforms a completion signature <code class='backtick'>Tag(Args...)</code> into
the tuple specialization <code><i>decayed-tuple</i>&lt;Tag, Args...&gt;</code>.
</del>
<ins>
such that <code><i>as-tuple</i>&lt;Tag(Args...)&gt;</code> denotes
the type <code><i>decayed-tuple</i>&lt;Tag, Args...&gt;</code>,
and let <code><i>is-nothrow-decay-copy-sig</i></code>
be a variable template such that
<code>auto(<i>is-nothrow-decay-copy-sig</i>&lt;Tag(Args...)&gt;)</code>
is a constant expression of type <code class='backtick'>bool</code> and equal to 
<code>(is_nothrow_constructible_v&lt;decay_t&lt;Args&gt;, Args&gt; &amp;&amp; ...)</code>.
Let <i><code>error-completion</code></i> be a pack consisting of the type
<code class='backtick'>set_error_t(exception_ptr)</code> if
<code>(<i>is-nothrow-decay-copy-sig</i>&lt;Sigs&gt; &amp;&amp;...)</code>
is <code class='backtick'>false</code>, and an empty pack otherwise.
</ins>
Then <code class='backtick'>variant_t</code> denotes the type
<code>variant&lt;monostate, <i>as-tuple</i>&lt;Sigs&gt;...<ins>, <i>error-completion</i>...</ins>&gt;</code>,
except with duplicate types removed.
</p>
</blockquote>
</li>
<li>
<p>Modify 33.9.12.7 <a href="https://wg21.link/exec.schedule.from">[exec.schedule.from]</a> p11 as indicated:</p>
<blockquote><p>
-11- The member <code><i>impls-for</i>&lt;schedule_from_t&gt;::<i>complete</i></code> 
is initialized with a callable object equivalent to the following lambda:
</p>
<blockquote><pre><code>
[]&lt;class Tag, class... Args&gt;(auto, auto&amp; state, auto&amp; rcvr, Tag, Args&amp;&amp;... args) noexcept
    -&gt; void {
  using result_t = <i>decayed-tuple</i>&lt;Tag, Args...&gt;;
  constexpr bool nothrow = <del>is_nothrow_constructible_v&lt;result_t, Tag, Args...&gt;</del>
                           <ins>(is_nothrow_constructible_v&lt;decay_t&lt;Args&gt;, Args&gt; &amp;&amp; ...)</ins>;

  try {
    state.<i>async-result</i>.template emplace&lt;result_t&gt;(Tag(), std::forward&lt;Args&gt;(args)...);
  } catch (...) {
    if constexpr (!nothrow) <del>{</del>
      <ins>state.<i>async-result</i>.template emplace&lt;tuple&lt;set_error_t, exception_ptr&gt;&gt;(set_error, current_exception());</ins>
      <del>set_error(std::move(rcvr), current_exception());</del>
      <del>return;</del>
    <del>}</del>
  }
  start(state.<i>op-state</i>);
};
</code></pre></blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
