Rayrun
← Back to Discord Forum

Chrome process remains after close in Celery task

I'm using Playwright-Python to generate PDFs inside of a Celery task. This works great, except the chrome process sticks around afterwards, even when browser.close is called, and with sync_playwright used as a context.

Interestingly, the process appears to get properly closed when running this locally in Linux, and macOS, but in production, running inside a Docker container + Celery task, it does not. The node driver process is properly cleaned up.

Curious if anyone has any ideas about what might be going on, or if there's further debug logs that could be enabled.

Thanks.

with sync_playwright() as p:
    browser = None
    try:
        logger.info("Launching browser")
        browser = p.chromium.launch(args=["--font-render-hinting=none"])
        page = browser.new_page()

        page.on("console", lambda msg: logger.info(msg.text))
        browser_error = None

        def on_pageerror(err):
            nonlocal browser_error
            browser_error = err

        page.on("pageerror", on_pageerror)

        logger.info("Loading template in browser")
        page.goto(f"file://{template_path}", wait_until="networkidle")
        logger.info("Exporting as PDF")
        pdf_bytes = page.pdf(print_background=True)

        if browser_error:
            raise RuntimeError(
                f"Browser error while generating PDF: {browser_error}"
            )
    finally:
        if browser:
            browser.close()

This thread is trying to answer question "Why does the Chrome process remain after closing in a Celery task using Playwright-Python when running inside a Docker container?"

3 replies

Quoting from the docs:

  1. Using --ipc=host is also recommended when using Chromium. Without it Chromium can run out of memory and crash. Learn more about this option in Docker docs.
  2. Seeing other weird errors when launching Chromium? Try running your container with docker run --cap-add=SYS_ADMIN when developing locally.
  3. Using --init Docker flag or dumb-init is recommended to avoid special treatment for processes with PID=1. This is a common reason for zombie processes. See https://playwright.dev/docs/ci#docker

(--init is fixing the zombies)

Ah, checked and they are indeed zombie processes. Thanks.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].