fix(hopper): enforce caller-supplied request deadlines (#1119)

* fix(hopper): enforce caller-supplied request deadlines

* fix(hopper): correct idle deadline state and name timeout bound

Zero-ms deadlines never enter the FIFO, so report providerState
not_started instead of busy; the busy wording tells callers to wait
for an active request that does not exist. Name the Node timer
ceiling once and reuse it in validation and the error message.

* fix(hopper): enforce elapsed deadlines before dispatch and replies

* fix(hopper): distinguish unsent requests in timeout diagnostics

* test(hopper): exercise request deadlines against native Hopper

---------

Co-authored-by: Vilmer Frost <frost.vilmer@gmail.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
vilmerfrost
2026-10-09 06:52:07 +08:00
committed by GitHub
co-authored by Vilmer Frost morluto
parent 329cf206d1
commit e8939ca220
7 changed files with 688 additions and 4 deletions
+5
View File
@@ -405,6 +405,8 @@ export class HopperClient {
arguments_: Readonly<Record<string, JsonValue>> = {},
options: {
readonly signal?: AbortSignal;
/** Deadline after startup, including waiting for Hopper's serial bridge. */
readonly timeoutMs?: number;
readonly progress?: ProgressReporter;
} = {},
): Promise<Result<JsonValue, AnalysisError>> {
@@ -638,6 +640,9 @@ export class HopperClient {
const startedAt = performance.now();
const result = await this.#requests.run(id, method, params, {
...(options.signal === undefined ? {} : { signal: options.signal }),
...(options.timeoutMs === undefined
? {}
: { timeoutMs: options.timeoutMs }),
...(options.progress !== undefined ? { progress: options.progress } : {}),
});
this.#retainRequestHealth(result, id, method);
+300
View File
@@ -0,0 +1,300 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import {
HopperCancelledError,
HopperProtocolError,
} from "../domain/hopperErrors.js";
import { ok } from "../domain/result.js";
import { HopperRequestQueue } from "./HopperRequestQueue.js";
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
describe("HopperRequestQueue deadlines", () => {
it("times out active callers while retaining the wire until the late reply", async () => {
vi.useFakeTimers();
const sent: number[] = [];
const queue = new HopperRequestQueue(({ id }) => sent.push(id));
const result = vi.fn();
const pending = queue
.run(1, "analyze_function", {}, { timeoutMs: 25 })
.then(result);
const next = queue.run(2, "echo", {}, {});
await vi.advanceTimersByTimeAsync(25);
expect(result).toHaveBeenCalledWith({
ok: false,
error: expect.objectContaining({
_tag: "HopperTimeoutError",
timeoutMs: 25,
operation: "analyze_function",
requestId: 1,
providerState: "busy",
}),
});
expect(sent).toEqual([1]);
expect(queue.activity()).toMatchObject({
callerState: "cancelled",
queuedRequests: 1,
});
expect(vi.getTimerCount()).toBe(0);
expect(queue.accept(1, ok("late"))).toBe(true);
expect(sent).toEqual([1, 2]);
queue.accept(2, ok("next"));
await pending;
await expect(next).resolves.toEqual(ok("next"));
expect(result).toHaveBeenCalledTimes(1);
expect(queue.activity()).toBeNull();
expect(vi.getTimerCount()).toBe(0);
});
it("expires a queued request without transmitting it", async () => {
vi.useFakeTimers();
const sent: number[] = [];
const queue = new HopperRequestQueue(({ id }) => sent.push(id));
const active = queue.run(1, "active", {}, {});
const settled = vi.fn();
const queued = queue.run(2, "queued", {}, { timeoutMs: 10 }).then(settled);
await vi.advanceTimersByTimeAsync(10);
expect(settled).toHaveBeenCalledWith(
expect.objectContaining({
ok: false,
error: expect.objectContaining({
_tag: "HopperTimeoutError",
requestId: 2,
providerState: "not_started",
}),
}),
);
expect(queue.hasQueued(2)).toBe(false);
queue.accept(1, ok(null));
await Promise.all([active, queued]);
expect(sent).toEqual([1]);
expect(vi.getTimerCount()).toBe(0);
});
it("cleans deadline and abort observers on success before the deadline", async () => {
vi.useFakeTimers();
const queue = new HopperRequestQueue(() => {});
const controller = new AbortController();
const remove = vi.spyOn(controller.signal, "removeEventListener");
const settled = vi.fn();
const pending = queue
.run(1, "echo", {}, { timeoutMs: 25, signal: controller.signal })
.then(settled);
queue.accept(1, ok(42));
controller.abort();
await vi.advanceTimersByTimeAsync(100);
await pending;
expect(settled).toHaveBeenCalledExactlyOnceWith(ok(42));
expect(remove).toHaveBeenCalledWith("abort", expect.any(Function));
expect(vi.getTimerCount()).toBe(0);
});
it("keeps explicit cancellation distinct from a later timeout", async () => {
vi.useFakeTimers();
const queue = new HopperRequestQueue(() => {});
const controller = new AbortController();
const pending = queue.run(
1,
"echo",
{},
{ timeoutMs: 25, signal: controller.signal },
);
controller.abort();
await expect(pending).resolves.toEqual({
ok: false,
error: new HopperCancelledError(),
});
expect(vi.getTimerCount()).toBe(0);
queue.accept(1, ok(null));
await vi.advanceTimersByTimeAsync(100);
expect(queue.activity()).toBeNull();
});
it("does not recreate timers when a progress observer cancels synchronously", async () => {
vi.useFakeTimers();
const send = vi.fn();
const queue = new HopperRequestQueue(send);
const controller = new AbortController();
const pending = queue.run(
1,
"echo",
{},
{
timeoutMs: 100,
signal: controller.signal,
progress: {
report: () => {
controller.abort();
return Promise.resolve();
},
},
},
);
await expect(pending).resolves.toMatchObject({
ok: false,
error: { _tag: "HopperCancelledError" },
});
expect(vi.getTimerCount()).toBe(0);
expect(send).not.toHaveBeenCalled();
expect(queue.activity()).toBeNull();
const next = queue.run(2, "next", {}, {});
expect(send).toHaveBeenCalledExactlyOnceWith(
{ id: 2, method: "next", params: {} },
expect.any(Function),
);
queue.accept(2, ok(null));
await next;
});
it("cleans all deadlines on teardown, including after an active timeout", async () => {
vi.useFakeTimers();
const queue = new HopperRequestQueue(() => {});
const result = vi.fn();
const active = queue.run(1, "active", {}, { timeoutMs: 10 }).then(result);
const queued = queue.run(2, "queued", {}, { timeoutMs: 100 });
await vi.advanceTimersByTimeAsync(10);
const failure = new HopperProtocolError("closed");
queue.failAll(failure);
await active;
await expect(queued).resolves.toEqual({ ok: false, error: failure });
expect(result).toHaveBeenCalledTimes(1);
expect(queue.activity()).toBeNull();
expect(queue.queuedCount()).toBe(0);
expect(vi.getTimerCount()).toBe(0);
});
});
describe("HopperRequestQueue deadline admission and races", () => {
it("removes queued cancelled work and honours cancellation before admission", async () => {
vi.useFakeTimers();
const sent: number[] = [];
const queue = new HopperRequestQueue(({ id }) => sent.push(id));
const active = queue.run(1, "active", {}, {});
const controller = new AbortController();
const queued = queue.run(
2,
"queued",
{},
{ timeoutMs: 100, signal: controller.signal },
);
controller.abort();
await expect(queued).resolves.toMatchObject({
ok: false,
error: { _tag: "HopperCancelledError" },
});
await expect(
queue.run(
3,
"pre-aborted",
{},
{ timeoutMs: 100, signal: controller.signal },
),
).resolves.toMatchObject({
ok: false,
error: { _tag: "HopperCancelledError" },
});
expect(queue.queuedCount()).toBe(0);
queue.accept(1, ok(null));
await active;
expect(sent).toEqual([1]);
expect(vi.getTimerCount()).toBe(0);
});
it.each([NaN, Infinity, -1, 0.5, 2_147_483_648])(
"rejects invalid deadline %s without sending",
async (timeoutMs) => {
vi.useFakeTimers();
const send = vi.fn();
const queue = new HopperRequestQueue(send);
await expect(
queue.run(1, "echo", {}, { timeoutMs }),
).resolves.toMatchObject({
ok: false,
error: { _tag: "HopperProtocolError" },
});
expect(send).not.toHaveBeenCalled();
expect(vi.getTimerCount()).toBe(0);
},
);
it("expires zero deadlines without sending and settles only once if aborted afterward", async () => {
vi.useFakeTimers();
const send = vi.fn();
const queue = new HopperRequestQueue(send);
await expect(
queue.run(1, "echo", {}, { timeoutMs: 0 }),
).resolves.toMatchObject({
ok: false,
error: {
_tag: "HopperTimeoutError",
timeoutMs: 0,
providerState: "not_started",
},
});
expect(send).not.toHaveBeenCalled();
const controller = new AbortController();
const settled = vi.fn();
const pending = queue
.run(2, "echo", {}, { timeoutMs: 10, signal: controller.signal })
.then(settled);
await vi.advanceTimersByTimeAsync(10);
controller.abort();
queue.accept(2, ok("late"));
await pending;
expect(settled).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
ok: false,
error: expect.objectContaining({ _tag: "HopperTimeoutError" }),
}),
);
expect(vi.getTimerCount()).toBe(0);
});
});
describe("Hopper deadlines before timer dispatch", () => {
it("does not transmit overdue queued work when the event loop delays its timer", async () => {
vi.useFakeTimers();
const clock = vi.spyOn(performance, "now").mockReturnValue(0);
const sent: number[] = [];
const queue = new HopperRequestQueue(({ id }) => sent.push(id));
const active = queue.run(1, "active", {}, {});
const expired = queue.run(2, "set_comment", {}, { timeoutMs: 10 });
clock.mockReturnValue(20);
queue.accept(1, ok(null));
await expect(expired).resolves.toMatchObject({
ok: false,
error: {
_tag: "HopperTimeoutError",
operation: "set_comment",
providerState: "not_started",
},
});
await active;
expect(sent).toEqual([1]);
expect(queue.activity()).toBeNull();
expect(vi.getTimerCount()).toBe(0);
});
it("accepts an overdue active reply without turning elapsed timeout into success", async () => {
vi.useFakeTimers();
const clock = vi.spyOn(performance, "now").mockReturnValue(0);
const sent: number[] = [];
const queue = new HopperRequestQueue(({ id }) => sent.push(id));
const expired = queue.run(1, "active", {}, { timeoutMs: 10 });
const next = queue.run(2, "next", {}, {});
clock.mockReturnValue(20);
expect(queue.accept(1, ok("late"))).toBe(true);
await expect(expired).resolves.toMatchObject({
ok: false,
error: { _tag: "HopperTimeoutError" },
});
expect(sent).toEqual([1, 2]);
queue.accept(2, ok(null));
await next;
expect(queue.activity()).toBeNull();
expect(vi.getTimerCount()).toBe(0);
});
});
+96 -4
View File
@@ -10,14 +10,20 @@ import {
HopperProcessError,
HopperProtocolError,
HopperRemoteError,
HopperTimeoutError,
} from "../domain/hopperErrors.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { err, type Result } from "../domain/result.js";
export type HopperRequestResult = Result<JsonValue, HopperError>;
/** Node timer ceiling; larger deadlines would be clamped instead of honored. */
const MAX_TIMEOUT_MS = 2_147_483_647;
export interface HopperRequestQueueOptions {
readonly signal?: AbortSignal;
/** Caller deadline from admission, including time waiting in the FIFO. */
readonly timeoutMs?: number;
readonly progress?: ProgressReporter;
}
@@ -48,8 +54,12 @@ interface QueuedRequest {
readonly onAbort: (() => void) | undefined;
callerState: "waiting" | "cancelled";
callerSettled: boolean;
transmitted: boolean;
startedAt: number | undefined;
heartbeat: NodeJS.Timeout | undefined;
deadline: NodeJS.Timeout | undefined;
readonly deadlineAt: number | undefined;
readonly timeoutMs: number | undefined;
}
/** FIFO that keeps the wire serialized until Hopper actually replies. */
@@ -59,7 +69,7 @@ export class HopperRequestQueue {
constructor(private readonly send: RequestSender) {}
/** Queue one request until Hopper replies or the caller cancels it. */
/** Settle the caller on reply, cancellation, or deadline; retain active wire work. */
run(
id: number,
method: string,
@@ -68,6 +78,32 @@ export class HopperRequestQueue {
): Promise<HopperRequestResult> {
if (options.signal?.aborted === true)
return Promise.resolve(err(new HopperCancelledError()));
const timeoutMs = options.timeoutMs;
if (
timeoutMs !== undefined &&
(!Number.isInteger(timeoutMs) ||
timeoutMs < 0 ||
timeoutMs > MAX_TIMEOUT_MS)
)
return Promise.resolve(
err(
new HopperProtocolError(
`Hopper timeoutMs must be an integer between 0 and ${String(MAX_TIMEOUT_MS)}`,
),
),
);
if (timeoutMs === 0)
return Promise.resolve(
err(
new HopperTimeoutError(
0,
method,
id,
"not_started",
hopperOperationStage(method),
),
),
);
return new Promise((resolve) => {
let entry: QueuedRequest;
const onAbort =
@@ -82,12 +118,22 @@ export class HopperRequestQueue {
onAbort,
callerState: "waiting",
callerSettled: false,
transmitted: false,
startedAt: undefined,
heartbeat: undefined,
deadline: undefined,
deadlineAt:
timeoutMs === undefined ? undefined : performance.now() + timeoutMs,
timeoutMs,
};
this.#queue.push(entry);
if (onAbort !== undefined)
options.signal?.addEventListener("abort", onAbort, { once: true });
if (timeoutMs !== undefined)
entry.deadline = setTimeout(
() => this.#cancel(entry, this.#timeoutError(entry, timeoutMs)),
timeoutMs,
);
if (this.#active !== undefined || this.#queue.length > 1)
this.#report(
entry,
@@ -101,6 +147,7 @@ export class HopperRequestQueue {
accept(id: number, result: HopperRequestResult): boolean {
const entry = this.#active;
if (entry === undefined || entry.id !== id) return false;
this.#expireIfElapsed(entry);
this.#releaseWire(entry);
if (!entry.callerSettled)
this.#settleCaller(
@@ -187,6 +234,10 @@ export class HopperRequestQueue {
if (this.#active !== undefined) return;
const entry = this.#queue.shift();
if (entry === undefined) return;
if (this.#expireIfElapsed(entry)) {
this.#drain();
return;
}
if (entry.signal?.aborted === true) {
this.#settleCaller(entry, err(new HopperCancelledError()), "cancelled");
this.#drain();
@@ -195,6 +246,17 @@ export class HopperRequestQueue {
this.#active = entry;
entry.startedAt = performance.now();
this.#report(entry, `${entry.method} started on Hopper's serial bridge`);
this.#expireIfElapsed(entry);
// A progress observer may cancel or tear down synchronously before send.
// No native work exists yet, so it is safe to release this reserved slot.
if (this.#active !== entry || entry.callerSettled) {
if (this.#active === entry) {
this.#active = undefined;
this.#releaseWire(entry);
this.#drain();
}
return;
}
entry.heartbeat = setInterval(() => {
if (entry.callerSettled || entry.startedAt === undefined) return;
const elapsed = Math.round(performance.now() - entry.startedAt);
@@ -204,6 +266,7 @@ export class HopperRequestQueue {
);
}, 1_000);
try {
entry.transmitted = true;
this.send(
{ id: entry.id, method: entry.method, params: entry.params },
() =>
@@ -229,16 +292,43 @@ export class HopperRequestQueue {
}
}
#cancel(entry: QueuedRequest): void {
#timeoutError(entry: QueuedRequest, timeoutMs: number): HopperTimeoutError {
return new HopperTimeoutError(
timeoutMs,
entry.method,
entry.id,
entry.transmitted ? "busy" : "not_started",
hopperOperationStage(entry.method),
);
}
#expireIfElapsed(entry: QueuedRequest): boolean {
if (
entry.deadlineAt !== undefined &&
entry.timeoutMs !== undefined &&
performance.now() >= entry.deadlineAt
)
this.#settleCaller(
entry,
err(this.#timeoutError(entry, entry.timeoutMs)),
"cancelled",
);
return entry.callerSettled;
}
#cancel(
entry: QueuedRequest,
error: HopperError = new HopperCancelledError(),
): void {
if (entry.callerSettled) return;
if (this.#active === entry) {
this.#settleCaller(entry, err(new HopperCancelledError()), "cancelled");
this.#settleCaller(entry, err(error), "cancelled");
return;
}
const index = this.#queue.indexOf(entry);
if (index < 0) return;
this.#queue.splice(index, 1);
this.#settleCaller(entry, err(new HopperCancelledError()), "cancelled");
this.#settleCaller(entry, err(error), "cancelled");
}
#settleCaller(
@@ -249,6 +339,8 @@ export class HopperRequestQueue {
if (entry.callerSettled) return;
entry.callerSettled = true;
entry.callerState = callerState;
if (entry.deadline !== undefined) clearTimeout(entry.deadline);
entry.deadline = undefined;
if (this.#active === entry && entry.heartbeat !== undefined) {
clearInterval(entry.heartbeat);
entry.heartbeat = undefined;