Skip to content

Commit fb0fa44

Browse files
committed
fix(rivetkit): fix actors trying to sleep after stop started
1 parent 0898db1 commit fb0fa44

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

engine/sdks/typescript/runner/src/mod.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ export class Runner {
172172

173173
// MARK: Manage actors
174174
sleepActor(actorId: string, generation?: number) {
175+
if (this.#shutdown) {
176+
this.log?.warn({
177+
msg: "runner is shut down, cannot sleep actor",
178+
});
179+
return;
180+
}
181+
175182
const actor = this.getActor(actorId, generation);
176183
if (!actor) return;
177184

@@ -860,6 +867,7 @@ export class Runner {
860867
intentType: "sleep" | "stop",
861868
) {
862869
if (this.#shutdown) {
870+
console.trace("send actor intent", actorId, intentType);
863871
this.log?.warn({
864872
msg: "Runner is shut down, cannot send actor intent",
865873
});

rivetkit-typescript/packages/rivetkit/src/actor/instance.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,9 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
19201920
#resetSleepTimer() {
19211921
if (this.#config.options.noSleep || !this.#sleepingSupported) return;
19221922

1923+
// Don't sleep if already stopping
1924+
if (this.#stopCalled) return;
1925+
19231926
const canSleep = this.#canSleep();
19241927

19251928
this.#rLog.debug({
@@ -1979,11 +1982,20 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
19791982
* 4. Engine runner will publish EventActorStateUpdate with ActorStateSTop
19801983
**/
19811984
_startSleep() {
1985+
if (this.#stopCalled) {
1986+
this.#rLog.debug({
1987+
msg: "cannot call _startSleep if actor already stopping",
1988+
});
1989+
return;
1990+
}
1991+
19821992
// IMPORTANT: #sleepCalled should have no effect on the actor's
19831993
// behavior aside from preventing calling _startSleep twice. Wait for
19841994
// `_onStop` before putting in a stopping state.
19851995
if (this.#sleepCalled) {
1986-
this.#rLog.warn({ msg: "already sleeping actor" });
1996+
this.#rLog.warn({
1997+
msg: "cannot call _startSleep twice, actor already sleeping",
1998+
});
19871999
return;
19882000
}
19892001
this.#sleepCalled = true;
@@ -2069,7 +2081,6 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
20692081

20702082
// Clear timeouts
20712083
if (this.#pendingSaveTimeout) clearTimeout(this.#pendingSaveTimeout);
2072-
if (this.#sleepTimeout) clearTimeout(this.#sleepTimeout);
20732084
if (this.#checkConnLivenessInterval)
20742085
clearInterval(this.#checkConnLivenessInterval);
20752086

rivetkit-typescript/packages/rivetkit/src/drivers/engine/actor-driver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ export class EngineActorDriver implements ActorDriver {
602602
}
603603

604604
async shutdownRunner(immediate: boolean): Promise<void> {
605-
logger().info({ msg: "stopping engine actor driver" });
605+
logger().info({ msg: "stopping engine actor driver", immediate });
606606

607607
// Clear the ack flush interval
608608
if (this.#wsAckFlushInterval) {

0 commit comments

Comments
 (0)