Skip to content

Comments

fix(rivetkit): stall stop handler until start completes#4192

Open
MasterPtato wants to merge 1 commit intomainfrom
02-13-fix_rivetkit_stall_stop_handler_until_start_completes
Open

fix(rivetkit): stall stop handler until start completes#4192
MasterPtato wants to merge 1 commit intomainfrom
02-13-fix_rivetkit_stall_stop_handler_until_start_completes

Conversation

@MasterPtato
Copy link
Contributor

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

@railway-app
Copy link

railway-app bot commented Feb 13, 2026

🚅 Deployed to the rivet-pr-4192 environment in rivet-frontend

Service Status Web Updated (UTC)
website 😴 Sleeping (View Logs) Web Feb 25, 2026 at 12:28 am
frontend-inspector ❌ Build Failed (View Logs) Web Feb 19, 2026 at 10:55 pm
frontend-cloud ❌ Build Failed (View Logs) Web Feb 19, 2026 at 10:54 pm
ladle ❌ Build Failed (View Logs) Web Feb 19, 2026 at 10:54 pm
mcp-hub ✅ Success (View Logs) Web Feb 13, 2026 at 9:55 pm

Copy link
Contributor Author


How to use the Graphite Merge Queue

Add the label merge-queue to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

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

@claude
Copy link

claude bot commented Feb 13, 2026

PR Review: fix(rivetkit): stall stop handler until start completes

Summary

This PR fixes a race condition in EngineActorDriver where #runnerOnActorStop could be called before #runnerOnActorStart finishes, leaving an orphaned handler in #actors and skipping onStop. The fix is correct and well-targeted.


Core Fix Analysis (actor-driver.ts)

The bug: #actors.delete(actorId) was inside the if (handler?.actor) block. If stop arrived before the actor fully started (handler.actor not yet set), the handler was never removed from the map — a leak.

The fix: moves delete unconditionally after the stop logic and adds an await handler.actorStartPromise.promise gate so stop properly serializes behind start.

This is the right approach. One observation: after await handler.actorStartPromise.promise, we still hold the pre-await handler reference. Since handler is an object reference (not a copy) and the map value is mutated in-place (e.g. handler.actor = ..., handler.actorStartPromise = undefined), this is correct — the local reference reflects the live state after start completes.

Potential double-stop concern: If #runnerOnActorStop is called concurrently for the same actorId (unlikely but possible), both calls would capture the same handler before either deletes it, both would await the start promise, and both would call onStop. This isn't introduced by this PR, but the new async gap makes it slightly more reachable. Worth noting as a follow-up.


Tests (actor-lifecycle.ts)

Test: "onDestroy is called even when actor is destroyed during start" — this test has a likely ordering bug:

const statePromise = actor.getState();   // (1) enqueues getState
await actor.destroy();                   // (2) waits for start, then stops actor
const state = await statePromise;        // (3) awaits getState result
expect(state.destroyCalled).toBe(true);  // expects onDestroy already ran

With the fix, the sequence is: start completes → queued getState action runs (returning destroyCalled: false) → actor stops → onDestroy sets destroyCalled: true. The action result from step (3) would be false, not true. This test will likely fail or pass only by coincidence.

Test: "multiple rapid create/destroy cycles..." — the assertion expect(true).toBe(true) is an anti-pattern. If the intent is just "no error thrown," the assertion can be removed (Vitest will pass the test if no exception is thrown).

Unused fixture: lifecycleObserver is added to the registry but not used in any test in this PR. It should either be used or removed to avoid dead code.


Tests (conn-error-serialization.ts)

Cleanup not in finally: The conn.dispose() call in both tests sits after the assertion, so if an assertion fails the dispose never runs. Consider wrapping with try/finally:

const conn = actor.connect();
try {
  await conn.getValue();
} catch (err) {
  caughtError = err;
} finally {
  await conn.dispose();
}

This is especially relevant for the error-path test where conn may be in a failed state — disposing a never-established connection may also need to be handled gracefully.


Linter / Style

The Graphite bot already flagged the { } spacing in empty arrow functions (Biome prefers {}). These are minor formatting issues that should be fixed to pass CI.


Overall

