Repository navigation
Conversation
Mantisus
left a comment
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
Description
Issues
Testing
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.Checklist
AI assistance: ChatGPT/Codex assisted with implementation and testing.