Skip to content

fix(rivetkit-core): return stopping not starting when sleep/destroy called mid-shutdown#4757

Merged
NathanFlurry merged 1 commit intomainfrom
sleep-cleanup/12-sleep-destroy-mid-shutdown-diagnostic
Apr 27, 2026
Merged

fix(rivetkit-core): return stopping not starting when sleep/destroy called mid-shutdown#4757
NathanFlurry merged 1 commit intomainfrom
sleep-cleanup/12-sleep-destroy-mid-shutdown-diagnostic

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 24, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude Bot commented Apr 24, 2026

PR Review: fix(rivetkit-core): return stopping not starting when sleep/destroy called mid-shutdown

Overview

This PR fixes a diagnostic accuracy bug in the sleep() and destroy() lifecycle methods. When the lifecycle state machine transitions into SleepGrace or DestroyGrace, it clears the started flag. Any subsequent call to sleep() or destroy() would hit the !started guard and incorrectly return ActorLifecycleError::Starting ("cannot request sleep before actor startup completes") -- misleading callers into thinking the actor never started. The fix correctly disambiguates "never started" vs. "mid-shutdown" by consulting the sleep_requested / destroy_requested flags.


Code Quality

Correct and targeted. The three-flag check in destroy() follows naturally from the escalation semantics: destroy() after sleep() is valid and intentional, so the guard must pass through even when started=false.

Non-atomic multi-load pattern is acceptable here. The three atomic reads are not one coherent snapshot, but this only matters for diagnostic classification, not for making a state transition. The actual state transition is governed by the subsequent swap(true) calls.

Comments follow conventions -- complete sentences, no em-dashes, explain the why not the what.


Potential Issues

Missing test for the destroy() path. The new test covers sleep() after grace, but the symmetric destroy() scenario has no coverage:

  • destroy() called when started=false and sleep_requested=true (escalation path, should succeed).
  • destroy() called when started=false and destroy_requested=true (idempotent path, should return Stopping at the swap).

Both branches are reachable in production and meaningfully different from the pre-startup case.

Asymmetry between sleep() and destroy() at the guard level. sleep() classifies and returns Stopping at the guard. destroy() lets the already-stopping case fall through to the destroy_requested.swap(true) check, which also returns Stopping. This is correct, but a short inline comment at the destroy() guard would confirm to future readers that the fall-through is intentional.


Suggestions

  1. Add a destroy() counterpart test to cover both the escalation path (sleep requested, then destroy) and the idempotent path (destroy requested, then destroy again). The existing sleep_after_grace_clears_started_returns_stopping_not_starting test is a good template.

  2. Optional one-liner at the destroy() guard to clarify the fall-through behavior for already-stopping cases.


Summary

The core fix is correct, well-commented, and safe. The one material gap is missing test coverage for the destroy() escalation and idempotent paths.

@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/11-remove-prevent-sleep-fixture branch from c7f9133 to ace5f49 Compare April 24, 2026 13:16
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/12-sleep-destroy-mid-shutdown-diagnostic branch from 1939547 to 68ddecb Compare April 24, 2026 13:16
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

Preview packages published to npm

Install with:

npm install rivetkit@pr-4757

All packages published as 0.0.0-pr.4757.c15ed18 with tag pr-4757.

Engine binary is shipped via @rivetkit/engine-cli on linux-x64-musl, linux-arm64-musl, darwin-x64, and darwin-arm64. Windows users should use the release installer or set RIVET_ENGINE_BINARY.

Docker images:

docker pull rivetdev/engine:slim-c15ed18
docker pull rivetdev/engine:full-c15ed18
Individual packages
npm install rivetkit@pr-4757
npm install @rivetkit/react@pr-4757
npm install @rivetkit/rivetkit-napi@pr-4757
npm install @rivetkit/workflow-engine@pr-4757

Base automatically changed from sleep-cleanup/11-remove-prevent-sleep-fixture to main April 27, 2026 07:13
@NathanFlurry NathanFlurry merged commit a4740bd into main Apr 27, 2026
18 of 22 checks passed
@NathanFlurry NathanFlurry deleted the sleep-cleanup/12-sleep-destroy-mid-shutdown-diagnostic branch April 27, 2026 07:14
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