Skip to content

Commit d6cf484

Browse files
committed
fix(engine-runner): handle shutdown close codes correctly
1 parent af232ee commit d6cf484

File tree

1 file changed

+22
-27
lines changed
  • engine/sdks/typescript/runner/src

1 file changed

+22
-27
lines changed

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

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ export class Runner {
205205
runnerId?: string;
206206
#started: boolean = false;
207207
#shutdown: boolean = false;
208-
#shuttingDown: boolean = false;
209208
#reconnectAttempt: number = 0;
210209
#reconnectTimeout?: NodeJS.Timeout;
211210

@@ -480,20 +479,19 @@ export class Runner {
480479
// MARK: Shutdown
481480
async shutdown(immediate: boolean, exit: boolean = false) {
482481
// Prevent concurrent shutdowns
483-
if (this.#shuttingDown) {
482+
if (this.#shutdown) {
484483
this.log?.debug({
485484
msg: "shutdown already in progress, ignoring",
486485
});
487486
return;
488487
}
489-
this.#shuttingDown = true;
488+
this.#shutdown = true;
490489

491490
this.log?.info({
492491
msg: "starting shutdown",
493492
immediate,
494493
exit,
495494
});
496-
this.#shutdown = true;
497495

498496
// Clear reconnect timeout
499497
if (this.#reconnectTimeout) {
@@ -918,41 +916,34 @@ export class Runner {
918916
});
919917

920918
ws.addEventListener("close", async (ev) => {
921-
const closeError = parseWebSocketCloseReason(ev.reason);
922-
if (
923-
closeError?.group === "ws" &&
924-
closeError?.error === "eviction"
925-
) {
926-
this.log?.info("runner websocket evicted");
927-
928-
this.#config.onDisconnected(ev.code, ev.reason);
929-
930-
await this.shutdown(true);
931-
} else {
919+
if (!this.#shutdown) {
920+
const closeError = parseWebSocketCloseReason(ev.reason);
932921
if (
933-
closeError?.group === "pegboard" &&
934-
closeError?.error === "runner_shutdown"
922+
closeError?.group === "ws" &&
923+
closeError?.error === "eviction"
935924
) {
936-
this.log?.info("runner shutdown");
925+
this.log?.info("runner websocket evicted");
926+
927+
this.#config.onDisconnected(ev.code, ev.reason);
928+
929+
await this.shutdown(true);
937930
} else {
938931
this.log?.warn({
939932
msg: "runner disconnected",
940933
code: ev.code,
941934
reason: ev.reason.toString(),
942935
closeError,
943936
});
944-
}
945937

946-
this.#config.onDisconnected(ev.code, ev.reason);
947-
}
938+
this.#config.onDisconnected(ev.code, ev.reason);
939+
}
948940

949-
// Clear ack interval on close
950-
if (this.#ackInterval) {
951-
clearInterval(this.#ackInterval);
952-
this.#ackInterval = undefined;
953-
}
941+
// Clear ack interval on close
942+
if (this.#ackInterval) {
943+
clearInterval(this.#ackInterval);
944+
this.#ackInterval = undefined;
945+
}
954946

955-
if (!this.#shutdown) {
956947
// Start runner lost timeout if we have a threshold and are not shutting down
957948
if (
958949
!this.#runnerLostTimeout &&
@@ -977,6 +968,10 @@ export class Runner {
977968

978969
// Attempt to reconnect if not stopped
979970
this.#scheduleReconnect();
971+
} else {
972+
this.log?.info("websocket closed");
973+
974+
this.#config.onDisconnected(ev.code, ev.reason);
980975
}
981976
});
982977
}

0 commit comments

Comments
 (0)