From 3570ff738ca18b379cd8f67384f69968ea5e72f7 Mon Sep 17 00:00:00 2001 From: Lucian Radu Teodorescu Date: Wed, 14 Aug 2024 16:38:18 +0300 Subject: [PATCH] Fix race condition that would make the system context tests fail occasionally. --- .../__detail/__system_context_default_impl.hpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/include/exec/__detail/__system_context_default_impl.hpp b/include/exec/__detail/__system_context_default_impl.hpp index 5e31e3443..5e9b88634 100644 --- a/include/exec/__detail/__system_context_default_impl.hpp +++ b/include/exec/__detail/__system_context_default_impl.hpp @@ -45,20 +45,26 @@ namespace exec::__system_context_default_impl { void set_value() noexcept { auto __op = __op_; - __r_->set_value(); - __op->__destruct(); + auto __r = __r_; + __op->__destruct(); // destroys the operation, including `this`. + __r->set_value(); + // Note: when calling a completion signal, the parent operation might complete, making the + // static storage passed to this operation invalid. Thus, we need to ensure that we are not + // using the operation state after the completion signal. } void set_error(std::exception_ptr __ptr) noexcept { auto __op = __op_; - __r_->set_error(__ptr); - __op->__destruct(); + auto __r = __r_; + __op->__destruct(); // destroys the operation, including `this`. + __r->set_error(__ptr); } void set_stopped() noexcept { auto __op = __op_; - __r_->set_stopped(); - __op->__destruct(); + auto __r = __r_; + __op->__destruct(); // destroys the operation, including `this`. + __r->set_stopped(); } };