Skip to content
Merged
Show file tree
Hide file tree
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
63 changes: 63 additions & 0 deletions e2e/console/daemon-start-failure.spec.ts
Original file line number Diff line number Diff line change
@@ -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 <home>/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<void>> {
const holder = createServer();
await new Promise<void>((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<void>((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();
}
});
10 changes: 8 additions & 2 deletions e2e/console/fixtures.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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");
Expand Down
Loading