fix(browser): budget scenario cleanup for content drain and shutdown

This commit is contained in:
64579525
2026-10-13 10:00:52 +08:00
parent d3e315cf33
commit a74a599fce
5 changed files with 58 additions and 6 deletions
@@ -2,6 +2,14 @@ import {
AnalysisCancelledError,
AnalysisTimeoutError,
} from "../domain/analysisErrorCore.js";
import { WEB_RUNTIME_LIMITS } from "../domain/webRuntime.js";
export const PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS = 5_000;
// Cleanup drains bounded content reads before shutting down the browser and
// removing its profile. The outer deadline must cover both sequential stages.
export const PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS =
PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS + WEB_RUNTIME_LIMITS.cleanupTimeoutMs;
const OPERATION = "capture_browser_scenario" as const;
+31 -1
View File
@@ -2,6 +2,7 @@ import { afterEach, expect, it, vi } from "vitest";
import type { Browser } from "playwright-core";
import { browserScenarioSchema } from "../domain/browserScenario.js";
import { PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS } from "./PlaywrightExecutionBoundary.js";
import {
attachedScenarioLeaseKey,
openPlaywrightScenarioBrowser,
@@ -144,6 +145,35 @@ it("keeps attached-target ownership until cancelled cleanup actually settles", a
expect(settled).toEqual([true]);
});
it("waits for bounded event finalization and owned browser teardown", async () => {
vi.useFakeTimers();
try {
const closeBrowser = vi.fn(
() => new Promise<void>((resolve) => setTimeout(resolve, 1_500)),
);
const removeProfile = vi.fn(async () => undefined);
const cleanup = new PlaywrightScenarioBrowserCleanupOwner({
closeBrowser,
removeProfile,
});
const finished = cleanup
.close(() => new Promise<void>((resolve) => setTimeout(resolve, 4_500)))
.then(
() => ({ ok: true }),
(cause: unknown) => ({ ok: false, cause }),
);
await vi.advanceTimersByTimeAsync(4_500);
expect(removeProfile).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(1_500);
expect(await finished).toEqual({ ok: true });
expect(cleanup.browserClosed).toBe(true);
expect(closeBrowser).toHaveBeenCalledOnce();
expect(removeProfile).toHaveBeenCalledOnce();
} finally {
vi.useRealTimers();
}
});
it("keeps attached-target ownership after the cleanup response timeout", async () => {
vi.useFakeTimers();
try {
@@ -164,7 +194,7 @@ it("keeps attached-target ownership after the cleanup response timeout", async (
cleanup: { reason: expect.stringContaining("timed out") },
});
await closeStarted.promise;
await vi.advanceTimersByTimeAsync(1_000);
await vi.advanceTimersByTimeAsync(PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS);
await timeoutExpectation;
expect(settled).toEqual([]);
+9 -2
View File
@@ -8,7 +8,10 @@ import type { BrowserScenario } from "../domain/browserScenario.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import { isLiteralLoopbackHostname } from "../domain/browserObservation.js";
import { BrowserObservationError } from "../domain/browserObservationError.js";
import { withPlaywrightExecutionBoundary } from "./PlaywrightExecutionBoundary.js";
import {
PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS,
withPlaywrightExecutionBoundary,
} from "./PlaywrightExecutionBoundary.js";
const OPERATION = "capture_browser_scenario" as const;
@@ -71,7 +74,11 @@ export class PlaywrightScenarioBrowserCleanupOwner {
signal?: AbortSignal,
): Promise<void> {
try {
await withPlaywrightExecutionBoundary(() => cleanup, 1_000, signal);
await withPlaywrightExecutionBoundary(
() => cleanup,
PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS,
signal,
);
} catch (cause: unknown) {
throw new BrowserObservationError(OPERATION, "cleanup_failed", {
cause,
+8 -2
View File
@@ -14,7 +14,10 @@ import { performPlaywrightScenarioAction } from "./PlaywrightScenarioActions.js"
import { capturePlaywrightStepArtifacts } from "./PlaywrightScenarioArtifacts.js";
import { PlaywrightScenarioEvents } from "./PlaywrightScenarioEvents.js";
import { PlaywrightScenarioStorage } from "./PlaywrightScenarioStorage.js";
import { withPlaywrightExecutionBoundary } from "./PlaywrightExecutionBoundary.js";
import {
PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS,
withPlaywrightExecutionBoundary,
} from "./PlaywrightExecutionBoundary.js";
import {
browserScenarioCaptureData,
browserScenarioOperationFailure,
@@ -154,7 +157,10 @@ export class PlaywrightScenarioSession implements BrowserScenarioSessionPort {
() => undefined,
);
options.retainCleanup?.(() =>
withPlaywrightExecutionBoundary(lateCleanup, 1_000),
withPlaywrightExecutionBoundary(
lateCleanup,
PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS,
),
);
void lateCleanup().catch((cleanupCause: unknown) => {
// best-effort cleanup: retainCleanup keeps the retry capability.
@@ -4,6 +4,7 @@ import type {
} from "../../domain/browserNetworkEvidence.js";
import type { BrowserScenarioEvent } from "../../domain/browserScenarioCaptureValues.js";
import type { BrowserScenarioSecrets } from "../BrowserScenarioSecrets.js";
import { PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS } from "../PlaywrightExecutionBoundary.js";
import {
captureNetworkBody,
captureNetworkHeaders,
@@ -76,7 +77,7 @@ export class PlaywrightScenarioNetwork {
constructor(private readonly options: NetworkCaptureOptions) {
this.readOptions = {
timeoutMs: options.readTimeoutMs ?? 5_000,
timeoutMs: options.readTimeoutMs ?? PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS,
...(options.signal === undefined ? {} : { signal: options.signal }),
};
}