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
2 changes: 1 addition & 1 deletion graphify-out/graph.json

Large diffs are not rendered by default.

58 changes: 40 additions & 18 deletions lib/hosts/daemon.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,35 @@ after(() => {
rmSync(scratch, { recursive: true, force: true });
});

// The fixture's flood and echoes sizes, shared with the tests that wait for the daemon to read them.
const FLOOD_LINES = 400;
const FLOOD_PAD = 100_000;
const ECHO_PAD = 6_500_000;
const ECHO_CHUNK = 262_144;
/** The flood's output in bytes, less the line envelope. Once all but a buffer's worth is dropped, the daemon has read
* it all but a few lines. */
const FLOOD_BYTES = FLOOD_LINES * FLOOD_PAD;
/** The echoes' base64 data in bytes, less the line envelope: never above what the daemon queues, so waiting for
* ECHOES_BYTES minus the buffer cannot hang. */
const ECHOES_BYTES = ["start", "end"].reduce((sum, id) => sum + Math.ceil(Buffer.byteLength(JSON.stringify({ type: `message_${id}`, pad: "z".repeat(ECHO_PAD) })) / 3) * 4, 0);

writeFakeOmp(fakeOmp, `if (process.argv.includes("--version")) { console.log("14.0.0-fake"); process.exit(0); }
require("node:fs").writeFileSync(${JSON.stringify(pidFile)}, String(process.pid));
require("node:readline").createInterface({ input: process.stdin }).on("line", (line) => {
const command = JSON.parse(line);
const { type } = command;
if (type === "state") process.stdout.write(JSON.stringify({ type: "response", id: "g1", command: "get_state", success: true, data: { sessionId: command.sessionId } }) + "\\n");
if (type === "flood") for (let i = 0; i < 400; i++) process.stdout.write(JSON.stringify({ type: "x", i, pad: "y".repeat(100_000) }) + "\\n");
if (type === "flood") for (let i = 0; i < ${FLOOD_LINES}; i++) process.stdout.write(JSON.stringify({ type: "x", i, pad: "y".repeat(${FLOOD_PAD}) }) + "\\n");
if (type === "many") for (let i = 0; i < 25000; i++) process.stdout.write("1\\n");
if (type === "parts") for (let i = 0; i < command.n; i++) process.stdout.write(JSON.stringify({ type: "p", i, pad: "q".repeat(400_000) }) + "\\n");
if (type === "cjk") for (let i = 0; i < 100; i++) process.stdout.write(JSON.stringify({ type: "c", i, text: "漢".repeat(10_000) }) + "\\n");
if (type === "huge") process.stdout.write(JSON.stringify({ type: "x", pad: "y".repeat(9 * 1024 * 1024) }) + "\\n");
if (type === "echo") process.stdout.write('{"type":"echoed"}\\n');
// Two frames of ~8.7 MB of rpc_chunk lines each, as omp echoes an image prompt (message_start, message_end).
if (type === "echoes") for (const id of ["start", "end"]) {
const bytes = Buffer.from(JSON.stringify({ type: "message_" + id, pad: "z".repeat(6_500_000) }));
const count = Math.ceil(bytes.length / 262144);
for (let index = 0; index < count; index++) process.stdout.write(JSON.stringify({ type: "rpc_chunk", chunkId: id, index, count, byteLength: bytes.length, data: bytes.subarray(index * 262144, (index + 1) * 262144).toString("base64") }) + "\\n");
const bytes = Buffer.from(JSON.stringify({ type: "message_" + id, pad: "z".repeat(${ECHO_PAD}) }));
const count = Math.ceil(bytes.length / ${ECHO_CHUNK});
for (let index = 0; index < count; index++) process.stdout.write(JSON.stringify({ type: "rpc_chunk", chunkId: id, index, count, byteLength: bytes.length, data: bytes.subarray(index * ${ECHO_CHUNK}, (index + 1) * ${ECHO_CHUNK}).toString("base64") }) + "\\n");
}
if (type === "ask") process.stdout.write('{"type":"extension_ui_request","id":"q1","method":"ask","questions":[]}\\n');
});
Expand Down Expand Up @@ -109,6 +121,14 @@ async function waitFor(predicate, what, timeoutMs = 15_000) {
}
}

/** Wait until the daemon has logged dropping at least `bytes` of a channel's backlog. A fixed sleep guesses how fast
* the daemon reads a child's output; a slow runner misses the overflow and the uplink resumes before it. */
async function waitForDropped(log, bytes, what = "the overflow") {
const dropped = () => log.lines.reduce((sum, line) => sum + Number(/dropped (\d+) bytes/.exec(line)?.[1] ?? 0), 0);
// Under the suite's 30 s per-test timeout, so this message names the stall rather than the runner's timeout.
await waitFor(() => dropped() >= bytes, what, 25_000);
}

/** A scripted main server. `gets` counts upgrade attempts; `refuse(n)` answers the nth with a status (null: upgrade);
* a plain GET (the daemon's diagnosis) gets the status the latest attempt got, or 426 after an upgrade, as the hub
* answers. A socket's first frame is its hello, answered `welcome`; later text frames are split into `acks` (their
Expand Down Expand Up @@ -534,12 +554,12 @@ test("after a channel overflow drops its backlog, bytesAhead counts only what is
await spawnChannel(server, "c1");
server.holdAcks = true;
server.send({ type: "stdin", ch: "c1", line: '{"type":"flood"}\n' });
// 20 MB of output: over the 8 MB channel buffer, so the daemon drops its backlog (twice).
await sleep(1_500);
// 40 MB of output: over the 32 MiB channel buffer, so the daemon drops its backlog once.
await waitForDropped(log, FLOOD_BYTES - LINK_LIMITS.channelBufferBytes);
server.holdAcks = false;
await waitFor(() => server.up.some((m) => m.type === "resync" && m.ch === "c1"), "resync");
// The rest of the flood is acked.
await sleep(300);
// The rest of the flood (the lines after the drop) arrives and is acked.
await waitFor(() => server.up.some((m) => m.type === "stdout" && m.ch === "c1" && m.line.includes(`"i":${FLOOD_LINES - 1},`)), "the last flood line", 25_000);
const before = Date.now();
server.send({ type: "stdin", ch: "c1", line: '{"type":"echo"}\n' });
await waitFor(() => sampled().some((s) => s.at >= before), "the echo's sample");
Expand Down Expand Up @@ -669,8 +689,8 @@ test("output beyond the channel buffer while the uplink is down is dropped, then
await spawnChannel(server, "c1");
uplink.paused = true;
server.send({ type: "stdin", ch: "c1", line: '{"type":"flood"}\n' });
// 40 MB of output, read by the daemon well within this pause.
await sleep(1_500);
// 40 MB of output: the daemon drops its backlog as it reads, however slowly.
await waitForDropped(log, FLOOD_BYTES - LINK_LIMITS.channelBufferBytes);
uplink.paused = false;
await waitFor(() => server.up.some((m) => m.type === "resync" && m.ch === "c1"), "resync");
const floodLines = server.up.filter((m) => m.type === "stdout" && m.ch === "c1").length;
Expand Down Expand Up @@ -706,17 +726,19 @@ function framesUp(messages) {
return frames;
}

/** The channel's messages main gets for the two echoes, written while the uplink is stalled. */
async function echoesWhileStalled(ch) {
/** The channel's messages main gets for the two echoes (~17 MB), written while the uplink is stalled. `log`: the
* buffer is smaller than the echoes, so wait for the daemon to log dropping all but a buffer's worth. Without it
* nothing observable says the daemon has read the output, so the stall is timed. */
async function echoesWhileStalled(ch, log) {
const server = scriptedServer();
const daemon = start(server);
try {
await waitFor(() => server.hellos.length === 1, "the hello");
await spawnChannel(server, ch);
uplink.paused = true;
server.send({ type: "stdin", ch, line: '{"type":"echoes"}\n' });
// ~17 MB of output, read by the daemon well within this pause.
await sleep(1_500);
if (log) await waitForDropped(log, ECHOES_BYTES - LINK_LIMITS.channelBufferBytes);
else await sleep(1_500);
uplink.paused = false;
// Settled: the last thing up is a resync or the second echo's last chunk.
await waitFor(() => {
Expand All @@ -737,7 +759,7 @@ test("an overflow drops the backlog mid chunk sequence: main's RpcProcess resets
// Smaller than one echo, so the backlog overflows twice over.
LINK_LIMITS.channelBufferBytes = 4 * 1024 * 1024;
try {
const messages = await echoesWhileStalled("drop");
const messages = await echoesWhileStalled("drop", log);
assert.ok(messages.some((m) => m.type === "resync"), "the overflow sent a resync");
const listeners = { line: [], resync: [], exit: [] };
const transport = {
Expand Down Expand Up @@ -1200,7 +1222,7 @@ test("an acked line sent in parts releases its bytes from the channel buffer onc
assert.equal(server.up.some((m) => m.type === "resync"), false);
uplink.paused = true;
server.send({ type: "stdin", ch: "c1", line: '{"type":"parts","n":30}\n' });
await sleep(1_000);
await waitForDropped(log, 1);
uplink.paused = false;
await waitFor(() => server.up.some((m) => m.type === "resync" && m.ch === "c1"), "the overflow's resync");
} finally {
Expand Down Expand Up @@ -1309,8 +1331,8 @@ test("a resend skips a channel's lines dropped by an overflow after the welcome"
try {
const { resent, unacked } = await stalledRedial(server, async () => {
server.send({ type: "stdin", ch: "c1", line: '{"type":"flood"}\n' });
// 40 MB of output, past the channel buffer, read by the daemon well within this pause.
await sleep(1_500);
// 40 MB of output, past the channel buffer: wait for the daemon to drop it.
await waitForDropped(log, FLOOD_BYTES - LINK_LIMITS.channelBufferBytes);
});
// The resend stalled after about three frames; the fourth frame's lines must not go again.
assert.ok(unacked - resent >= 2_000, `${resent} of ${unacked} lines resent`);
Expand Down
2 changes: 2 additions & 0 deletions lib/hosts/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ export function runHostDaemon(options: { url: string; token: string; allowHttp:
if (channel.buffered > LINK_LIMITS.channelBufferBytes) {
// The uplink lags too far: drop this child's backlog, a line part-way out included (main drops that partial
// line). The resync goes out ahead of the channel's later output.
// lib/hosts/daemon.test.mjs parses this line (`dropped N bytes`) to wait for the overflow.
log(`channel ${ch}: dropped ${channel.buffered} bytes of output the server has not taken; resync`);
sender.drop(ch);
for (const item of outbox.drop((item) => item.key === ch)) unackedBytes -= item.unacked;
resend = resend.slice(resendAt).filter((item) => item.key !== ch);
Expand Down
Loading