Skip to content

Commit 70309d9

Browse files
vdusekclaude
andauthored
fix: Prevent list modification during iteration in BrowserPool (#1703)
Both `_identify_inactive_browsers` and `_close_inactive_browsers` methods were modifying their respective lists (`_active_browsers` and `_inactive_browsers`) while iterating over them. This is a classic bug that can cause items to be skipped or raise `RuntimeError`. The fix iterates over a copy of the list (`list(...)`) while modifying the original, ensuring all items are properly processed. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9737752 commit 70309d9

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/crawlee/browsers/_browser_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,14 @@ async def _launch_new_browser(self, plugin: BrowserPlugin) -> BrowserController:
346346

347347
def _identify_inactive_browsers(self) -> None:
348348
"""Identify inactive browsers and move them to the inactive list if their idle time exceeds the threshold."""
349-
for browser in self._active_browsers:
349+
for browser in list(self._active_browsers):
350350
if browser.idle_time >= self._browser_inactive_threshold:
351351
self._active_browsers.remove(browser)
352352
self._inactive_browsers.append(browser)
353353

354354
async def _close_inactive_browsers(self) -> None:
355355
"""Close the browsers that have no active pages and have been idle for a certain period."""
356-
for browser in self._inactive_browsers:
356+
for browser in list(self._inactive_browsers):
357357
if not browser.pages:
358358
await browser.close()
359359
self._inactive_browsers.remove(browser)

0 commit comments

Comments
 (0)