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