From f0d17db6f0a39fd4307017e51a93aaae53b0e458 Mon Sep 17 00:00:00 2001 From: Waterpine Date: Wed, 25 Nov 2026 21:55:02 -0600 Subject: [PATCH 1/2] Keep spawn_future state alive during reentrant cancellation --- include/stdexec/__detail/__spawn_future.hpp | 7 + test/exec/CMakeLists.txt | 1 + .../test_spawn_future_reentrant_cancel.cpp | 142 ++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 test/exec/test_spawn_future_reentrant_cancel.cpp diff --git a/include/stdexec/__detail/__spawn_future.hpp b/include/stdexec/__detail/__spawn_future.hpp index 30f6d4c70..0e2849957 100644 --- a/include/stdexec/__detail/__spawn_future.hpp +++ b/include/stdexec/__detail/__spawn_future.hpp @@ -389,6 +389,9 @@ namespace STDEXEC // 4. any other value means __consume has "registered" its receiver to be // completed by __complete when it is invoked __std::atomic __registered_receiver_{nullptr}; + // The protocol owns one reference; an executing cancellation callback + // temporarily owns another because request_stop can complete inline. + __std::atomic __lifetime_refs_{1}; // Type-erased completion callback. // // The void* will receive the address of the receiver, which will need to @@ -402,6 +405,8 @@ namespace STDEXEC void __destroy() noexcept { + if (__lifetime_refs_.fetch_sub(1, __std::memory_order_acq_rel) != 1) + return; [[maybe_unused]] auto __assoc = std::move(__assoc_); @@ -492,6 +497,8 @@ namespace STDEXEC static void __do_try_cancel(__try_cancelable* __base_ptr) noexcept { auto* __self = static_cast<__spawn_future_state*>(__base_ptr); + __self->__lifetime_refs_.fetch_add(1, __std::memory_order_relaxed); + __scope_guard __keep_alive{[__self]() noexcept { __self->__destroy(); }}; // Consider: there's a sense in which we only need to invoke // request_stop if we've arrived here before the producer has diff --git a/test/exec/CMakeLists.txt b/test/exec/CMakeLists.txt index d83ea9c96..ea520d7bd 100644 --- a/test/exec/CMakeLists.txt +++ b/test/exec/CMakeLists.txt @@ -16,6 +16,7 @@ set(exec_test_sources ../test_main.cpp + test_spawn_future_reentrant_cancel.cpp test_bwos_lifo_queue.cpp test_atomic_intrusive_queue.cpp test_any_sender.cpp diff --git a/test/exec/test_spawn_future_reentrant_cancel.cpp b/test/exec/test_spawn_future_reentrant_cancel.cpp new file mode 100644 index 000000000..dd1751748 --- /dev/null +++ b/test/exec/test_spawn_future_reentrant_cancel.cpp @@ -0,0 +1,142 @@ +/* + * Licensed under the Apache License, Version 2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + */ + +#include +#include +#include +#include +#include +#include + +namespace regression_spawn_future_reentrant_cancel +{ + // Source shape follows test/stdexec/algos/adaptors/test_spawn_future.cpp's + // never sender, but connects/starts the future before requesting cancellation. + + struct control + { + bool complete_inline = true; + bool stop_seen = false; + void* operation{}; + void (*complete)(void*){}; + }; + struct source + { + using sender_concept = stdexec::sender_tag; + using completion_signatures = stdexec::completion_signatures; + control* state; + template + struct operation + { + R receiver; + control* state; + struct callback + { + operation* self; + void operator()() const noexcept + { + auto* saved = self; + saved->state->stop_seen = true; + if (saved->state->complete_inline) + saved->finish(); + } + }; + using token = stdexec::stop_token_of_t>; + using cb = stdexec::stop_callback_for_t; + std::optional registration; + operation(R r, control* c) + : receiver(std::move(r)) + , state(c) + {} + void finish() noexcept + { + registration.reset(); + stdexec::set_stopped(std::move(receiver)); + } + void start() & noexcept + { + state->operation = this; + state->complete = [](void* p) + { + static_cast(p)->finish(); + }; + registration.emplace(stdexec::get_stop_token(stdexec::get_env(receiver)), callback{this}); + } + }; + template + auto connect(R r) const + { + return operation{std::move(r), state}; + } + }; + struct receiver + { + using receiver_concept = stdexec::receiver_tag; + stdexec::inplace_stop_token token; + int* completed; + auto get_env() const noexcept + { + return stdexec::prop{stdexec::get_stop_token, token}; + } + void set_stopped() noexcept + { + ++*completed; + std::fputs("future receiver stopped\n", stderr); + } + void set_error(std::exception_ptr) noexcept + { + std::abort(); + } + }; + int run_case(int argc, char** argv) + { + auto mode = argc > 1 ? std::string_view(argv[1]) : std::string_view("inline"); + stdexec::simple_counting_scope scope; + stdexec::inplace_stop_source stop; + control state; + state.complete_inline = mode != "deferred"; + int completed = 0; + auto future = stdexec::spawn_future(source{&state}, scope.get_token()); + if (mode == "pre-stopped") + stop.request_stop(); + auto op = stdexec::connect(std::move(future), receiver{stop.get_token(), &completed}); + stdexec::start(op); + std::fputs("requesting consumer stop\n", stderr); + stop.request_stop(); + if (mode == "deferred") + state.complete(state.operation); + stdexec::sync_wait(scope.join()); + std::printf("PASS mode=%.*s completions=%d source_stopped=%d\n", + int(mode.size()), + mode.data(), + completed, + state.stop_seen); + return completed == 1 && state.stop_seen ? 0 : 2; + } +} // namespace regression_spawn_future_reentrant_cancel + +TEST_CASE("spawn_future_reentrant_cancel: inline", "[regression][spawn-future-cancel]") +{ + char program[] = "regression"; + char arg0[] = "inline"; + char* argv[] = {program, arg0}; + REQUIRE(regression_spawn_future_reentrant_cancel::run_case(2, argv) == 0); +} + +TEST_CASE("spawn_future_reentrant_cancel: deferred", "[regression][spawn-future-cancel]") +{ + char program[] = "regression"; + char arg0[] = "deferred"; + char* argv[] = {program, arg0}; + REQUIRE(regression_spawn_future_reentrant_cancel::run_case(2, argv) == 0); +} + +TEST_CASE("spawn_future_reentrant_cancel: pre-stopped", "[regression][spawn-future-cancel]") +{ + char program[] = "regression"; + char arg0[] = "pre-stopped"; + char* argv[] = {program, arg0}; + REQUIRE(regression_spawn_future_reentrant_cancel::run_case(2, argv) == 0); +} From 4a8ebbd2353ab412bf66837d6a786e21bc74d7e6 Mon Sep 17 00:00:00 2001 From: Waterpine Date: Wed, 25 Nov 2026 20:53:03 -0800 Subject: [PATCH 2/2] Use STDEXEC:: namespace macro in regression test nvc++ >= 26.3 configures stdexec into std::execution, so the stdexec:: namespace does not exist in the GPU CI builds. Use the STDEXEC macro like the rest of the test suite. Co-Authored-By: Claude Opus 5.5 --- .../test_spawn_future_reentrant_cancel.cpp | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/exec/test_spawn_future_reentrant_cancel.cpp b/test/exec/test_spawn_future_reentrant_cancel.cpp index dd1751748..784001121 100644 --- a/test/exec/test_spawn_future_reentrant_cancel.cpp +++ b/test/exec/test_spawn_future_reentrant_cancel.cpp @@ -24,8 +24,8 @@ namespace regression_spawn_future_reentrant_cancel }; struct source { - using sender_concept = stdexec::sender_tag; - using completion_signatures = stdexec::completion_signatures; + using sender_concept = STDEXEC::sender_tag; + using completion_signatures = STDEXEC::completion_signatures; control* state; template struct operation @@ -43,8 +43,8 @@ namespace regression_spawn_future_reentrant_cancel saved->finish(); } }; - using token = stdexec::stop_token_of_t>; - using cb = stdexec::stop_callback_for_t; + using token = STDEXEC::stop_token_of_t>; + using cb = STDEXEC::stop_callback_for_t; std::optional registration; operation(R r, control* c) : receiver(std::move(r)) @@ -53,7 +53,7 @@ namespace regression_spawn_future_reentrant_cancel void finish() noexcept { registration.reset(); - stdexec::set_stopped(std::move(receiver)); + STDEXEC::set_stopped(std::move(receiver)); } void start() & noexcept { @@ -62,7 +62,7 @@ namespace regression_spawn_future_reentrant_cancel { static_cast(p)->finish(); }; - registration.emplace(stdexec::get_stop_token(stdexec::get_env(receiver)), callback{this}); + registration.emplace(STDEXEC::get_stop_token(STDEXEC::get_env(receiver)), callback{this}); } }; template @@ -73,12 +73,12 @@ namespace regression_spawn_future_reentrant_cancel }; struct receiver { - using receiver_concept = stdexec::receiver_tag; - stdexec::inplace_stop_token token; + using receiver_concept = STDEXEC::receiver_tag; + STDEXEC::inplace_stop_token token; int* completed; auto get_env() const noexcept { - return stdexec::prop{stdexec::get_stop_token, token}; + return STDEXEC::prop{STDEXEC::get_stop_token, token}; } void set_stopped() noexcept { @@ -93,21 +93,21 @@ namespace regression_spawn_future_reentrant_cancel int run_case(int argc, char** argv) { auto mode = argc > 1 ? std::string_view(argv[1]) : std::string_view("inline"); - stdexec::simple_counting_scope scope; - stdexec::inplace_stop_source stop; + STDEXEC::simple_counting_scope scope; + STDEXEC::inplace_stop_source stop; control state; state.complete_inline = mode != "deferred"; int completed = 0; - auto future = stdexec::spawn_future(source{&state}, scope.get_token()); + auto future = STDEXEC::spawn_future(source{&state}, scope.get_token()); if (mode == "pre-stopped") stop.request_stop(); - auto op = stdexec::connect(std::move(future), receiver{stop.get_token(), &completed}); - stdexec::start(op); + auto op = STDEXEC::connect(std::move(future), receiver{stop.get_token(), &completed}); + STDEXEC::start(op); std::fputs("requesting consumer stop\n", stderr); stop.request_stop(); if (mode == "deferred") state.complete(state.operation); - stdexec::sync_wait(scope.join()); + STDEXEC::sync_wait(scope.join()); std::printf("PASS mode=%.*s completions=%d source_stopped=%d\n", int(mode.size()), mode.data(),