FSM: fix parked-coroutine leak — Machine owns its driver
A machine started in worker mode and stopped while still parked on an event (e.g. await_event that never arrives) leaked its whole suspended coroutine frame chain. start() used Async::spawn, which detaches the driver in a fire-and-forget DetachedTask with no retained handle, so nothing could ever destroy the parked frames. Pre-existing since before the FSM upgrade (confirmed via a worktree at the prior baseline); the Serialize suite hit it (two tests start a machine that parks then stop()). Fix: the Machine now owns its top-level driver coroutine as a member (std::optional<Coro::Task<void>>) instead of detaching it — start() posts the driver's handle to the executor while retaining ownership, and stop() / reset() destroy it after the worker is joined. Destroying the owning Task cascade-destroys the suspended chain (each nested Task destructor frees its handle). Added a non-owning Coro::Task::handle() accessor for scheduling. Full ASan/UBSan suite now clean (was: 1616 bytes leaked in Serialize); 1002 tests (999 green, 3 skipped), 0 flaky in 12 worker-mode runs, Void_Fsm -Werror clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4af3ee6d0c
commit
43b5a81504
2 files changed
+221
-147
No files matched your search
@@ -533,7 +533,11 @@ namespace Void::Fsm
|
||||
m_owns_ctx = true;
|
||||
}
|
||||
m_status.store(Status::Running);
|
||||
Async::spawn(m_ctx->executor(), driver());
|
||||
// Own the driver coroutine (rather than detaching it) so a
|
||||
// machine stopped while parked on an event can destroy the
|
||||
// suspended frame chain instead of leaking it.
|
||||
m_driver.emplace(driver());
|
||||
m_ctx->executor().post(m_driver->handle());
|
||||
if (m_options.own_worker && m_owns_ctx) {
|
||||
m_ctx->start_worker();
|
||||
}
|
||||
@@ -589,6 +593,11 @@ namespace Void::Fsm
|
||||
if (m_owns_ctx && m_ctx) {
|
||||
m_ctx->stop();
|
||||
}
|
||||
// The worker is joined now (no concurrent resume), so destroy
|
||||
// the owned driver — this cascade-destroys the suspended
|
||||
// coroutine chain (a machine parked on an event) instead of
|
||||
// leaking it. Safe on a finished coroutine too.
|
||||
m_driver.reset();
|
||||
}
|
||||
|
||||
/// Return a finished (or idle) machine to Idle so it can run
|
||||
@@ -602,6 +611,7 @@ namespace Void::Fsm
|
||||
if (m_owns_ctx && m_ctx) {
|
||||
m_ctx->stop();
|
||||
}
|
||||
m_driver.reset(); // destroy the (possibly parked) driver chain
|
||||
m_ctx.reset();
|
||||
m_owns_ctx = false;
|
||||
m_current.store(-1, std::memory_order_release);
|
||||
@@ -1421,6 +1431,11 @@ namespace Void::Fsm
|
||||
// External jump request (jump<S>()); applied by drive() at the
|
||||
// next transition boundary.
|
||||
std::atomic<const std::type_info*> m_jump{nullptr};
|
||||
|
||||
// The owned top-level driver coroutine (worker mode). Holding it
|
||||
// (rather than detaching via Async::spawn) lets stop() destroy the
|
||||
// suspended frame chain of a machine parked on an event.
|
||||
std::optional<Coro::Task<void>> m_driver;
|
||||
};
|
||||
|
||||
} // namespace Void::Fsm
|
||||
Reference in new issue
Block a user