diff --git a/rivetkit-typescript/packages/rivetkit/src/registry/native.ts b/rivetkit-typescript/packages/rivetkit/src/registry/native.ts index 9f4ab17873..15a424edd1 100644 --- a/rivetkit-typescript/packages/rivetkit/src/registry/native.ts +++ b/rivetkit-typescript/packages/rivetkit/src/registry/native.ts @@ -3950,16 +3950,19 @@ export function buildNativeFactory( const actorCtx = makeActorCtx(ctx); try { if (onSleep) { - await onSleep(actorCtx); - if (runtime.kind === "wasm") { - // Wasm cannot use the native context save helper here because - // the runtime owns the serialized state handoff. - await runtime.actorSaveState( - ctx, - actorCtx.serializeForTick("save"), - ); - } else { - await actorCtx.saveState({ immediate: true }); + try { + await onSleep(actorCtx); + } finally { + if (runtime.kind === "wasm") { + // Wasm cannot use the native context save helper here because + // the runtime owns the serialized state handoff. + await runtime.actorSaveState( + ctx, + actorCtx.serializeForTick("save"), + ); + } else { + await actorCtx.saveState({ immediate: true }); + } } } } finally {