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
27 changes: 27 additions & 0 deletions lib/hosts/hub.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,33 @@ test("an op with no answer within opTimeoutMs rejects with 504 naming the host",
await down.cancel();
});

test("an op unanswered past slowOpWarnMs warns once naming the host and op, then still times out", async (t) => {
hub.linkTimings.opTimeoutMs = 200;
hub.linkTimings.slowOpWarnMs = 50;
const warn = t.mock.method(console, "warn", () => {});
const { remote, down } = await linkedHost("Slow box");
await assert.rejects(remote.sessionOps.listSessions(), (error) => error.status === 504);
const lines = warn.mock.calls.map((call) => String(call.arguments[0]));
assert.equal(lines.filter((line) => line.includes("Slow box") && line.includes("listSessions")).length, 1, "one slow-op warning");
assert.match(lines[0], /has not answered listSessions after \d+ms/);
await down.cancel();
});

test("an op answered before slowOpWarnMs logs no slow-op warning", async (t) => {
hub.linkTimings.opTimeoutMs = 200;
hub.linkTimings.slowOpWarnMs = 50;
const warn = t.mock.method(console, "warn", () => {});
const link = await linkedHost("Quick box");
const listing = link.remote.sessionOps.listSessions();
const req = await link.down.nextReal();
linkPost(link, [{ type: "res", seq: 1, id: req.id, ok: true, result: [] }], req.seq);
assert.deepEqual(await listing, []);
await sleep(100); // past the warn threshold: a leaked timer would have fired by now
const lines = warn.mock.calls.map((call) => String(call.arguments[0]));
assert.equal(lines.filter((line) => line.includes("Quick box")).length, 0, "no slow-op warning for a fast op");
await link.down.cancel();
});

test("an op that timed out is not resent after the daemon redials", async () => {
hub.linkTimings.opTimeoutMs = 30;
const link = await linkedHost("Lid closed");
Expand Down
17 changes: 16 additions & 1 deletion lib/hosts/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export const linkTimings = {
pingMs: LINK_LIMITS.pingMs,
offlineMs: LINK_LIMITS.offlineMs,
opTimeoutMs: LINK_LIMITS.opTimeoutMs,
/** A host op unanswered past this logs one warning naming the host and op, so the next slow op is diagnosable
* from the log alone; the warning changes nothing about the op, which still fails at its own timeout. */
slowOpWarnMs: 10_000,
};

/** What the hub keeps on each link socket (Bun's `ws.data`): the host its token named, and whether its hello came. */
Expand Down Expand Up @@ -107,6 +110,8 @@ interface PendingOp {
resolve: (value: unknown) => void;
reject: (error: Error) => void;
timer: NodeJS.Timeout;
/** Fires once while the op is still waiting, unless the op settles or fails first. */
warnTimer?: NodeJS.Timeout;
sessionId?: string | null;
recoveryId?: string;
/** Result bytes from the binary frames sent ahead of their `res`, in order (views into those frames). */
Expand Down Expand Up @@ -513,6 +518,7 @@ function failOp(link: Link, id: string, error: Error): void {
if (!pending) return;
link.pending.delete(id);
clearTimeout(pending.timer);
if (pending.warnTimer) clearTimeout(pending.warnTimer);
link.sender.drop(id);
released(link, link.outbox.drop((m) => m.key === id));
pending.reject(error);
Expand Down Expand Up @@ -1055,6 +1061,7 @@ function settle(link: Link, message: Res): void {
if (!pending) return;
link.pending.delete(message.id);
clearTimeout(pending.timer);
if (pending.warnTimer) clearTimeout(pending.warnTimer);
const label = labelOf(link.id);
if (!message.ok) {
const { status, code, message: text } = message.error;
Expand Down Expand Up @@ -1090,15 +1097,23 @@ function request(hostId: string, op: HostOpName, args: Record<string, unknown>,
const timer = setTimeout(() => failOp(link, id, new HostOpError(504, "host_timeout", `${label} did not answer ${op} in time`)), timeoutMs);
// A deadline, like the ping and offline timers: never what keeps the server running.
timer.unref?.();
// A slow op warns once, well before its deadline, so the next timeout is diagnosable from the log alone; the
// warning changes nothing about the op. Ops whose deadline is shorter than the warning never warn.
const startedAt = Date.now();
const warnTimer = timeoutMs > linkTimings.slowOpWarnMs ? setTimeout(() => {
console.warn(`[omp-web] ${label} has not answered ${op} after ${Date.now() - startedAt}ms`);
}, linkTimings.slowOpWarnMs) : undefined;
warnTimer?.unref?.();
// Accept runs synchronously while applying `res`, before following stdout in the same frame.
const pending: PendingOp = { op, resolve: (value) => {
try { resolve(accept ? accept(value) : value); } catch (error) { reject(error); }
}, reject, timer, blob: [], blobBytes: 0, ...(recoveryId ? { recoveryId } : {}), ...(op === "open" ? { sessionId: typeof args.sessionId === "string" ? args.sessionId : null } : {}) };
}, reject, timer, warnTimer, blob: [], blobBytes: 0, ...(recoveryId ? { recoveryId } : {}), ...(op === "open" ? { sessionId: typeof args.sessionId === "string" ? args.sessionId : null } : {}) };
link.pending.set(id, pending);
try {
sendDown(link, { type: "req", id, op, args }, id);
} catch (error) {
clearTimeout(timer);
if (warnTimer) clearTimeout(warnTimer);
link.pending.delete(id);
reject(error instanceof LinkLimitError ? new HostOpError(413, "host_request_too_large", `The request is too large for ${label}`) : error);
}
Expand Down
Loading