Skip to content

Commit c12423d

Browse files
NathanFlurryMasterPtato
authored andcommitted
fix(rivetkit): exit pid1 after signal shutdown
1 parent 3e77ae5 commit c12423d

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

  • rivetkit-typescript/packages/rivetkit/src/registry

rivetkit-typescript/packages/rivetkit/src/registry/index.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ import type { RuntimeServerlessResponseHead } from "./runtime";
1313

1414
type ShutdownSignal = "SIGINT" | "SIGTERM";
1515

16+
function signalExitCode(signal: ShutdownSignal): number {
17+
switch (signal) {
18+
case "SIGINT":
19+
return 130;
20+
case "SIGTERM":
21+
return 143;
22+
}
23+
}
24+
25+
function finishShutdownSignal(signal: ShutdownSignal): void {
26+
if (process.pid === 1) {
27+
process.exit(signalExitCode(signal));
28+
}
29+
process.kill(process.pid, signal);
30+
}
31+
1632
export type FetchHandler = (
1733
request: Request,
1834
...args: any
@@ -452,10 +468,11 @@ export class Registry<A extends RegistryActors> {
452468
): void {
453469
if (this.#shutdownInFlight !== null) {
454470
// Second delivery of the same (or another) shutdown signal.
455-
// Remove our handler only (preserving any user-installed listeners)
456-
// and re-raise so Node proceeds with its default exit path.
471+
// Remove our handler only, preserving any user-installed listeners.
472+
// PID 1 must exit directly because re-raised default signals can be
473+
// swallowed by the container signal path.
457474
this.#removeSignalHandlers();
458-
process.kill(process.pid, signal);
475+
finishShutdownSignal(signal);
459476
return;
460477
}
461478
this.#shutdownInFlight = this.#runShutdown(
@@ -530,7 +547,7 @@ export class Registry<A extends RegistryActors> {
530547
),
531548
]);
532549
this.#removeSignalHandlers();
533-
process.kill(process.pid, signal);
550+
finishShutdownSignal(signal);
534551
}
535552

536553
#removeSignalHandlers(): void {

0 commit comments

Comments
 (0)