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
+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;