diff --git a/src/javascript/sourceMaps/SourceMapDecoder.test.ts b/src/javascript/sourceMaps/SourceMapDecoder.test.ts index d555ae05d8..326bf4e9e0 100644 --- a/src/javascript/sourceMaps/SourceMapDecoder.test.ts +++ b/src/javascript/sourceMaps/SourceMapDecoder.test.ts @@ -1,4 +1,5 @@ import { access } from "node:fs/promises"; +import { setImmediate as nextTurn } from "node:timers/promises"; import { expect, it } from "vitest"; import { ProviderCleanupError } from "../../domain/providerCleanupError.js"; import { DarwinProcessOwnershipInspectionError } from "../../process/DarwinProcessRunTokenReader.js"; @@ -18,6 +19,7 @@ import { webSourceLocationFixture, } from "../../../tests/fixtures/webSourceLocation.js"; import { releaseOwnedCleanup } from "../../../tests/support/cleanupRelease.js"; +import { createDeferred } from "../../../tests/fixtures/binarySession.js"; const fixture = webSourceLocationFixture(); const input = { @@ -39,6 +41,66 @@ it("preserves the exact caller reason before source-map admission", async () => expect(response.error.cause).toBe(reason); }); +it("cancels queued source-map work while the active request keeps its runtime", async () => { + const entered = createDeferred(); + const release = createDeferred(); + const roots: string[] = []; + const decoder = new SourceMapDecoder({ + async createRuntime() { + const root = await PrivateRuntimeRoot.create({ + prefix: "rea-source-map-queued-test-", + }); + roots.push(root.path); + if (roots.length === 1) { + entered.resolve(); + await release.promise; + } + return root; + }, + launcher: (options) => + spawnOwnedProviderProcess({ + ...options, + arguments: ["-e", "process.exit(7)"], + }), + }); + const active = decoder.trace(input); + try { + await entered.promise; + const controller = new AbortController(); + const reason = { caller: "cancel the waiting source-map request" }; + const queued = decoder.trace(input, { signal: controller.signal }); + const following = decoder.trace(input); + controller.abort(reason); + + const result = await Promise.race([ + queued, + nextTurn("still queued" as const), + ]); + expect(result).not.toBe("still queued"); + if (result === "still queued" || result.ok) + throw new Error("Expected cancellation before decoder admission"); + expect(result.error).toBeInstanceOf(AnalysisCancelledError); + expect(result.error.cause).toBe(reason); + expect(result.error.cleanupIncomplete).toBe(false); + expect(roots).toHaveLength(1); + await expect(access(roots[0]!)).resolves.toBeUndefined(); + + release.resolve(); + const exited = { + ok: false, + error: { _tag: "ProviderAdapterError", diagnostics: { exit_code: 7 } }, + }; + await expect(active).resolves.toMatchObject(exited); + await expect(following).resolves.toMatchObject(exited); + expect(roots).toHaveLength(2); + for (const path of roots) + await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); + } finally { + release.resolve(); + await decoder.close(); + } +}); + it("retains physical failure and late cancellation through cleanup failure and retry", async () => { const root = await PrivateRuntimeRoot.create({ prefix: "rea-source-map-cleanup-retry-test-", diff --git a/src/javascript/sourceMaps/SourceMapDecoder.ts b/src/javascript/sourceMaps/SourceMapDecoder.ts index 32874b5a67..0f934c06c0 100644 --- a/src/javascript/sourceMaps/SourceMapDecoder.ts +++ b/src/javascript/sourceMaps/SourceMapDecoder.ts @@ -26,6 +26,10 @@ import { type SourceMapDecoderDependencies, } from "./SourceMapCommand.js"; import { jsonParts } from "../../domain/jsonSerialization.js"; +import { + AbortedWait, + waitForAbortable, +} from "../../process/AbortablePromise.js"; const OPERATION = "trace_web_source_location"; /** Integrate the pinned upstream codec through an independently bounded owned process. */ @@ -37,22 +41,32 @@ export class SourceMapDecoder implements WebSourceMapPort { constructor(readonly dependencies: SourceMapDecoderDependencies = {}) {} /** Return complete point evidence after process and private-root cleanup. */ - trace( + async trace( input: Parameters[0], options?: ExecutionOptions, ): Promise> { if (this.#closing) - return Promise.resolve( - err( - new AnalysisCapabilityUnavailableError( - "source-map-decoder", - OPERATION, - "Provider is closing", - ), + return err( + new AnalysisCapabilityUnavailableError( + "source-map-decoder", + OPERATION, + "Provider is closing", ), ); - const operation = this.#tail.then(() => - this.#closing + const predecessor = this.#tail; + let releaseTurn!: () => void; + this.#tail = new Promise((resolve) => { + releaseTurn = resolve; + }); + const ready = await waitForAbortable(predecessor, options?.signal); + if (ready instanceof AbortedWait) { + // The caller can leave before admission, but later work still queues + // behind the request that owns the decoder and its cleanup. + void predecessor.then(releaseTurn); + return err(cancelledBy(OPERATION, ready)); + } + try { + return this.#closing ? err( new AnalysisCapabilityUnavailableError( "source-map-decoder", @@ -60,13 +74,10 @@ export class SourceMapDecoder implements WebSourceMapPort { "Provider is closing", ), ) - : this.#trace(input, options), - ); - this.#tail = operation.then( - () => undefined, - () => undefined, - ); - return operation; + : await this.#trace(input, options); + } finally { + releaseTurn(); + } } async #trace(