fix(browser): retain startup owners and coordinate retryable close

Validation: integrated changes passed formatting, typecheck, lint, resolved module-boundary verification and git diff --check. Tests and real-provider workflows were not run.
This commit is contained in:
morluto
2026-10-11 00:03:22 +08:00
parent d1235fadc9
commit 2a66ec9094
7 changed files with 184 additions and 81 deletions
+32 -8
View File
@@ -42,6 +42,7 @@ export class PlaywrightScenarioBrowserCleanupOwner {
#profileRemoved: boolean;
#closePromise: Promise<void> | undefined;
#eventFinalizationPromise: Promise<void> | undefined;
#eventFinalizationFailureReported = false;
/** Keep the narrow provider actions needed to release one opened browser. */
constructor(private readonly resources: PlaywrightScenarioCleanupResources) {
@@ -97,7 +98,10 @@ export class PlaywrightScenarioBrowserCleanupOwner {
try {
await eventFinalization;
} catch (cause: unknown) {
failures.push(cause);
if (!this.#eventFinalizationFailureReported) {
failures.push(cause);
this.#eventFinalizationFailureReported = true;
}
}
if (!this.#browserClosed) {
try {
@@ -380,6 +384,7 @@ const openAttachedScenarioBrowser = async (
environment: BrowserScenario["environment"],
launcher: ScenarioBrowserLauncher,
signal: AbortSignal | undefined,
retainCleanup: ((close: () => Promise<unknown>) => void) | undefined,
): Promise<OpenedScenarioBrowser> => {
const lease = await acquireAttachedScenario(browserRequest, signal);
let cleanupOwner: PlaywrightScenarioBrowserCleanupOwner | undefined;
@@ -431,7 +436,13 @@ const openAttachedScenarioBrowser = async (
throw cause;
}
const cleanup = cleanupOwner;
return failBrowserScenarioOperation(() => cleanup.close(), cause);
const close = () => cleanup.close();
try {
return await failBrowserScenarioOperation(close, cause);
} catch (failure: unknown) {
if (failure !== cause) retainCleanup?.(close);
throw failure;
}
}
};
@@ -442,6 +453,7 @@ export const openPlaywrightScenarioBrowser = async (
options: {
readonly launcher?: ScenarioBrowserLauncher;
readonly signal?: AbortSignal | undefined;
readonly retainCleanup?: (close: () => Promise<unknown>) => void;
} = {},
): Promise<OpenedScenarioBrowser> => {
const launcher = options.launcher ?? chromium;
@@ -451,6 +463,7 @@ export const openPlaywrightScenarioBrowser = async (
scenario.environment,
launcher,
options.signal,
options.retainCleanup,
);
const profilePath = await mkdtemp(join(tmpdir(), "rea-browser-scenario-"));
@@ -478,11 +491,16 @@ export const openPlaywrightScenarioBrowser = async (
timeout: 0,
});
} catch (cause: unknown) {
return failBrowserScenarioOperation(
() => rm(profilePath, { recursive: true, force: true, maxRetries: 3 }),
cause,
["browser_profile"],
);
const cleanup = () =>
rm(profilePath, { recursive: true, force: true, maxRetries: 3 });
try {
return await failBrowserScenarioOperation(cleanup, cause, [
"browser_profile",
]);
} catch (failure: unknown) {
if (failure !== cause) options.retainCleanup?.(cleanup);
throw failure;
}
}
const cleanup = new PlaywrightScenarioBrowserCleanupOwner({
closeBrowser: () => context.close(),
@@ -502,6 +520,12 @@ export const openPlaywrightScenarioBrowser = async (
cleanup,
};
} catch (cause: unknown) {
return failBrowserScenarioOperation(() => cleanup.close(), cause);
const close = () => cleanup.close();
try {
return await failBrowserScenarioOperation(close, cause);
} catch (failure: unknown) {
if (failure !== cause) options.retainCleanup?.(close);
throw failure;
}
}
};