Isolated browser context for each page#2160
Merged
Conversation
Cookies set via Network.setCookies persist across browser targets in the same context. When processing many snapshots on sites sharing a parent domain, consent cookies (e.g., didomi_token) accumulate until the Cookie header exceeds the server's ~8KB limit, causing HTTP 400 errors on all CSS/JS/image requests after ~40 snapshots. Add Network.clearBrowserCookies before Network.setCookies in page.goto() so each snapshot starts with a clean cookie jar containing only its own captured cookies. Fixes: PER-7073
Percy caches discovered resources across snapshots. The test now uses img2.gif for the second snapshot so discovery must fetch it fresh, allowing us to verify the Cookie header contains only cookie-b.
Replace Network.clearBrowserCookies (browser-global, unsafe at concurrency > 1) with Target.createBrowserContext per snapshot page. Each snapshot's discovery page now runs in its own isolated browser context (like an incognito window) with a separate cookie jar. This eliminates cookie leakage between concurrent snapshots at any concurrency level. Changes: - browser.js: create isolated context before target, store contextId - page.js: dispose context after session close, remove clearBrowserCookies - Performance: +0.35ms/snapshot (negligible vs 3-60s per snapshot) Fixes: PER-7073
When a page crashes or session closes abruptly, this.session.browser can be null. Add null check before calling disposeBrowserContext.
ninadbstack
approved these changes
Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request improves browser isolation for snapshot discovery to prevent cookies and storage from leaking between concurrent or sequential snapshots. The main changes include creating a new isolated browser context for each page, ensuring proper cleanup of these contexts, and adding a test to verify that cookies do not accumulate across multiple snapshots.
Browser context isolation
Browserclass (browser.js) to create a new isolated browser context for each page usingTarget.createBrowserContext, ensuring that cookies and storage do not leak between concurrent snapshot discovery pages.Pageclass (page.js) to store thebrowserContextIdand dispose of the browser context when the page is closed, preventing resource leaks.Testing cookie isolation
discovery.test.jsto verify that cookies are not accumulated across multiple snapshots, ensuring that cookies set in one snapshot do not carry over to the next.