Files
rea/src/process/FileDeadline.ts
T
548fc2c811 fix(process): settle worker, scope, and capture retries against close (#14779)
* fix(process): settle worker, scope, and capture retries against close

- Return before posting a regex job that was cancelled before dispatch, and
  report a stop failure even when the match itself succeeded.
- Keep a worker runtime closed when start races a cleanup retry, and let
  sibling retries wait instead of reporting runtime_closing.
- Ignore owners a concurrent release settled while a scope drained.
- Check scope closure before waiting on a drain, and send SIGKILL once.
- Share one ASAR snapshot open across concurrent readers so a cancelled
  reader cannot close a peer's descriptor.
- Keep a verified staged-output identity across close retries, and stop the
  stdio transport from counting or reading after close or repeating its
  input end and onclose on retry.
- Clear a file deadline's timer when it ends.

* fix(artifacts): close a cancelled ASAR snapshot handle before publishing it

Snapshot acquisition is already serialized by the operation queue. The shared
open published the descriptor and then returned cancellation without closing
it, so the boundary test still saw a live fd. Close that handle before it is
published and drop the duplicate waiter state.

* fix(process): end the capture wait when a direct SIGKILL throws

The direct kill paths recorded killSent and then called unobservedExit, which
returned while finalization had not started. Settle that wait as unobserved
with the initiating reason and no exit code or finalization record, including
the zero-interval deadline path.

* fix(process): notify stdio close once after a natural server exit

Exit and output-close called onclose without the close flag, so a later
close() sent it again. Route exit, output close, and explicit close through
one notifier.

* test(process): cover post-acquisition abort and drain-time release

Abort after an idle worker is acquired and before dispatch, and fail a scope
owner during drain before a concurrent release succeeds. Those are the paths
the close-against-retry fixes change.

* fix(domain): remember sealed JSON identities weakly

The per-call completed set retained every finished row, so sealing an
object-heavy Evidence tree exceeded the ancestry budget. A WeakSet still
seals a shared subtree once, and the strong set stays the active ancestry.

* test(resources): release the drain gate if an assertion fails

The drain regression held owner2 until after its assertions. A failed
barrier left close waiting. Release the gate and join close in finally.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-26 18:19:27 +00:00

83 lines
2.6 KiB
TypeScript

/** How a wait for one file-system step ended. */
export type FileStepOutcome<Value> =
| { readonly status: "fulfilled"; readonly value: Value }
| { readonly status: "rejected"; readonly cause: unknown }
| {
readonly status: "interrupted";
readonly by: "cancellation" | "deadline";
};
/** One deadline and abort listener shared by every step of a file task. */
export interface FileDeadline {
/** How the deadline or cancellation ended the task, once one has. */
readonly ended: "cancellation" | "deadline" | undefined;
/**
* Start one step, unless the task has already ended, and wait for it until
* it settles or the task ends. A file-system call cannot be cancelled, so a
* step the task stops waiting for keeps running until its mount answers.
*/
readonly run: <Value>(
start: () => Promise<Value>,
) => Promise<FileStepOutcome<Value>>;
readonly release: () => void;
}
export const fileDeadline = (
signal: AbortSignal | undefined,
timeoutMs: number | undefined,
): FileDeadline => {
let ended: "cancellation" | "deadline" | undefined;
let timer: ReturnType<typeof setTimeout> | undefined;
let interrupt: ((by: "cancellation" | "deadline") => void) | undefined;
const interrupted = new Promise<"cancellation" | "deadline">((resolve) => {
interrupt = (by) => {
ended ??= by;
if (timer !== undefined) {
clearTimeout(timer);
timer = undefined;
}
resolve(by);
};
});
const cancel = (): void => interrupt?.("cancellation");
if (signal?.aborted === true) {
cancel();
} else {
if (timeoutMs !== undefined)
timer = setTimeout(() => interrupt?.("deadline"), timeoutMs);
signal?.addEventListener("abort", cancel, { once: true });
}
return {
get ended() {
return ended;
},
run: async <Value>(start: () => Promise<Value>) => {
if (ended !== undefined)
return { status: "interrupted", by: ended } as const;
let step: Promise<Value>;
try {
step = start();
} catch (cause) {
return { status: "rejected", cause } as const;
}
return Promise.race([
step.then(
(value): FileStepOutcome<Value> => ({ status: "fulfilled", value }),
(cause: unknown): FileStepOutcome<Value> => ({
status: "rejected",
cause,
}),
),
interrupted.then((by): FileStepOutcome<Value> => ({
status: "interrupted",
by,
})),
]);
},
release: () => {
if (timer !== undefined) clearTimeout(timer);
signal?.removeEventListener("abort", cancel);
},
};
};