[Bug]: Docker server deadlocks on browser recycle: get_page() leaks context refcounts on cancellation, then waits forever for a drain slot
维护者通常 2 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 49/100
调研方向
Read crawl4ai/browser_manager.py, especially BrowserManager.get_page() and _maybe_bump_browser_version(), to trace how context refcounts and pending cleanup interact during cancellation and recycling. Reproduce with wall_clock_s: 3, max_pages_before_recycle: 5, and the slow local paths described; compare behavior against the reported soak results. Done means cancelled or failed calls release acquired resources, recycling does not block callers indefinitely, and the reproducer continues serving requests.
由索引模型根据 Issue 内容生成。
描述
crawl4ai version
0.9.5 (Docker image unclecode/crawl4ai:0.9.5). crawl4ai/browser_manager.py is unchanged on develop.
Expected Behavior
With browser recycling on (max_pages_before_recycle), a cancelled or failed get_page() gives back the context refcount it took, old browser versions drain, and the server keeps serving.
Current Behavior
The Docker server deadlocks after a few hours of normal traffic. Every /crawl returns 504 at wall_clock_s, the log repeats Waiting for old browser to drain (pending: 3), and PIDs climb until the container hits its limit. Only a restart clears it. We saw this in production every 10 to 14 hours.
Cause, in BrowserManager.get_page() (v0.9.5 browser_manager.py lines 1593 to 1768):
- The context refcount is incremented inside
_contexts_lock, then several awaits follow before the page is returned: closing an LRU-evicted context,context.new_page(),_apply_stealth_to_page(), and_maybe_bump_browser_version(). - The refcount is given back only on an
Exceptionfromnew_page(). Anything else that raises after the increment leaks it, and so does any cancellation (asyncio.CancelledErroris aBaseException). The Docker server cancels any crawl still running atwall_clock_s, so in production this happens constantly. - After a recycle bump, a context with a leaked refcount never reaches 0, so its entry stays in
_pending_cleanupforever. - Once 3 entries are stuck (
_max_pending_browsers = 3, which counts context signatures, not browser versions),_maybe_bump_browser_version()loops waiting for a slot. It does so insideget_page(), after the page is created and before it is returned, so the caller never gets a page it could release. Every crawl then waits until the deadline cancels it inside that wait, which leaks more refcounts.
This is separate from #2205 / #2236. Those fix the finally cleanup in _crawl_web (and add crawl_timeout). The leak and the blocking wait above are in browser_manager.py, which #2236 does not touch. We checked: stock 0.9.5 with #2236's library changes applied (async_configs.py, async_crawler_strategy.py) fails the same way in the soak below: every request from round 2 on is a 504, the final fast crawl is a 504, and the container ends at 11,060 PIDs (we did not count drain lines on that run).
Is this reproducible?
Yes
Inputs Causing the Bug
Any steady /crawl traffic where some requests hit wall_clock_s, with recycling on. A low wall_clock_s and max_pages_before_recycle make it quick.
Steps to Reproduce
- Copy
/app/config.ymlfrom the 0.9.5 image and setwall_clock_s: 3andmax_pages_before_recycle: 5. Mount it over/app/config.yml. - Run the container with
CRAWL4AI_ALLOW_INTERNAL_URLS=trueand a local HTTP server inside it, where a third of the paths sleep 20 s before answering. - Send 8 rounds of 150 single-URL
/crawlrequests at 30 concurrent. - Then send one fast crawl to a page that answers at once.
Results (all on the same host, throwaway containers):
| Build | Rounds 2 to 8 | Final fast crawl | PIDs at end |
|---|---|---|---|
| stock 0.9.5 | every request 504, drain loop in the log | 504 | 10,161 |
| stock 0.9.5 + #2236 (library files) | every request 504 | 504 | 11,060 |
| 0.9.5 + our patch | every round serves, no drain loop | 200 | about 200 |
Code snippets
# v0.9.5 BrowserManager.get_page(), simplified
async with self._contexts_lock:
...
self._context_refcounts[config_signature] = self._context_refcounts.get(config_signature, 0) + 1
...
try:
page = await context.new_page()
except Exception: # CancelledError is not caught; nor is anything after this line
self._context_refcounts[config_signature] -= 1
raise
await self._apply_stealth_to_page(page)
await self._maybe_bump_browser_version() # can wait indefinitely for a drain slot
return page, context
OS
Linux (Docker, host kernel 7.0)
Python version
3.12.15 (image default)
Browser
Chromium headless shell (image default)
Browser version
Playwright 1.64.0, chromium_headless_shell-1248
Error logs & Screenshots (if applicable)
Waiting for old browser to drain (pending: 3)
(BROWSER tag, debug level) repeating, with every /crawl returning 504 at wall_clock_s.
We have a fix running in production since 2026-10-17 17:06Z, with no drain loop and no deadlock restarts since. In short:
- The recycle bump never blocks. When 3 old versions are still draining, it first cleans pending entries whose refcount is already 0, then defers and lets a later request retry.
get_page()records what it took (refcount, page, whether it created the page) and gives back exactly that on anyBaseException, with no await.new_page()/new_context()are awaited shielded, so an object Playwright creates after the caller was cancelled is closed when it arrives.
It comes with 16 tests (15 with fake contexts, 1 with a real browser). 15 of them fail on stock 0.9.5. Happy to open a PR against develop if that is useful.
- 主要语言
- Python
- 星标
- 85.9k
- 派生
- 8.9k
- 平均合并
- 5 天 8 小时
- 30 天内合并 PR
- 24
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
unclecode/crawl4ai 的其他 Issue
-
[Bug]: Text after an inline <link>, <meta>, <style> or <noscript> is dropped from cleaned_html and markdown可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭🐞 Bug 🩺 Needs Triage
难度 2/5 1-3 小时 新手友好度 85/100
unclecode/crawl4ai#2416 · 1 条评论 ·
维护者通常 2 天内回复
-
[Bug]: Since 0.9.1, Linux launches chrome-headless-shell instead of Chromium可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭🐞 Bug 🩺 Needs Triage
难度 2/5 1-3 小时 新手友好度 72/100
unclecode/crawl4ai#2382 · 1 条评论 ·
维护者通常 2 天内回复
-
[Bug]: BestFirstCrawlingStrategy fetches a full batch past max_pages可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭🐞 Bug 🩺 Needs Triage
难度 2/5 1-3 小时 新手友好度 74/100
unclecode/crawl4ai#2381 · 1 条评论 ·
维护者通常 2 天内回复
-
[Bug]: CrawlerRunConfig.clone() drops fallback_fetch_function, so deep crawls never use it可能已有人在做 @siva23367 于 4 天前认领。 未关闭🐞 Bug 🩺 Needs Triage
难度 2/5 1-3 小时 新手友好度 78/100
unclecode/crawl4ai#2379 · 1 条评论 ·
维护者通常 2 天内回复
-
[Bug]: Docker memory guard reads host RAM when no container limit is set — cgroup v2 "max" defeats get_container_memory_percent可能已有人在做 @Battleplus 于 60 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
unclecode/crawl4ai#2123 · 1 条评论 ·
维护者通常 2 天内回复
查看 unclecode/crawl4ai 的全部 Issue
相似的 Issue
-
type-bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
push_gh-pages fails on master branch on forks可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭good first issue infrastructure
难度 2/5 1-3 小时 新手友好度 76/100
kuznia-rdzeni/coreblocks#1135 ·
-
good first issue server
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
documentation priority: high
难度 2/5 1-3 小时 新手友好度 78/100
a-lex-s/mealie-telegram-bot#54 ·
维护者通常 1 天内回复