Skip to content

fix(rivetkit): keep actor event loop alive through shutdown state serialization - #5546

Open
abcxff wants to merge 1 commit into
stack/feat-container-runner-report-actors-as-crashed-on-unexpected-platform-sigterm-lzuymmykfrom
stack/fix-rivetkit-keep-actor-event-loop-alive-through-shutdown-state-serialization-losmyvnt
Open

fix(rivetkit): keep actor event loop alive through shutdown state serialization#5546
abcxff wants to merge 1 commit into
stack/feat-container-runner-report-actors-as-crashed-on-unexpected-platform-sigterm-lzuymmykfrom
stack/fix-rivetkit-keep-actor-event-loop-alive-through-shutdown-state-serialization-losmyvnt

Conversation

@abcxff

@abcxff abcxff commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@claude

claude Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review

Re-verified against rivetkit-core's shutdown sequence (ActorTask::run_shutdown in rivetkit-rust/packages/rivetkit-core/src/actor/task.rs); the fix is correct and this comment supersedes my prior pass.

Root cause confirmed: run_shutdown calls save_final_state() (which sends ActorEvent::SerializeState) before close_actor_event_channel(). Previously, handle_actor_event returned Ok(true) right after handling RunGracefulCleanup, breaking the run_actor loop and dropping the Events<A> receiver. When core then tried to send the follow-up SerializeState event, the enqueue failed (send_actor_event -> not_ready), and core fell back to self.ctx.save_state(Vec::new()).await, discarding any state written by on_sleep/on_destroy. The new code correctly lets the loop keep running until core itself drops the sender via close_actor_event_channel() (task.rs:1494-1497), matching the pattern already used by the NAPI runtime (napi_actor_events.rs, which never terminates its dispatch loop on RunGracefulCleanup either).

I also checked whether this can deadlock if run_shutdown's inner block returns early (e.g. save_state fails before reaching close_actor_event_channel()): it cannot, because ActorTask::run takes self by value and returns right after, so actor_event_tx is dropped when the task struct itself goes out of scope regardless of the early return. No hang risk.

Test coverage: run_actor_serializes_state_after_cleanup is a solid regression test. It would fail under the old code since tx.send(SerializeState) happens after the loop already exited and dropped the receiver. Good that it asserts on the actual on_sleep-written log entry rather than just "no error." The mechanical drop(tx) additions across the other lifecycle tests are consistent and necessary now that the loop no longer self-terminates on RunGracefulCleanup; every request_sleep/request_destroy call site got the corresponding drop(tx).

Minor (non-blocking) nit: handle_actor_event's Result<bool> return and the should_stop/break in run_actor (start.rs:249-252) are now dead code. No match arm returns Ok(true) anymore, so should_stop is always false and the loop only ever ends via events.recv_raw() returning None. Worth collapsing to Result<()> (or a one-line comment noting it is kept for a future early-stop case) so the dead branch does not linger.

Scope is minimal and confined to rivetkit-rust/packages/rivetkit/src/start.rs, consistent with this crate's role as a thin wrapper. No bugs, security, or performance concerns found. Nothing blocking.

@abcxff
abcxff force-pushed the stack/fix-rivetkit-keep-actor-event-loop-alive-through-shutdown-state-serialization-losmyvnt branch from a4f9445 to a08e479 Compare August 4, 2026 16:15
@abcxff
abcxff force-pushed the stack/feat-container-runner-report-actors-as-crashed-on-unexpected-platform-sigterm-lzuymmyk branch from d383183 to 2e35cdc Compare August 4, 2026 16:15
@abcxff
abcxff force-pushed the stack/feat-container-runner-report-actors-as-crashed-on-unexpected-platform-sigterm-lzuymmyk branch from 2e35cdc to 3f1016b Compare August 5, 2026 17:09
@abcxff
abcxff force-pushed the stack/fix-rivetkit-keep-actor-event-loop-alive-through-shutdown-state-serialization-losmyvnt branch from a08e479 to 40d03bf Compare August 5, 2026 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant