Skip to content
Draft
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
25 changes: 21 additions & 4 deletions rivetkit-typescript/packages/rivetkit/src/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ import type { RuntimeServerlessResponseHead } from "./runtime";

type ShutdownSignal = "SIGINT" | "SIGTERM";

function signalExitCode(signal: ShutdownSignal): number {
switch (signal) {
case "SIGINT":
return 130;
case "SIGTERM":
return 143;
}
}

function finishShutdownSignal(signal: ShutdownSignal): void {
if (process.pid === 1) {
process.exit(signalExitCode(signal));
}
process.kill(process.pid, signal);
}

export type FetchHandler = (
request: Request,
...args: any
Expand Down Expand Up @@ -452,10 +468,11 @@ export class Registry<A extends RegistryActors> {
): void {
if (this.#shutdownInFlight !== null) {
// Second delivery of the same (or another) shutdown signal.
// Remove our handler only (preserving any user-installed listeners)
// and re-raise so Node proceeds with its default exit path.
// Remove our handler only, preserving any user-installed listeners.
// PID 1 must exit directly because re-raised default signals can be
// swallowed by the container signal path.
this.#removeSignalHandlers();
process.kill(process.pid, signal);
finishShutdownSignal(signal);
return;
}
this.#shutdownInFlight = this.#runShutdown(
Expand Down Expand Up @@ -530,7 +547,7 @@ export class Registry<A extends RegistryActors> {
),
]);
this.#removeSignalHandlers();
process.kill(process.pid, signal);
finishShutdownSignal(signal);
}

#removeSignalHandlers(): void {
Expand Down
Loading