Skip to content

Commit b4de0c7

Browse files
committed
stdexec::when_all: Hang After Stop Request
std::execution::when_all forwards stop requests through to its children via the exposition-only class on-stop-request, defined as follows (see [exec.snd.general]): struct on-stop-request { inplace_stop_source& stop-src; // exposition only void operator()() noexcept { stop-src.request_stop(); } }; stdexec::when_all used to be conforming in this regard, however this changed in 36a92fd. That commit not only complicates the stop callback used by stdexec::when_all but also causes the __stopped state to be reachable even when no child is capable of sending a stopped completion signal. The change to std::execution:: when_all introduced by P3887 (see 712953b) depends on the __stopped state being impossible unless a child sends a stopped completion signal. This tension meant that 36a92fd caused stdexec::when_all to not complete (thereby leading to hangs) when: - No child potentially sent a stopped completion signal, and - Stop was requested 712953b intended to include a unit test preventing the above-described regression but due to a typo (corrected in this commit) that unit test failed in this regard. Note that 36a92fd is a misdiagnosis and should eventually be reverted (along with this commit save the above- mentioned unit test correction). The issue that 36a92fd aims to address has nothing to do with stdexec::when_all and is instead caused by the fact that stdexec::inplace_stop_source is not well-behaved when the lifetime of the stop source ends during the dispatch of the final stop callback. This issue should be fixed separately and this commit is only intended to workaround the issue in the meantime.
1 parent aae4d02 commit b4de0c7

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎include/stdexec/__detail/__when_all.hpp‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ namespace STDEXEC {
285285
__on_stop_.reset();
286286
// All child operations have completed and arrived at the barrier.
287287
switch (__state_.load(__std::memory_order_relaxed)) {
288+
case __stopped:
289+
if constexpr (_SendsStopped) {
290+
STDEXEC::set_stopped(static_cast<_Receiver&&>(__rcvr_));
291+
break;
292+
}
293+
// This is reachable because the stop callback sets __stopped whether
294+
// or not any child can send set_stopped, therefore we handle this the
295+
// same as __started
296+
[[fallthrough]];
288297
case __started:
289298
if constexpr (!__std::same_as<_ValuesTuple, __ignore>) {
290299
// All child operations completed successfully:
@@ -298,13 +307,6 @@ namespace STDEXEC {
298307
__mk_completion_fn(set_error, __rcvr_), static_cast<_ErrorsVariant&&>(__errors_));
299308
}
300309
break;
301-
case __stopped:
302-
if constexpr (_SendsStopped) {
303-
STDEXEC::set_stopped(static_cast<_Receiver&&>(__rcvr_));
304-
} else {
305-
STDEXEC_UNREACHABLE();
306-
}
307-
break;
308310
default:;
309311
}
310312
}
@@ -489,23 +491,19 @@ namespace STDEXEC {
489491
__state.__stop_source_.request_stop();
490492
}
491493
} else if constexpr (!__same_as<_ValuesTuple, __ignore>) {
492-
// We only need to bother recording the completion values
493-
// if we're not already in the "error" or "stopped" state.
494-
if (__state.__state_.load() == __started) {
495-
auto& __opt_values = STDEXEC::__get<_Index::value>(__state.__values_);
496-
using _Tuple = __decayed_tuple<_Args...>;
497-
static_assert(
498-
__same_as<decltype(*__opt_values), _Tuple&>,
499-
"One of the senders in this when_all() is fibbing about what types it sends");
500-
if constexpr ((__nothrow_decay_copyable<_Args> && ...)) {
494+
auto& __opt_values = STDEXEC::__get<_Index::value>(__state.__values_);
495+
using _Tuple = __decayed_tuple<_Args...>;
496+
static_assert(
497+
__same_as<decltype(*__opt_values), _Tuple&>,
498+
"One of the senders in this when_all() is fibbing about what types it sends");
499+
if constexpr ((__nothrow_decay_copyable<_Args> && ...)) {
500+
__opt_values.emplace(_Tuple{static_cast<_Args&&>(__args)...});
501+
} else {
502+
STDEXEC_TRY {
501503
__opt_values.emplace(_Tuple{static_cast<_Args&&>(__args)...});
502-
} else {
503-
STDEXEC_TRY {
504-
__opt_values.emplace(_Tuple{static_cast<_Args&&>(__args)...});
505-
}
506-
STDEXEC_CATCH_ALL {
507-
__set_error(__state, std::current_exception());
508-
}
504+
}
505+
STDEXEC_CATCH_ALL {
506+
__set_error(__state, std::current_exception());
509507
}
510508
}
511509
}

‎test/stdexec/algos/adaptors/test_when_all.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ namespace {
404404
ex::completion_signatures_of_t<decltype(snd), decltype(env)>,
405405
ex::completion_signatures<ex::set_value_t()>
406406
>);
407-
auto op = ex::connect(snd, expect_void_receiver{});
407+
auto op = ex::connect(snd, expect_void_receiver(env));
408408
ex::start(op);
409409
}
410410

0 commit comments

Comments
 (0)