Skip to content

Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.

fix(docker): recycle pooled browser contexts by pages served (#2231)

ntohidi6ccf610bcb44
diff --git a/deploy/docker/config.yml b/deploy/docker/config.yml
index 7aabc681..6040ed1c 100644
--- a/deploy/docker/config.yml
+++ b/deploy/docker/config.yml
@@ -88,6 +88,14 @@ crawler:
kwargs:
headless: true
text_mode: true
+ # Pooled browsers are long-lived, and their browser context is reused for
+ # every crawl that shares a config. Real sites leave state in that context
+ # (cookies, localStorage, service workers) which is never cleared, and
+ # navigation gets measurably slower as it builds up. Recycling by pages
+ # served bounds that: the context is replaced inside the same Chromium
+ # process, old contexts drain on their own, and it works under sustained
+ # load (unlike the janitor, which only closes *idle* browsers). See #2231.
+ max_pages_before_recycle: 200
extra_args:
# - "--single-process"
# SECURITY: --no-sandbox disables the Chromium renderer sandbox (a renderer
diff --git a/tests/docker/test_pool_recycle_config.py b/tests/docker/test_pool_recycle_config.py
new file mode 100644
index 00000000..c51a81b0
--- /dev/null
+++ b/tests/docker/test_pool_recycle_config.py
@@ -0,0 +1,42 @@
+"""The Docker pool must recycle its browser context by pages served (#2231).
+
+The pool's janitor only closes *idle* browsers, so a server under sustained
+load never recycles one. Recycling by page count is what bounds the context
+state (cookies/localStorage/service workers) that slows navigation down.
+"""
+import sys
+from pathlib import Path
+
+import pytest
+import yaml
+
+from crawl4ai import BrowserConfig
+
+CONFIG_PATH = Path(__file__).resolve().parents[2] / "deploy" / "docker" / "config.yml"
+
+
[email protected](scope="module")
+def browser_section():
+ return yaml.safe_load(CONFIG_PATH.read_text())["crawler"]["browser"]
+
+
+def test_recycle_is_enabled(browser_section):
+ kwargs = browser_section.get("kwargs", {})
+ assert kwargs.get("max_pages_before_recycle", 0) > 0, (
+ "deploy/docker/config.yml must set crawler.browser.kwargs."
+ "max_pages_before_recycle > 0, otherwise pooled browser contexts are "
+ "never recycled under sustained load (issue #2231)."
+ )
+
+
+def test_kwargs_reach_browser_config(browser_section):
+ """server.py/api.py splat these kwargs straight into BrowserConfig."""
+ cfg = BrowserConfig(
+ extra_args=browser_section.get("extra_args", []),
+ **browser_section.get("kwargs", {}),
+ )
+ assert cfg.max_pages_before_recycle > 0
+
+
+if __name__ == "__main__":
+ sys.exit(pytest.main([__file__, "-v"]))