Skip to content

Commit 357000c

Browse files
committed
fix(daemon): prefer stderr over stale stdout in gateway restart diagnostics
1 parent ef47dd6 commit 357000c

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/daemon/diagnostics.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,21 @@ describe("readLastGatewayErrorLine", () => {
3636
"gateway stdout current",
3737
);
3838
});
39+
40+
it("prefers the current stderr error over a stale stdout match on linux", async () => {
41+
const stateDir = makeTempStateDir();
42+
const homeDir = makeTempStateDir();
43+
const env = { HOME: homeDir, OPENCLAW_STATE_DIR: stateDir };
44+
const stateLogs = resolveGatewayLogPaths(env);
45+
fs.mkdirSync(stateLogs.logDir, { recursive: true });
46+
// stderr carries the real, current failure; stdout carries an older matching
47+
// line. On non-darwin platforms stderr is the strongest failure signal, so
48+
// it must win instead of the stale stdout match.
49+
fs.writeFileSync(stateLogs.stderrPath, "failed to bind gateway socket EADDRINUSE\n", "utf8");
50+
fs.writeFileSync(stateLogs.stdoutPath, "gateway start blocked: stale prior reason\n", "utf8");
51+
52+
await expect(readLastGatewayErrorLine(env, { platform: "linux" })).resolves.toBe(
53+
"failed to bind gateway socket EADDRINUSE",
54+
);
55+
});
3956
});

src/daemon/diagnostics.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ export async function readLastGatewayErrorLine(
4040
: resolveGatewayLogPaths(env);
4141
const stderrRaw = readStderr ? await fs.readFile(stderrPath, "utf8").catch(() => "") : "";
4242
const stdoutRaw = await fs.readFile(stdoutPath, "utf8").catch(() => "");
43-
const lines = [...stderrRaw.split(/\r?\n/), ...stdoutRaw.split(/\r?\n/)].map((line) =>
43+
// stderr is the strongest failure signal on non-darwin platforms, so place it
44+
// last and scan from the end: the most recent stderr error line then wins over
45+
// any (possibly stale) stdout match, matching the stderr-first fallback below.
46+
const lines = [...stdoutRaw.split(/\r?\n/), ...stderrRaw.split(/\r?\n/)].map((line) =>
4447
line.trim(),
4548
);
4649
for (let i = lines.length - 1; i >= 0; i -= 1) {

0 commit comments

Comments
 (0)