@rivetkit/engine-runner 0.0.0-main.41efcce → 0.0.0-main.4994268

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivetkit/engine-runner",
3
- "version": "0.0.0-main.41efcce",
3
+ "version": "0.0.0-main.4994268",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -25,8 +25,8 @@
25
25
  "bench": "tsx benches/actor-lifecycle.bench.ts"
26
26
  },
27
27
  "dependencies": {
28
- "@rivetkit/virtual-websocket": "0.0.0-main.41efcce",
29
- "@rivetkit/engine-runner-protocol": "0.0.0-main.41efcce",
28
+ "@rivetkit/virtual-websocket": "0.0.0-main.4994268",
29
+ "@rivetkit/engine-runner-protocol": "0.0.0-main.4994268",
30
30
  "uuid": "^12.0.0",
31
31
  "pino": "^9.9.5",
32
32
  "ws": "^8.18.3"
package/src/mod.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as protocol from "@rivetkit/engine-runner-protocol";
2
2
  import type { Logger } from "pino";
3
+ import { v4 as uuidv4 } from "uuid";
3
4
  import type WebSocket from "ws";
4
5
  import { type ActorConfig, RunnerActor } from "./actor";
5
6
  import { logger, setLogger } from "./log.js";
@@ -12,7 +13,6 @@ import {
12
13
  unreachable,
13
14
  } from "./utils";
14
15
  import { importWebSocket } from "./websocket.js";
15
- import { v4 as uuidv4 } from "uuid";
16
16
 
17
17
  export type { HibernatingWebSocketMetadata };
18
18
  export { RunnerActor, type ActorConfig };
@@ -560,9 +560,18 @@ export class Runner {
560
560
  this.#kvRequests.clear();
561
561
 
562
562
  // Close WebSocket
563
- const pegboardWebSocket = this.getPegboardWebSocketIfReady();
564
- if (pegboardWebSocket) {
565
- if (immediate) {
563
+ //
564
+ // A CONNECTING socket cannot send graceful stopping messages, so
565
+ // it is always closed directly. Without this, a shutdown that
566
+ // races with a reconnect leaves the in-flight WebSocket alive
567
+ // while the rest of the runner state (tunnel, intervals) has been
568
+ // torn down.
569
+ const pegboardWebSocket = this.#pegboardWebSocket;
570
+ const readyState = pegboardWebSocket?.readyState;
571
+ const isOpen = readyState === 1;
572
+ const isConnecting = readyState === 0;
573
+ if (pegboardWebSocket && (isOpen || isConnecting)) {
574
+ if (immediate || isConnecting) {
566
575
  // Stop immediately
567
576
  pegboardWebSocket.close(1000, "pegboard.runner_shutdown");
568
577
  } else {
@@ -874,6 +883,10 @@ export class Runner {
874
883
  if (this.runnerId !== init.runnerId) {
875
884
  this.runnerId = init.runnerId;
876
885
 
886
+ // Invalidate cached child logger so subsequent log
887
+ // lines pick up the new runnerId.
888
+ this.#logCached = undefined;
889
+
877
890
  // Clear actors if runner id changed
878
891
  this.#stopAllActors();
879
892
  }
@@ -1044,7 +1057,7 @@ export class Runner {
1044
1057
 
1045
1058
  for (const [_, actor] of this.#actors) {
1046
1059
  const checkpoint = ack.lastEventCheckpoints.find(
1047
- (x) => x.actorId == actor.actorId,
1060
+ (x) => x.actorId === actor.actorId,
1048
1061
  );
1049
1062
 
1050
1063
  if (checkpoint) actor.handleAckEvents(checkpoint.index);
@@ -1165,14 +1178,14 @@ export class Runner {
1165
1178
  await this.#config.onActorStart(actorId, generation, actorConfig);
1166
1179
 
1167
1180
  instance.actorStartPromise.resolve();
1168
- } catch (err) {
1181
+ } catch (error) {
1169
1182
  this.log?.error({
1170
1183
  msg: "error starting runner actor",
1171
1184
  actorId,
1172
- err,
1185
+ error,
1173
1186
  });
1174
1187
 
1175
- instance.actorStartPromise.reject(err);
1188
+ instance.actorStartPromise.reject(error);
1176
1189
 
1177
1190
  // TODO: Mark as crashed
1178
1191
  // Send stopped state update if start failed
@@ -1181,7 +1194,7 @@ export class Runner {
1181
1194
  }
1182
1195
 
1183
1196
  async #handleCommandStopActor(commandWrapper: protocol.CommandWrapper) {
1184
- const stopCommand = commandWrapper.inner
1197
+ const _stopCommand = commandWrapper.inner
1185
1198
  .val as protocol.CommandStopActor;
1186
1199
 
1187
1200
  const actorId = commandWrapper.checkpoint.actorId;
package/src/tunnel.ts CHANGED
@@ -1,9 +1,5 @@
1
1
  import type * as protocol from "@rivetkit/engine-runner-protocol";
2
- import type {
3
- GatewayId,
4
- MessageId,
5
- RequestId,
6
- } from "@rivetkit/engine-runner-protocol";
2
+ import type { GatewayId, RequestId } from "@rivetkit/engine-runner-protocol";
7
3
  import type { Logger } from "pino";
8
4
  import { type Runner, type RunnerActor, RunnerShutdownError } from "./mod";
9
5
  import {
@@ -1,9 +1,9 @@
1
- import type { Logger } from "pino";
2
1
  import {
3
- VirtualWebSocket,
4
- type UniversalWebSocket,
5
2
  type RivetMessageEvent,
3
+ type UniversalWebSocket,
4
+ VirtualWebSocket,
6
5
  } from "@rivetkit/virtual-websocket";
6
+ import type { Logger } from "pino";
7
7
  import type { Tunnel } from "./tunnel";
8
8
  import {
9
9
  MAX_PAYLOAD_SIZE,