Skip to content

Close Context leaves the context open when stopping the trace or saving coverage fails #5322

Open

Description

@aaltat

Describe the bug
closeContext in node/playwright-wrapper/playwright-state.ts stops tracing and saves the coverage reports before it closes the context:

if (traceFile && saveTrace) {
    await openBrowsers.getActiveContext()?.tracing.stop({ path: traceFile });
}
for (const page of activeBrowser.context?.pageStack || []) {
    await _saveCoverageReport(page);
}
await openBrowsers.getActiveContext()?.close();
activeBrowser.popContext();

If tracing.stop or _saveCoverageReport throws, the context is not closed and stays on the context stack. For a persistent context, the browser without a browser object is not closed either.

This affects Close Context and the automatic closing of contexts at the end of a test or suite, which use closeContext. Close Browser is not affected: BrowserState.close() stops tracing and closes each context inside one try.

The bug is older than #5318. After #5318 it also affects adopted contexts: adoptContext's onClose runs when the browser of the context closes, so when the context is closed with Close Context or automatically and stopping the trace or saving coverage fails, onClose does not run either. #5318 put closing the context and the browser in try/finally, but not the two steps before it.

Expected behavior
The context is closed and removed from the stack, the browser of a persistent or adopted context is closed, and onClose runs, even when stopping the trace or saving coverage fails. The failure is still reported.

Possible fix
Move stopping the trace and saving coverage inside the try in closeContext, or give them their own try/finally around closing the context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions