Skip to content

Commit 372c906

Browse files
SebTardifsteipete
authored andcommitted
fix(gateway): add .catch() to SIGTERM/SIGUSR1 signal handlers
The SIGTERM handler's fire-and-forget IIFE can reject if the graceful drain or tunnel-teardown throws. Without a catch, this becomes an unhandled promise rejection. Add .catch() that logs the error and falls back to a hard stop request. Same treatment for SIGUSR1. Signed-off-by: Sebastien Tardif <[email protected]>
1 parent 77c3bdb commit 372c906

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎src/cli/gateway-cli/run-loop.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,91 @@ describe("runGatewayLoop", () => {
14011401
await expect(exited).resolves.toBe(0);
14021402
});
14031403
});
1404+
1405+
it("catches SIGTERM handler errors, logs them, and falls back to stop (#83131)", async () => {
1406+
vi.clearAllMocks();
1407+
consumeGatewayRestartIntentPayloadSync.mockImplementationOnce(() => {
1408+
throw new Error("dynamic import failed");
1409+
});
1410+
1411+
await withIsolatedSignals(async ({ captureSignal }) => {
1412+
const { close, runtime, exited } = await createSignaledLoopHarness();
1413+
const sigterm = captureSignal("SIGTERM");
1414+
1415+
sigterm();
1416+
1417+
await expect(exited).resolves.toBe(0);
1418+
expect(gatewayLog.error).toHaveBeenCalledWith(
1419+
"failed to handle SIGTERM: Error: dynamic import failed",
1420+
);
1421+
expect(close).toHaveBeenCalledWith({
1422+
reason: "gateway stopping",
1423+
restartExpectedMs: null,
1424+
});
1425+
expect(runtime.exit).toHaveBeenCalledWith(0);
1426+
});
1427+
});
1428+
1429+
it("catches SIGUSR1 handler errors even when token cleanup throws (#83131)", async () => {
1430+
vi.clearAllMocks();
1431+
consumeGatewayRestartIntentPayloadSync.mockImplementationOnce(() => {
1432+
throw new Error("lifecycle module corrupted");
1433+
});
1434+
markGatewaySigusr1RestartHandled.mockImplementationOnce(() => {
1435+
throw new Error("recovery import also failed");
1436+
});
1437+
1438+
await withIsolatedSignals(async ({ captureSignal }) => {
1439+
const { close, start, exited } = await createSignaledLoopHarness();
1440+
const sigusr1 = captureSignal("SIGUSR1");
1441+
const sigterm = captureSignal("SIGTERM");
1442+
1443+
sigusr1();
1444+
await new Promise<void>((resolve) => setImmediate(resolve));
1445+
await new Promise<void>((resolve) => setImmediate(resolve));
1446+
1447+
expect(gatewayLog.error).toHaveBeenCalledWith(
1448+
"SIGUSR1 handler failed: lifecycle module corrupted",
1449+
);
1450+
expect(markGatewaySigusr1RestartHandled).toHaveBeenCalled();
1451+
expect(close).not.toHaveBeenCalled();
1452+
expect(start).toHaveBeenCalledTimes(1);
1453+
1454+
sigterm();
1455+
await expect(exited).resolves.toBe(0);
1456+
});
1457+
});
1458+
1459+
it("catches SIGUSR1 handler errors, clears restart token, and does not crash (#83131)", async () => {
1460+
vi.clearAllMocks();
1461+
consumeGatewayRestartIntentPayloadSync.mockImplementationOnce(() => {
1462+
throw new Error("sigusr1 lifecycle import failed");
1463+
});
1464+
1465+
await withIsolatedSignals(async ({ captureSignal }) => {
1466+
const { close, start, exited } = await createSignaledLoopHarness();
1467+
const sigusr1 = captureSignal("SIGUSR1");
1468+
const sigterm = captureSignal("SIGTERM");
1469+
1470+
sigusr1();
1471+
// The catch handler clears the restart token from the eagerly-loaded
1472+
// lifecycle runtime, so wait for the async signal body to reject.
1473+
await new Promise<void>((resolve) => setImmediate(resolve));
1474+
await new Promise<void>((resolve) => setImmediate(resolve));
1475+
1476+
expect(gatewayLog.error).toHaveBeenCalledWith(
1477+
"SIGUSR1 handler failed: sigusr1 lifecycle import failed",
1478+
);
1479+
// Restart token must be cleared so future SIGUSR1 restarts are not
1480+
// permanently coalesced as "already in-flight".
1481+
expect(markGatewaySigusr1RestartHandled).toHaveBeenCalled();
1482+
expect(close).not.toHaveBeenCalled();
1483+
expect(start).toHaveBeenCalledTimes(1);
1484+
1485+
sigterm();
1486+
await expect(exited).resolves.toBe(0);
1487+
});
1488+
});
14041489
});
14051490

14061491
describe("gateway discover routing helpers", () => {

‎src/cli/gateway-cli/run-loop.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,10 @@ export async function runGatewayLoop(params: {
716716
restartIntent?.reason,
717717
restartIntent ?? undefined,
718718
);
719-
})();
719+
})().catch((err: unknown) => {
720+
gatewayLog.error(`failed to handle SIGTERM: ${String(err)}`);
721+
request("stop", "SIGTERM");
722+
});
720723
};
721724
const onSigint = () => {
722725
gatewayLog.info("signal SIGINT received");

0 commit comments

Comments
 (0)