Skip to content

Commit 8fcd61b

Browse files
committed
fix(gateway): avoid start-limit false positive for exit 78
1 parent 98d261f commit 8fcd61b

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎src/cli/daemon-cli/status.print.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,4 +756,36 @@ describe("printDaemonStatus", () => {
756756
expect(errors).toContain("systemd stopped restarting the gateway after repeated crashes");
757757
expect(errors).not.toContain("likely exited immediately");
758758
});
759+
760+
it("keeps the generic stopped message after a config exit (78) despite a stale restart count", () => {
761+
// RestartPreventExitStatus=78 stopped systemd deliberately; a leftover
762+
// NRestarts must not surface start-limit recovery guidance here.
763+
const platform = vi.spyOn(process, "platform", "get").mockReturnValue("linux");
764+
try {
765+
printDaemonStatus(
766+
{
767+
service: {
768+
label: "systemd",
769+
loaded: true,
770+
loadedText: "loaded",
771+
notLoadedText: "not loaded",
772+
runtime: {
773+
status: "stopped",
774+
state: "failed",
775+
lastExitStatus: 78,
776+
systemd: { result: "exit-code", nRestarts: 5, startLimitBurst: 5 },
777+
},
778+
},
779+
extraServices: [],
780+
},
781+
{ json: false },
782+
);
783+
} finally {
784+
platform.mockRestore();
785+
}
786+
787+
const errors = runtime.error.mock.calls.map(([line]) => line).join("\n");
788+
expect(errors).toContain("Service is loaded but not running (likely exited immediately).");
789+
expect(errors).not.toContain("systemd stopped restarting the gateway");
790+
});
759791
});

‎src/commands/doctor-format.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ describe("buildGatewayRuntimeHints", () => {
8989
expect(text).not.toContain("systemd stopped restarting the gateway");
9090
});
9191

92+
it("keeps the generic stopped hint after a config exit (78) despite a stale restart count", () => {
93+
// RestartPreventExitStatus=78 stopped systemd on purpose; the leftover
94+
// NRestarts must not flip the hint to start-limit recovery guidance.
95+
const text = buildGatewayRuntimeHints(
96+
{
97+
status: "stopped",
98+
state: "failed",
99+
lastExitStatus: 78,
100+
systemd: { result: "exit-code", nRestarts: 5, startLimitBurst: 5 },
101+
},
102+
{ platform: "linux", env: {} },
103+
).join("\n");
104+
105+
expect(text).toContain("likely exited immediately");
106+
expect(text).not.toContain("systemd stopped restarting the gateway");
107+
});
108+
92109
it("keeps the generic stopped hint for an ordinary cleanly-stopped service", () => {
93110
const text = buildGatewayRuntimeHints(
94111
{ status: "stopped", state: "inactive" },

‎src/daemon/service-runtime.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ describe("isSystemdStartLimitHit", () => {
1515
).toBe(true);
1616
});
1717

18+
it("detects a crash loop when the last exit was a non-config code", () => {
19+
// exit 1 is a normal crash, not the RestartPreventExitStatus=78 no-restart
20+
// path, so a counter that reached StartLimitBurst is real start-limit exhaustion.
21+
expect(
22+
isSystemdStartLimitHit({
23+
status: "stopped",
24+
state: "failed",
25+
lastExitStatus: 1,
26+
systemd: { result: "exit-code", nRestarts: 3, startLimitBurst: 3 },
27+
}),
28+
).toBe(true);
29+
});
30+
1831
it("detects Result=start-limit-hit even when restart counters are absent", () => {
1932
expect(
2033
isSystemdStartLimitHit({
@@ -25,6 +38,31 @@ describe("isSystemdStartLimitHit", () => {
2538
).toBe(true);
2639
});
2740

41+
it("does not counter-detect the deliberate EX_CONFIG (78) no-restart exit", () => {
42+
// RestartPreventExitStatus=78 stops systemd on purpose; the NRestarts left
43+
// over from earlier crashes is stale and must not read as start-limit exhaustion.
44+
expect(
45+
isSystemdStartLimitHit({
46+
status: "stopped",
47+
state: "failed",
48+
lastExitStatus: 78,
49+
systemd: { result: "exit-code", nRestarts: 5, startLimitBurst: 5 },
50+
}),
51+
).toBe(false);
52+
});
53+
54+
it("keeps Result=start-limit-hit authoritative even after a config (78) exit", () => {
55+
// The explicit systemd give-up signal wins regardless of the last exit code.
56+
expect(
57+
isSystemdStartLimitHit({
58+
status: "stopped",
59+
state: "failed",
60+
lastExitStatus: 78,
61+
systemd: { result: "start-limit-hit", nRestarts: 5, startLimitBurst: 5 },
62+
}),
63+
).toBe(true);
64+
});
65+
2866
it("does not flag a single failed exit below the start limit", () => {
2967
expect(
3068
isSystemdStartLimitHit({

‎src/daemon/service-runtime.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ export type GatewayServiceRuntime = {
3535
export const SYSTEMD_TASKS_CURRENT_WARNING_THRESHOLD = 200;
3636
export const SYSTEMD_MEMORY_CURRENT_WARNING_BYTES = 2 * 1024 * 1024 * 1024;
3737

38+
// EX_CONFIG (78) from sysexits.h. The generated systemd unit pins
39+
// RestartPreventExitStatus=78 (see systemd-unit.ts) so the gateway's
40+
// config-error / duplicate-lock exit (gateway-cli run) deliberately stops
41+
// without a restart. A last exit of 78 therefore means systemd gave up on
42+
// purpose, not that it exhausted StartLimitBurst, so any accumulated NRestarts
43+
// is stale from earlier crashes and must not drive start-limit detection.
44+
const SYSTEMD_NO_RESTART_EXIT_STATUS = 78;
45+
3846
export function isRiskySystemdKillMode(value: string | undefined): boolean {
3947
const normalized = normalizeLowercaseStringOrEmpty(value);
4048
return normalized === "process" || normalized === "none";
@@ -103,6 +111,12 @@ export function isSystemdCgroupHygieneRisk(runtime?: GatewayServiceSystemdRuntim
103111
* common one: once the gateway process has actually run and exited non-zero,
104112
* systemd keeps Result=exit-code and never overwrites it with start-limit-hit
105113
* (verified against systemd 249), so Result alone misses real crash loops.
114+
*
115+
* The counter path is guarded against the deliberate no-restart exit: a last
116+
* exit of 78 (EX_CONFIG, held back by RestartPreventExitStatus=78) means
117+
* systemd stopped on purpose, so a stale NRestarts left over from earlier
118+
* crashes must not be mistaken for start-limit exhaustion. The explicit
119+
* Result=start-limit-hit signal stays authoritative regardless of exit status.
106120
*/
107121
export function isSystemdStartLimitHit(runtime?: GatewayServiceRuntime): boolean {
108122
if (!runtime || normalizeLowercaseStringOrEmpty(runtime.state) !== "failed") {
@@ -115,6 +129,9 @@ export function isSystemdStartLimitHit(runtime?: GatewayServiceRuntime): boolean
115129
if (normalizeLowercaseStringOrEmpty(systemd.result) === "start-limit-hit") {
116130
return true;
117131
}
132+
if (runtime.lastExitStatus === SYSTEMD_NO_RESTART_EXIT_STATUS) {
133+
return false;
134+
}
118135
return (
119136
typeof systemd.startLimitBurst === "number" &&
120137
systemd.startLimitBurst > 0 &&

0 commit comments

Comments
 (0)