fix(fsm): publish terminal status after exit hooks and join worker before derived teardown

This commit is contained in:
faumaray committed 2026-11-26 17:10:54 +03:00
1 parent ef5312c4b3
commit 40b579d683
2 files changed
+43 -35

No files matched your search

+36 -35
View File
@@ -647,7 +647,7 @@ namespace Void::Fsm
if (opts.stop.stop_requested()) {
m_termination_reason.store(TerminationReason::cancelled,
std::memory_order_release);
fault("cancelled before start");
finish_with_fault("cancelled before start", false);
co_return Void::Result::fail(make_fsm_error(
FsmErrorCode::cancelled,
"Machine::run: cancelled before start"));
@@ -655,7 +655,7 @@ namespace Void::Fsm
if (opts.deadline.has_value() && clk->now() >= *opts.deadline) {
m_termination_reason.store(TerminationReason::timed_out,
std::memory_order_release);
fault("deadline expired before start");
finish_with_fault("deadline expired before start", false);
co_return Void::Result::fail(make_fsm_error(
FsmErrorCode::timeout,
"Machine::run: deadline expired before start"));
@@ -2195,7 +2195,7 @@ namespace Void::Fsm
}
}
void fault(std::string message)
void finish_with_fault(std::string message, bool call_machine_exit)
{
m_system_error = FsmError{std::move(message)};
if (m_termination_reason.load(std::memory_order_acquire) ==
@@ -2205,18 +2205,23 @@ namespace Void::Fsm
}
Void::Log::error("Fsm", "system fault: {}",
m_system_error->message);
m_status.store(Status::Faulted, std::memory_order_release);
reject_pending_checkpoints(make_fsm_error(
FsmErrorCode::checkpoint_rejected,
"machine faulted before checkpoint boundary"));
if (call_machine_exit) {
on_machine_exit();
}
m_parent_stop_link.reset();
// Publish terminal status strictly last so polling/joining
// threads synchronize after all virtual hooks and field writes.
m_status.store(Status::Faulted, std::memory_order_release);
}
Coro::Task<void> drive()
{
const std::type_info* cur = m_initial;
if (cur == nullptr) {
fault("no initial state");
m_parent_stop_link.reset();
finish_with_fault("no initial state", false);
co_return;
}
on_machine_enter();
@@ -2228,11 +2233,11 @@ namespace Void::Fsm
? TerminationReason::timed_out
: TerminationReason::cancelled,
std::memory_order_release);
fault(m_root_timeout.load(std::memory_order_acquire)
? "deadline expired"
: "cancelled");
on_machine_exit();
m_parent_stop_link.reset();
finish_with_fault(
m_root_timeout.load(std::memory_order_acquire)
? "deadline expired"
: "cancelled",
true);
co_return;
}
if (const auto* j =
@@ -2244,9 +2249,8 @@ namespace Void::Fsm
}
const auto it = m_factories.find(std::type_index{*cur});
if (it == m_factories.end()) {
fault("transition to unregistered state");
on_machine_exit();
m_parent_stop_link.reset();
finish_with_fault("transition to unregistered state",
true);
co_return;
}
@@ -2338,9 +2342,7 @@ namespace Void::Fsm
fire_after(current_state());
if (exit_failed) {
fault(std::move(exit_error));
on_machine_exit();
m_parent_stop_link.reset();
finish_with_fault(std::move(exit_error), true);
co_return;
}
if (body_cancelled ||
@@ -2350,17 +2352,15 @@ namespace Void::Fsm
? TerminationReason::timed_out
: TerminationReason::cancelled,
std::memory_order_release);
fault(m_root_timeout.load(std::memory_order_acquire)
? "deadline expired"
: "cancelled");
on_machine_exit();
m_parent_stop_link.reset();
finish_with_fault(
m_root_timeout.load(std::memory_order_acquire)
? "deadline expired"
: "cancelled",
true);
co_return;
}
if (body_failed) {
fault(std::move(body_error));
on_machine_exit();
m_parent_stop_link.reset();
finish_with_fault(std::move(body_error), true);
co_return;
}
@@ -2379,32 +2379,31 @@ namespace Void::Fsm
m_termination_reason.store(
TerminationReason::succeeded,
std::memory_order_release);
m_status.store(Status::Succeeded,
std::memory_order_release);
reject_pending_checkpoints(make_fsm_error(
FsmErrorCode::checkpoint_rejected,
"machine succeeded before checkpoint boundary"));
on_machine_exit();
m_parent_stop_link.reset();
m_status.store(Status::Succeeded,
std::memory_order_release);
co_return;
case StateKey::Kind::Fail:
m_result_value = std::move(step.payload());
m_termination_reason.store(
TerminationReason::failed,
std::memory_order_release);
m_status.store(Status::Failed,
std::memory_order_release);
reject_pending_checkpoints(make_fsm_error(
FsmErrorCode::checkpoint_rejected,
"machine failed before checkpoint boundary"));
on_machine_exit();
m_parent_stop_link.reset();
m_status.store(Status::Failed,
std::memory_order_release);
co_return;
case StateKey::Kind::Fault:
fault(std::any_cast<FsmError>(step.payload())
.message);
on_machine_exit();
m_parent_stop_link.reset();
finish_with_fault(
std::any_cast<FsmError>(step.payload()).message,
true);
co_return;
}
}
@@ -2412,9 +2411,11 @@ namespace Void::Fsm
Coro::Task<void> driver()
{
const bool owns = m_owns_ctx;
auto ctx = m_ctx;
co_await drive();
if (m_owns_ctx && m_ctx) {
m_ctx->finish();
if (owns && ctx) {
ctx->finish();
}
}
+7
View File
@@ -79,6 +79,13 @@ namespace Void::Fsm
{
}
~FsmModel() override
{
if (m_fsm) {
m_fsm->stop();
}
}
void stop() override { m_fsm->stop(); }
void restart() override { m_fsm->restart(); }
[[nodiscard]] Status status() const override