The fix is correct and addresses a real race condition. Main items before merge:

  1. Fix the ordering assumption in the destroyCalled test or remove the assertion
  2. Remove unused lifecycleObserver fixture or add a test that uses it
  3. Move conn.dispose() to finally blocks in the error serialization tests
  4. Fix Biome formatting ({} vs { } in empty callbacks)

this.#runnerStarted.resolve(undefined);
},
onDisconnected: (_code, _reason) => {},
onDisconnected: (_code, _reason) => { },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove spaces inside the empty function braces to comply with Biome linter formatting rules. Change onDisconnected: (_code, _reason) => { }, to onDisconnected: (_code, _reason) => {},

Spotted by Graphite Agent (based on CI logs)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

return streamSSE(c, async (stream) => {
// NOTE: onAbort does not work reliably
stream.onAbort(() => {});
stream.onAbort(() => { });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove spaces inside the empty function braces to comply with Biome linter formatting rules. Change stream.onAbort(() => { }); to stream.onAbort(() => {});

Spotted by Graphite Agent (based on CI logs)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Comment on lines +1 to +71
import { actor } from "rivetkit";

/**
* Actor designed to test start/stop race conditions.
* Has a slow initialization to make race conditions easier to trigger.
*/
export const startStopRaceActor = actor({
state: {
initialized: false,
startTime: 0,
destroyCalled: false,
startCompleted: false,
},
onWake: async (c) => {
c.state.startTime = Date.now();

// Simulate slow initialization to create window for race condition
await new Promise((resolve) => setTimeout(resolve, 100));

c.state.initialized = true;
c.state.startCompleted = true;
},
onDestroy: (c) => {
c.state.destroyCalled = true;
// Don't save state here - the actor framework will save it automatically
},
actions: {
getState: (c) => {
return {
initialized: c.state.initialized,
startTime: c.state.startTime,
destroyCalled: c.state.destroyCalled,
startCompleted: c.state.startCompleted,
};
},
ping: (c) => {
return "pong";
},
destroy: (c) => {
c.destroy();
},
},
});

/**
* Observer actor to track lifecycle events from other actors
*/
export const lifecycleObserver = actor({
state: {
events: [] as Array<{
actorKey: string;
event: string;
timestamp: number;
}>,
},
actions: {
recordEvent: (c, params: { actorKey: string; event: string }) => {
c.state.events.push({
actorKey: params.actorKey,
event: params.event,
timestamp: Date.now(),
});
},
getEvents: (c) => {
return c.state.events;
},
clearEvents: (c) => {
c.state.events = [];
},
},
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run the Biome formatter on this file to ensure proper formatting and sorted imports. The file is new and likely has formatting issues that don't match the project's style guide.

Spotted by Graphite Agent (based on CI logs)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Comment on lines +71 to +72
import { startStopRaceActor, lifecycleObserver } from "./start-stop-race";
import { connErrorSerializationActor } from "./conn-error-serialization";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should be sorted alphabetically. Consider reordering these imports to maintain consistent sorting.

Spotted by Graphite Agent (based on CI logs)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

import { runActorConnHibernationTests } from "./tests/actor-conn-hibernation";
import { runActorConnStateTests } from "./tests/actor-conn-state";
import { runActorDbTests } from "./tests/actor-db";
import { runConnErrorSerializationTests } from "./tests/conn-error-serialization";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import should be sorted alphabetically with the other imports to maintain consistent ordering.

Spotted by Graphite Agent (based on CI logs)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 18, 2026

More templates

@rivetkit/cloudflare-workers

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/cloudflare-workers@4192

@rivetkit/framework-base

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/framework-base@4192

@rivetkit/next-js

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/next-js@4192

@rivetkit/react

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/react@4192

rivetkit

pnpm add https://pkg.pr.new/rivet-dev/rivet/rivetkit@4192

@rivetkit/sql-loader

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/sql-loader@4192

@rivetkit/sqlite-vfs

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/sqlite-vfs@4192

@rivetkit/traces

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/traces@4192

@rivetkit/workflow-engine

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/workflow-engine@4192

@rivetkit/virtual-websocket

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/virtual-websocket@4192

@rivetkit/engine-runner

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/engine-runner@4192

@rivetkit/engine-runner-protocol

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/engine-runner-protocol@4192

commit: 3e27725

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