fix(platform): let an interaction detach from its owner signal on completion (Q-6.9-7)
createExecutionContext forwarded an owner signal's abort and removed that listener only when the interaction itself aborted. An interaction that finished normally under a long-lived owner signal left its listener and AbortController on the owner. ExecutionContext gains complete(): idempotent, detaches from the owner without aborting the interaction. No caller passes an owner signal today, so this closes a latent leak. Verified: new test in tests/platform/execution/execution-context.test.ts (RED before: complete is not a function); all gates pass except the known tests/unit/removal-fixture.test.ts (artifacts/ absent). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b7c4e380e2
commit
911561985b
@@ -49,6 +49,13 @@ export interface ExecutionContext {
|
||||
readonly clock: () => number;
|
||||
/** Time left before the absolute interaction deadline; never negative. */
|
||||
remainingMs(): number;
|
||||
/**
|
||||
* Q-6.9-7. Ends the interaction normally: it detaches from the owner
|
||||
* `signal` without aborting its own. Idempotent. Without it, every
|
||||
* interaction under a long-lived owner signal left one abort listener (and
|
||||
* its controller) on that owner until the owner itself aborted.
|
||||
*/
|
||||
complete(): void;
|
||||
}
|
||||
|
||||
export type ExecutionContextOptions = Readonly<{
|
||||
@@ -95,17 +102,20 @@ export function createExecutionContext(
|
||||
|
||||
const controller = new AbortController();
|
||||
const parent = options.signal;
|
||||
let detachFromParent = () => {};
|
||||
if (parent) {
|
||||
if (parent.aborted) controller.abort();
|
||||
else {
|
||||
const forward = () => controller.abort();
|
||||
parent.addEventListener("abort", forward, { once: true });
|
||||
detachFromParent = () => {
|
||||
detachFromParent = () => {};
|
||||
parent.removeEventListener("abort", forward);
|
||||
};
|
||||
// The interaction must not keep the parent's listener alive after it ends.
|
||||
controller.signal.addEventListener(
|
||||
"abort",
|
||||
() => parent.removeEventListener("abort", forward),
|
||||
{ once: true },
|
||||
);
|
||||
controller.signal.addEventListener("abort", () => detachFromParent(), {
|
||||
once: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,5 +137,8 @@ export function createExecutionContext(
|
||||
return 0;
|
||||
}
|
||||
},
|
||||
complete() {
|
||||
detachFromParent();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createExecutionContext } from "../../../src/platform/execution/execution-context.ts";
|
||||
|
||||
@@ -69,6 +69,26 @@ describe("createExecutionContext", () => {
|
||||
expect(direct.cancellation.signal.aborted).toBe(true);
|
||||
});
|
||||
|
||||
it("detaches from a long-lived owner signal when the interaction completes (Q-6.9-7)", () => {
|
||||
const owner = new AbortController();
|
||||
const remove = vi.spyOn(owner.signal, "removeEventListener");
|
||||
const context = createExecutionContext({
|
||||
interactionId: "interaction-7",
|
||||
deadlineMs: 1_000,
|
||||
traffic: TRAFFIC,
|
||||
signal: owner.signal,
|
||||
});
|
||||
|
||||
context.complete();
|
||||
context.complete();
|
||||
expect(remove).toHaveBeenCalledOnce();
|
||||
expect(remove).toHaveBeenCalledWith("abort", expect.any(Function));
|
||||
|
||||
// A finished interaction is not cancelled by its owner afterwards.
|
||||
owner.abort();
|
||||
expect(context.cancellation.signal.aborted).toBe(false);
|
||||
});
|
||||
|
||||
it.each([0, -1, Number.POSITIVE_INFINITY, 60_001, 1.5])(
|
||||
"rejects an unbounded or invalid deadline %s",
|
||||
(deadlineMs) => {
|
||||
|
||||
Reference in New Issue
Block a user