Skip to content

Commit 30b896a

Browse files
committed
fix(runner): close active requests after onActorStop has finished running
1 parent 65f206c commit 30b896a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,19 @@ export class Runner {
164164
if (!actor) return;
165165

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

177+
// Close requests after onActorStop so you can send messages over the tunnel
178+
this.#tunnel?.closeActiveRequests(actor);
179+
173180
this.#sendActorStateUpdate(actorId, actor.generation, "stopped");
174181
}
175182

@@ -217,6 +224,7 @@ export class Runner {
217224
);
218225
}
219226

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

243251
this.#actors.delete(actorId);
244252

245-
// Unregister actor from tunnel
246-
this.#tunnel?.unregisterActor(actor);
247253

248254
return actor;
249255
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class Tunnel {
183183
}
184184
}
185185

186-
unregisterActor(actor: ActorInstance) {
186+
closeActiveRequests(actor: ActorInstance) {
187187
const actorId = actor.actorId;
188188

189189
// Terminate all requests for this actor
@@ -469,7 +469,12 @@ export class Tunnel {
469469
msg: "ignoring websocket for unknown actor",
470470
actorId: open.actorId,
471471
});
472-
// Send close immediately
472+
473+
// NOTE: Closing a WebSocket before open is equivalent to a Service
474+
// Unavailable error and will cause Guard to retry the request
475+
//
476+
// See
477+
// https://github.com/rivet-dev/rivet/blob/222dae87e3efccaffa2b503de40ecf8afd4e31eb/engine/packages/pegboard-gateway/src/lib.rs#L238
473478
this.#sendMessage(requestId, {
474479
tag: "ToServerWebSocketClose",
475480
val: {

0 commit comments

Comments
 (0)