From 911561985bdff0940e83e155c9c108d83bb1d089 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 13:27:04 +0900 Subject: [PATCH] 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 --- src/platform/execution/execution-context.ts | 23 +++++++++++++++---- .../execution/execution-context.test.ts | 22 +++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/platform/execution/execution-context.ts b/src/platform/execution/execution-context.ts index 84efd8f0..e60de32e 100644 --- a/src/platform/execution/execution-context.ts +++ b/src/platform/execution/execution-context.ts @@ -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(); + }, }); } diff --git a/tests/platform/execution/execution-context.test.ts b/tests/platform/execution/execution-context.test.ts index aabd421d..c0e611de 100644 --- a/tests/platform/execution/execution-context.test.ts +++ b/tests/platform/execution/execution-context.test.ts @@ -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) => {