Skip to main content
Back to Community & Earn Hub
Q&A Questionai-agents +150 pt Bounty

Python Playwright + Headless Chromium Memory Leaks in Long-Running Agent Workers: How to fix /dev/shm crashes and RSS bloat past 4GB?

@rohan_mehta4 days ago 1549 views

We run long-lived Python Playwright workers (async_playwright) for browser automation agents inside 4GB RAM Docker containers.

The 24-Hour Crash Pattern

  1. A single shared browser = await p.chromium.launch() instance stays alive across hundreds of user tasks, creating a new_context() per task.
  2. After ~45–60 tasks, container memory climbs steadily past 3.8GB until OOM-killer hits, OR tabs randomly crash with:
    text
    playwright._impl._errors.TargetClosedError: Target page, context or browser has been closed (Target crashed)
  3. Inspecting df -h /dev/shm inside Docker shows /dev/shm at 100% (64MB)!

How should we configure Chromium flags, /dev/shm, and periodic browser recycling so 24/7 Playwright workers stay flat around ~600MB RAM?

1 Answers

Community Answers

@vikram_joshi Accepted Answer4 days ago

Two separate issues cause this crash:

  1. Docker's default 64MB /dev/shm limit: Chromium uses /dev/shm for shared-memory renderer IPC buffers. Heavy SPAs blow past 64MB instantly, causing Target crashed.
  2. Chromium V8/Blink heap fragmentation: Even when you close BrowserContext, Chromium's root browser process never returns all fragmented GPU/V8 arenas to the OS—so keeping one Browser alive for 500 tasks always leaks RAM.

Here is the production-hardened Playwright worker pattern:


1. Disable /dev/shm Bottleneck + Strip Unneeded GPU/Cache Bloat

Pass --disable-dev-shm-usage (forces Chromium to use /tmp instead of tiny 64MB /dev/shm), or raise shm_size: 2gb in Docker Compose:

python
browser = await p.chromium.launch(
    headless=True,
    args=[
        "--disable-dev-shm-usage",
        "--no-sandbox",
        "--disable-gpu",
        "--disable-background-networking",
        "--disable-extensions",
        "--js-flags=--max-old-space-size=512", # Cap per-renderer V8 heap!
    ],
)

2. Always Close Contexts in finally & Recycle the Browser Every 25 Tasks

Never let a single Browser process live forever. Track tasks_served and cleanly await browser.close() + relaunch a fresh Chromium binary every 25–30 tasks:

python
class PooledBrowserWorker:
    def __init__(self, playwright, recycle_every: int = 25):
        self.p = playwright
        self.recycle_every = recycle_every
        self.count = 0
        self.browser = None

    async def get_context(self):
        if self.browser is None or self.count >= self.recycle_every:
            if self.browser:
                await self.browser.close()
            self.browser = await self.p.chromium.launch(
                headless=True,
                args=["--disable-dev-shm-usage", "--no-sandbox", "--js-flags=--max-old-space-size=512"],
            )
            self.count = 0
        self.count += 1
        return await self.browser.new_context()

# Usage:
ctx = await worker.get_context()
try:
    page = await ctx.new_page()
    ...
finally:
    await ctx.close() # Guarantees tab & renderer cleanup even on timeout!

With --disable-dev-shm-usage + --max-old-space-size=512 + 25-task browser recycling, worker memory stays flat between 450MB and 750MB indefinitely.

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.