Build permanent browser with egress-hardened default config so its pool signature matches - #2237
Open
SohamKukreti wants to merge 1 commit into
Open
Build permanent browser with egress-hardened default config so its pool signature matches#2237SohamKukreti wants to merge 1 commit into
SohamKukreti wants to merge 1 commit into
Conversation
…fig so its pool signature matches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Partially fixes #2204
The Docker server starts a "permanent" always-warm browser at boot so default-config requests can skip browser startup. On
developit never serves a single request: its pool signature (_sig()= SHA1 ofBrowserConfig.to_dict()) is computed from a raw config that skipsenforce_egress, so it hasproxy_config: None. Every real request goes throughenforce_egress, which setsproxy_configto the pinning proxy URL. The two signatures can never be equal, soget_crawler()never returnsPERMANENTand every container carries an idle ~270 MB Chromium + Playwright driver tree that the janitor never reclaims. This is the mysterious seconddriver → browsertree in #2202.This PR builds the permanent browser via
get_default_browser_config()(the same helper/html,/screenshot,/pdf,/execute_jsalready use), so the permanent browser gets the same egress hardening and therefore the same signature as those endpoints. Side effect: the permanent browser now also routes through the pinning proxy and has TLS verification on, which it did not before.The pinning proxy port is random per boot, so the signature changes on every restart. That is fine:
DEFAULT_CONFIG_SIGlives only in memory and is recomputed on each boot; nothing persists signatures (monitor only persists endpoint stats to Redis).What this does not fix (deliberately left for a follow-up)
/crawl,/crawl/streamand/crawl/jobstill miss the permanent browser. They buildBrowserConfigfrom the request body only (BrowserConfig.load(body)), never fromconfig.yml. The permanent browser is built fromconfig.yml, which addstext_mode: trueand fourextra_args. So even an emptybrowser_config(or nobrowser_configat all) hashes differently and lands in the cold pool.Making
/crawlmatch requires a decision that changes user-visible behaviour: mergeconfig.ymlbrowser defaults into every/crawlbody before hashing (and then decide whethertext_mode: true— which disables JS — should stay inconfig.yml). Keeping that out of this PR so this one stays a one-line, behaviour-neutral fix.Also not in this PR (known, from the same investigation, in
POST /monitor/actions/restart_browser): the route holdsLOCKand then callsinit_permanent, which re-acquires the same non-reentrant lock (deadlock); it importsPERMANENTby value so the global is never reset after close; and it rebuilds the config withoutenforce_egress, which would re-introduce this exact mismatch after a manual restart.List of files changed and why
deploy/docker/server.py— inlifespan(), replace the rawBrowserConfig(extra_args=..., **kwargs)passed toinit_permanentwithget_default_browser_config(), so the permanent browser's config (and thus its pool signature) goes throughenforce_egresslike every other server-built config.How Has This Been Tested?
Built the image locally from this branch (
docker build -t crawl4ai:ticket4-a .) and ran it with-e CRAWL4AI_API_TOKEN=... -p 11299:11235.Before (0.9.2 /
develop):driver → chrome-headless-shelltree, no--proxy-serverflag on it./crawlwith a plain body: 2 trees. Log shows🆕 Creating new browser in cold pool, never🔥 Using permanent browser.After (this branch):
--proxy-server=http://127.0.0.1:<egress port>matching theegress pinning proxy listening on ...log line.POST /html {"url":"https://example.com"}→ log shows🔥 Using permanent browser, still 1 tree.POST /crawl {"urls":["https://example.com"]}(with and withoutbrowser_config) → still🆕 Creating new browser in cold pool (sig=d32dbb0a), 2 trees. Expected — see "What this does not fix".Commands used:
Checklist: