BrowserPool: pool slots unusable cross-thread — pre-warm is effectively a no-op #53
Labels
No labels
accessibility
backlog
browser-pool
bug
cloud
enhancement
feature
infra
paid-tier
performance
ux
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: Circuit-Forge/snipe#53
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
BrowserPoolwarms up N Chromium browser slots in background threads at startup. But Playwright's sync API binds to the thread that created it — using a slot from a different thread (e.g. a FastAPI uvicorn thread-pool handler) raises:The
_fetch_with_slot()call always fails,_close_slot()terminates the browser, and_fetch_fresh()launches a completely new Xvfb+Chromium instance per request. The pre-warm overhead is wasted.Root Cause
playwright.sync_api.sync_playwright()creates an internal asyncio event loop bound to the calling thread. TheSyncBase._sync()wrapper detects if it's being called from a different thread and raises the error.Options
Thread-local pool — store a Playwright instance per-thread using
threading.local(). Each FastAPI thread creates its own browser on first use and reuses it for subsequent requests on the same thread. No cross-thread sharing.Playwright async API — rewrite the pool to use
playwright.async_apiwith asyncio. FastAPI supports async route handlers natively. The async API has no thread-binding constraint.Dedicated browser threads — one thread per slot, with each thread owning its Playwright instance. Requests are dispatched to slot-owner threads via a queue.
Option 1 is the lowest-effort path and compatible with FastAPI's sync route handlers running in a thread pool.
Current Behavior
_fetch_fresh()→ full Xvfb+Chromium cold start (~10s per fetch)Impact
Each Mercari/eBay scrape pays ~10s cold-start cost instead of reusing a warm context. With traffic this is fine for MVP but becomes a bottleneck under load.
Labels
enhancement,browser-pool,performance