diff --git a/src/browser/IsolatedWebBundleAnalyzer.test.ts b/src/browser/IsolatedWebBundleAnalyzer.test.ts index cc130793867..23a701c95e2 100644 --- a/src/browser/IsolatedWebBundleAnalyzer.test.ts +++ b/src/browser/IsolatedWebBundleAnalyzer.test.ts @@ -141,6 +141,7 @@ it("stops after a middle-script result-capacity failure and retains prefix facts ]); const calls: string[] = []; const caller = new AbortController(); + const cancellation = new Error("later cancellation during final close"); const primary = new AnalysisResourceConstraintError( "analyze_web_bundle", "memory", @@ -158,7 +159,7 @@ it("stops after a middle-script result-capacity failure and retains prefix facts }); }, close: async () => { - caller.abort(new Error("later cancellation during final close")); + caller.abort(cancellation); }, })); expect((await analyzer.reserve()).ok).toBe(true); @@ -176,6 +177,12 @@ it("stops after a middle-script result-capacity failure and retains prefix facts ); if (result.ok) throw new Error("Expected result-capacity failure"); expect(result.error).toBe(primary); + expect(result.error.secondaryFailures).toContainEqual( + expect.objectContaining({ + _tag: "AnalysisCancelledError", + cause: cancellation, + }), + ); expect(result.error.partialObservation).toMatchObject({ completeness: { status: "partial", parsed_scripts: 1 }, observations: { @@ -354,7 +361,7 @@ it("shares one capture reservation when analyzers use one admission", async () = second.releaseReservation(); }); -it("does not fail one admission when another host's worker runtime is retained", async () => { +it("does not fail one admission's analysis when another host's worker runtime is retained", async () => { let fail = true; const stuck = new OwnedWorkerRuntime({ rootPrefix: "rea-web-host-isolation-", @@ -364,8 +371,25 @@ it("does not fail one admission when another host's worker runtime is retained", }); stuck.retainForCleanup(); try { - const analyzer = new IsolatedWebBundleAnalyzer(() => inertWorker()); + const analyzed: string[] = []; + const analyzer = new IsolatedWebBundleAnalyzer(() => ({ + analyze: async (script) => { + analyzed.push(script.script_key); + return ok(analyzeCapturedWebScript(script)); + }, + close: async () => {}, + })); expect((await analyzer.reserve()).ok).toBe(true); + const captured = inspection(["fetch('/independent');"]); + const result = await analyzer.analyze(captured, noSourceMaps, input); + expect(result.ok).toBe(true); + expect(analyzed).toEqual( + captured.scripts.items.map(({ script_key }) => script_key), + ); + if (result.ok) + expect(result.value.observations.endpoints).toMatchObject([ + { value: "/independent" }, + ]); analyzer.releaseReservation(); await expect(OwnedWorkerRuntime.retryPending()).rejects.toThrow( "other host still owns its worker", diff --git a/src/browser/IsolatedWebBundleAnalyzer.ts b/src/browser/IsolatedWebBundleAnalyzer.ts index 12930f6e411..c40576e2db6 100644 --- a/src/browser/IsolatedWebBundleAnalyzer.ts +++ b/src/browser/IsolatedWebBundleAnalyzer.ts @@ -19,6 +19,7 @@ import { AnalysisOutputError, } from "../domain/analysisErrorCore.js"; import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js"; +import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { err, ok, type Result } from "../domain/result.js"; import { ProviderStartupDeadline } from "../process/ProviderDeadline.js"; @@ -205,7 +206,7 @@ export class IsolatedWebBundleAnalyzer { }); }; try { - await WebBundleAnalysisWorker.retryPending(); + await this.#admission.workers.retry(); this.#worker = this.createWorker( input.max_heap_mb ?? DEFAULT_WEB_BUNDLE_HEAP_MB, ); @@ -302,17 +303,14 @@ export class IsolatedWebBundleAnalyzer { } finally { deadline.dispose(); } - if ( - !hasPrimaryFailure && - caller.aborted && - deadline.interruption !== "timeout" - ) { + if (caller.aborted && deadline.interruption !== "timeout") { const cancelled = new AnalysisCancelledError("analyze_web_bundle", { cause: caller.reason, partialObservation: partial ?? inspection, }); - failure = - failure?.cleanupIncomplete === true + failure = hasPrimaryFailure + ? analysisErrorWithCancellation(failure, cancelled) + : failure?.cleanupIncomplete === true ? analysisErrorWithCleanupFailure( cancelled, failure, diff --git a/src/browser/modules/NativeModuleResolver.test.ts b/src/browser/modules/NativeModuleResolver.test.ts index 1a11a15f9c5..b7aaa1aa2bb 100644 --- a/src/browser/modules/NativeModuleResolver.test.ts +++ b/src/browser/modules/NativeModuleResolver.test.ts @@ -105,6 +105,9 @@ describe("native module resolver cleanup ownership", () => { expect(result.error.cleanupResources).toEqual([]); if (!blocked.ok) expect(blocked.error.cleanupIncomplete).toBe(false); }); +}); + +describe("native module resolver shutdown admission", () => { it("awaits acquisition and cleanup while refusing queued work during shutdown", async () => { let acquired: () => void = () => { throw new Error("acquisition gate missing"); @@ -150,10 +153,25 @@ describe("native module resolver cleanup ownership", () => { await first; const blocked = await queued; expect(blocked.ok).toBe(false); + if (!blocked.ok) { + expect(blocked.error.cleanupIncomplete).toBe(false); + expect(blocked.error.cleanupResources).toEqual([]); + expect(projectAnalysisError(blocked.error).code).toBe( + "capability_unavailable", + ); + } await closing; expect(closed).toBe(true); expect([launches, closes]).toEqual([1, 1]); - expect((await provider.resolve(input)).ok).toBe(false); + const afterClose = await provider.resolve(input); + expect(afterClose.ok).toBe(false); + if (!afterClose.ok) { + expect(afterClose.error.cleanupIncomplete).toBe(false); + expect(afterClose.error.cleanupResources).toEqual([]); + expect(projectAnalysisError(afterClose.error).code).toBe( + "capability_unavailable", + ); + } }); }); @@ -375,6 +393,12 @@ it.each([ cause: physicalFailure, }); expect(projectAnalysisError(result.error).code).not.toBe("cancelled"); + expect(result.error.secondaryFailures).toContainEqual( + expect.objectContaining({ + _tag: "AnalysisCancelledError", + cause: reason, + }), + ); await provider.close(); expect(closes).toBe(stage === "launch" ? 0 : 1); }, diff --git a/src/browser/modules/NativeModuleResolver.ts b/src/browser/modules/NativeModuleResolver.ts index 0e2215f5427..58e29353706 100644 --- a/src/browser/modules/NativeModuleResolver.ts +++ b/src/browser/modules/NativeModuleResolver.ts @@ -16,6 +16,7 @@ import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import type { WebModuleResolutionPort } from "../../application/WebModulePorts.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js"; +import { analysisErrorWithCancellation } from "../../domain/analysisErrorCancellation.js"; import { AnalysisCancelledError, AnalysisCapabilityUnavailableError, @@ -283,12 +284,19 @@ export class NativeModuleResolver implements WebModuleResolutionPort { if (lease !== undefined) new AnalysisCleanupLeaseGroup(cleanupError, [lease]); } - if (result.ok && options?.signal?.aborted) + if (options?.signal?.aborted) result = err( - new AnalysisCancelledError(OPERATION, { - cause: options.signal.reason, - partialObservation: completedObservation(input, result.value), - }), + analysisErrorWithCancellation( + result.ok ? undefined : result.error, + new AnalysisCancelledError(OPERATION, { + cause: options.signal.reason, + ...(result.ok + ? { + partialObservation: completedObservation(input, result.value), + } + : {}), + }), + ), ); if (cleanupError !== undefined) return err( @@ -319,9 +327,11 @@ export class NativeModuleResolver implements WebModuleResolutionPort { } #closedError(): AnalysisError { - return new BrowserObservationError(OPERATION, "cleanup_failed", { - detail: "Native module resolver is closing.", - }); + return new AnalysisCapabilityUnavailableError( + "native-module-resolver", + OPERATION, + "Native module resolver is closing.", + ); } async #retryCleanup(): Promise {