diff --git a/e2e/console/daemon-start-failure.spec.ts b/e2e/console/daemon-start-failure.spec.ts new file mode 100644 index 000000000..5dafbdf48 --- /dev/null +++ b/e2e/console/daemon-start-failure.spec.ts @@ -0,0 +1,63 @@ +import { createServer } from "node:net"; + +import { expect, startDaemon, test } from "./fixtures.js"; + +/** + * why: reproduces #673. A failed daemon start reports only what the CLI guessed at while it polled + * for a socket (`Timed out starting simlock daemon: connect ENOENT /daemon.sock`, or + * `Daemon connection closed` once the daemon answered and then tore itself down), never the reason + * it wrote to `daemon.log` -- and `startDaemon`'s catch deletes that log with the rest of the home, + * so the cause cannot be read afterwards either. The conflict below is the EADDRINUSE family of + * #553, staged by holding the HTTP port the daemon is about to bind. + */ + +function reported(error: unknown): string { + if (!(error instanceof Error)) return String(error); + return error.cause === undefined + ? error.message + : `${error.message}\ncause: ${String(error.cause)}`; +} + +async function hold(port: number): Promise<() => Promise> { + const holder = createServer(); + await new Promise((resolve, reject) => { + holder.once("error", (error: Error) => + reject(new Error(`premise: could not hold port ${String(port)}: ${error.message}`)), + ); + holder.listen(port, "127.0.0.1", () => resolve()); + }); + return () => new Promise((resolve) => holder.close(() => resolve())); +} + +test("a daemon start that cannot bind its HTTP port reports the port conflict", async () => { + const daemon = await startDaemon("worker"); + try { + // why: restarting on its own home and port first shows that same start is accepted, so the + // refused one below can only be about the port this test comes to hold. + await daemon.stop(); + await daemon.start(); + + await daemon.stop(); + const release = await hold(daemon.port); + let failure: unknown; + let started = false; + try { + await daemon.start(); + started = true; + } catch (error: unknown) { + failure = error; + } finally { + await release(); + } + + if (started) { + throw new Error( + `premise: the daemon bound port ${String(daemon.port)}, which this test holds`, + ); + } + expect(reported(failure)).toContain("EADDRINUSE"); + expect(reported(failure)).toContain(String(daemon.port)); + } finally { + await daemon.dispose(); + } +}); diff --git a/e2e/console/fixtures.ts b/e2e/console/fixtures.ts index c6319be77..1cba00273 100644 --- a/e2e/console/fixtures.ts +++ b/e2e/console/fixtures.ts @@ -1,5 +1,5 @@ import { existsSync } from "node:fs"; -import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -107,7 +107,13 @@ export async function startDaemon( }; const start = async () => { const started = await cli(["daemon", "start"], env, { timeout: 30_000 }); - expect(started.code, started.stderr).toBe(0); + if (started.code === 0) return; + // why: a failed start reports only what the CLI saw while polling the socket, and `dispose` + // deletes this home with `daemon.log` — the only record of the cause the daemon itself wrote. + // Read the tail here, before any caller's catch reaches `dispose`, and fail with both. + const log = await readFile(join(home, "daemon.log"), "utf8").catch(() => ""); + const tail = log.split("\n").slice(-20).join("\n"); + expect(started.code, `daemon start failed: ${started.stderr}\n${tail}`).toBe(0); }; const dispose = async () => { for (const handle of background) handle.kill("SIGTERM");