<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4215: run_loop::finish should be noexcept</title>
<meta property="og:title" content="Issue 4215: run_loop::finish should be noexcept">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4215.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="4215"><a href="lwg-active.html#4215">4215</a>. <code class='backtick'>run_loop::finish</code> should be <code class='backtick'>noexcept</code></h3>
<p><b>Section:</b> 33.12.1 <a href="https://wg21.link/exec.run.loop">[exec.run.loop]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Eric Niebler <b>Opened:</b> 2025-02-13 <b>Last modified:</b> 2025-06-13</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Imported from <a href="https://github.com/cplusplus/sender-receiver/issues/329">cplusplus/sender-receiver #329</a>.
</p>
<p>
<code class='backtick'>run_loop::finish</code> puts the <code class='backtick'>run_loop</code> into the <code><i>finishing</i></code> state so that the next 
time the work queue is empty, <code class='backtick'>run_loop::run</code> will return instead of waiting for more work.
<p/>
Calling <code class='backtick'>.finish()</code> on a <code class='backtick'>run_loop</code> instance can potentially throw (<code class='backtick'>finish()</code> is not marked <code class='backtick'>noexcept</code>), 
that is because one valid implementation involves acquiring a lock on a <code class='backtick'>std::mutex</code> &mdash; a potentially throwing operation.
<p/>
But failing to put the <code class='backtick'>run_loop</code> into the <code><i>finishing</i></code> state is problematic in the same way 
that a failing destructor is problematic: shutdown and clean-up code depends on it succeeding.
<p/>
Consider <code class='backtick'>sync_wait</code>'s use of <code class='backtick'>run_loop</code>:
</p>
<blockquote><pre>
<i>sync-wait-state</i>&lt;Sndr&gt; state;
auto op = connect(sndr, <i>sync-wait-receiver</i>&lt;Sndr&gt;{&amp;state});
start(op);

state.loop.run();
if (state.error) {
  rethrow_exception(std::move(state.error));
}
return std::move(state.result);
</pre></blockquote>
<p>
It is the job of <code><i>sync-wait-receiver</i></code> to put the <code class='backtick'>run_loop</code> into the <code><i>finishing</i></code> state 
so that the invocation of <code class='backtick'>state.loop.run()</code> will return. It does that in its completion functions, like so:
</p>
<blockquote><pre>
void set_stopped() &amp;&amp; noexcept;
</pre>
<blockquote>
<p>
<i>Effects</i>: Equivalent to <code>state-&gt;loop.finish()</code>.
</p>
</blockquote>
</blockquote>
<p>
Here we are not handling the fact that <code>state-&gt;loop.finish()</code> is potentially throwing. Given that this 
function is <code class='backtick'>noexcept</code>, this will lead to the application getting terminated. Not good.
<p/>
But even if we handle the exception and save it into <code class='backtick'>state.result</code> to be rethrown later, we still have a problem. 
Since <code class='backtick'>run_loop::finish()</code> threw, the <code class='backtick'>run_loop</code> has not been placed into the <code><i>finishing</i></code> state. 
That means that <code class='backtick'>state.loop.run()</code> will never return, and <code class='backtick'>sync_wait</code> will hang forever.
<p/>
Simply put, <code class='backtick'>run_loop::finish()</code> has to be <code class='backtick'>noexcept</code>. The implementation must find a way to put the <code class='backtick'>run_loop</code> 
into the <code><i>finishing</i></code> state. If it cannot, it should terminate. Throwing an exception and foisting the 
problem on the caller &mdash; who has no recourse &mdash; is simply wrong.
</p>

<p><i>[2025-06-13; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll.
</p>
<p>
"If this can call <code class='backtick'>terminate()</code>, we should explicitly say so
(c.f. 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> p11)"
</p>



<p id="res-4215"><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><p>Modify 33.12.1.1 <a href="https://wg21.link/exec.run.loop.general">[exec.run.loop.general]</a> as indicated:</p>

<blockquote><pre>
namespace std::execution {
  class run_loop {
    <i>// 33.12.1.2 <a href="https://wg21.link/exec.run.loop.types">[exec.run.loop.types]</a>, associated types</i>
    class <i>run-loop-scheduler</i>; <i>// exposition only</i>
    class <i>run-loop-sender</i>;    <i>// exposition only</i>
    struct <i>run-loop-opstate-base</i> { <i>// exposition only</i>
      virtual void <i>execute</i>() = 0;  <i>// exposition only</i>
      run_loop* <i>loop</i>;              <i>// exposition only</i>
      run-loop-opstate-base* <i>next</i>; <i>// exposition only</i>
    };
    template&lt;class Rcvr&gt;
      using <i>run-loop-opstate</i> = <i>unspecified</i>; <i>// exposition only</i>

    <i>// 33.12.1.4 <a href="https://wg21.link/exec.run.loop.members">[exec.run.loop.members]</a>, member functions</i>
    <i>run-loop-opstate-base</i>* <i>pop-front</i>(); <i>// exposition only</i>
    void <i>push-back</i>(<i>run-loop-opstate-base</i>*); <i>// exposition only</i>

  public:
    <i>// 33.12.1.3 <a href="https://wg21.link/exec.run.loop.ctor">[exec.run.loop.ctor]</a>, constructor and destructor</i>
    run_loop() noexcept;
    run_loop(run_loop&amp;&amp;) = delete;
    ~run_loop();

    <i>// 33.12.1.4 <a href="https://wg21.link/exec.run.loop.members">[exec.run.loop.members]</a>, member functions</i>
    <i>run-loop-scheduler</i> get_scheduler();
    void run();
    void finish() <ins>noexcept</ins>;
  };
}
</pre></blockquote>
</li>

<li><p>Modify 33.12.1.4 <a href="https://wg21.link/exec.run.loop.members">[exec.run.loop.members]</a> as indicated:</p>

<blockquote>
<pre>
void finish() <ins>noexcept</ins>;
</pre>
<blockquote>
<p>
-8- <i>Preconditions</i>: <code><i>state</i></code> is either <code><i>starting</i></code> or <code><i>running</i></code>.
<p/>
-9- <i>Effects</i>: Changes <code><i>state</i></code> to <code><i>finishing</i></code>.
<p/>
-10- <i>Synchronization</i>: <code class='backtick'>finish</code> synchronizes with the <code><i>pop-front</i></code> operation that returns <code>nullptr</code>.
</p>
</blockquote>
</blockquote>
</li>

</ol>






</body>
</html>
