Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions engine/sdks/typescript/runner/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,19 @@ export class Runner {
if (!actor) return;

// If onActorStop times out, Pegboard will handle this timeout with ACTOR_STOP_THRESHOLD_DURATION_MS
//
// If we receive a request while onActorStop is running, a Service
// Unavailable error will be returned to Guard and the request will be
// retried
try {
await this.#config.onActorStop(actorId, actor.generation);
} catch (err) {
console.error(`Error in onActorStop for actor ${actorId}:`, err);
}

// Close requests after onActorStop so you can send messages over the tunnel
this.#tunnel?.closeActiveRequests(actor);

this.#sendActorStateUpdate(actorId, actor.generation, "stopped");
}

Expand Down Expand Up @@ -217,6 +224,7 @@ export class Runner {
);
}

// IMPORTANT: Make sure to call stopActiveRequests if calling #removeActor
#removeActor(
actorId: string,
generation?: number,
Expand All @@ -242,8 +250,6 @@ export class Runner {

this.#actors.delete(actorId);

// Unregister actor from tunnel
this.#tunnel?.unregisterActor(actor);

return actor;
}
Expand Down
9 changes: 7 additions & 2 deletions engine/sdks/typescript/runner/src/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class Tunnel {
}
}

unregisterActor(actor: ActorInstance) {
closeActiveRequests(actor: ActorInstance) {
const actorId = actor.actorId;

// Terminate all requests for this actor
Expand Down Expand Up @@ -469,7 +469,12 @@ export class Tunnel {
msg: "ignoring websocket for unknown actor",
actorId: open.actorId,
});
// Send close immediately

// NOTE: Closing a WebSocket before open is equivalent to a Service
// Unavailable error and will cause Guard to retry the request
//
// See
// https://github.com/rivet-dev/rivet/blob/222dae87e3efccaffa2b503de40ecf8afd4e31eb/engine/packages/pegboard-gateway/src/lib.rs#L238
this.#sendMessage(requestId, {
tag: "ToServerWebSocketClose",
val: {
Expand Down
Loading