Skip to content

fix(browsers): wait for persistent profiles to be released before relaunch - #2581

Open
Liz-Lian wants to merge 1 commit into
apify:masterfrom
Liz-Lian:fix/persistent-profile-handoff
Open

Liz-Lian wants to merge 1 commit into
apify:masterfrom
Liz-Lian:fix/persistent-profile-handoff

Conversation

@Liz-Lian

@Liz-Lian Liz-Lian commented Dec 3, 2026

Copy link
Copy Markdown

Description

  • Coordinate page allocation for a caller-supplied persistent profile within a browser pool. If the current owner is retired, wait for its open pages to drain and for browser shutdown before launching a replacement.
  • Bound waiting by the pool's operation timeout without interrupting active pages. Unrelated browser profiles and automatically assigned temporary profiles do not wait on this profile's lock.

Issues

Testing

  • Windows Chrome 158, Playwright 1.65.2, clean master 8df3faf: a six-page local crawl with retirement every two pages completed 2/6 requests twice. The patched run completed 6/6 twice, with both the no-retirement and no-fixed-profile controls also completing 6/6. No remote pages or real browser profiles used.
  • Targeted deterministic coverage for cancellation, active-page draining, independent profiles, and cleanup versus handoff.
  • Both added repository integration tests passed on Windows with Playwright's bundled Chromium 159. The targeted harness reported missing timeout/flaky configuration plugins; neither is used by the two new cases.
  • Pinned Ruff checks and formatting for touched files, pinned ty for both modified production files, and Git whitespace check passed. Full repository CI was not run locally.

Checklist

  • CI passed

AI assistance: ChatGPT/Codex assisted with implementation and testing.

@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Dec 3, 2026

@Mantisus Mantisus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

My main concern is the design. The fix spreads over most of the generic pool paths: _pick_browser_with_free_capacity(), _identify_inactive_browsers(), _close_inactive_browsers() and _override_page_close() all learn about profiles, and on top of that there's a new dataclass with a lock, an event and page.on('close') handlers. The problem itself is narrower. A plugin with a fixed user_data_dir can't have two browsers alive at once, so only the launch of its next browser needs to wait. Also, the fix shouldn't break backward compatibility for custom PlaywrightBrowserPlugin implementations, and it should cover StagehandBrowserPlugin too, which runs into the same conflict.

) -> tuple[CrawleePage, BrowserController]:
"""Reserve the profile while selecting its owner and opening the page."""
async with async_timeout(self._operation_timeout.total_seconds()):
await profile.lock.acquire()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock wait gets its own operation_timeout here, and _get_profile_browser() and new_page() then get one each, so a single new_page() call can take up to three times the timeout. I think the lock and the wait for the previous browser could share one timeout, as the description says.

async with async_timeout(self._operation_timeout.total_seconds()):
await profile.lock.acquire()
try:
return await self._open_page(page_id, plugin, proxy_info, profile)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock also covers the pre-page-create hooks and new_page(), so all pages on the profile open one by one, not just browser launches. Maybe it's enough to hold it while waiting for the previous browser and launching the next one, since the controller already creates the context under its own lock.

if browser in self._active_browsers and profile.plugin is plugin:
if browser.has_free_capacity:
return browser
elif not browser.pages and not browser.is_opening_pages:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A browser also gets into the inactive list after 'browser_inactive_threshold' without new pages, even with a page still open. Then the next page waits for that page to close and restarts the browser, though it was never retired.

This branch has not been deployed

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

Labels

t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BrowserPool relaunch on a persistent user_data_dir fails with Chromium ProcessSingleton while the retired browser still holds the profile

2 participants