User Agents

Web browsers and everyday web tech, explained

Fix Lost UI Shared Context Error in Headless Chrome

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide explains how to resolve the "Lost UI shared context" error that sometimes appears when running headless Chrome, particularly in automation scripts or CI environments. The error typically occurs when Chrome's GPU or compositing process fails to initialize properly in headless mode. By applying the steps below, you'll configure Chrome to run stably without the UI context, avoiding crashes and hangs.

What You'll Need

  • Chrome or Chromium installed (version 60 or newer recommended)
  • Access to the command line or terminal
  • Basic knowledge of launching Chrome with flags
  • If using a driver (like Selenium or Puppeteer), access to its configuration file

Step-by-Step

  1. Identify the exact error message. The full error often looks like: "Lost UI shared context on the GPU channel" or "Lost UI shared context". Note the context—whether it appears at startup or after a period of running. This helps you verify the fix later.
  2. Launch headless Chrome with the recommended flags. Add these flags to your command line or driver options:
    --headless=new --disable-gpu --no-sandbox --disable-dev-shm-usage
    The --disable-gpu flag is critical because it forces Chrome to use software rendering, avoiding the GPU process that often causes the context loss. --no-sandbox is sometimes needed in containerized environments, and --disable-dev-shm-usage prevents shared memory issues in Docker.
  3. If using Selenium, set the options. In Python, for example:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    opts = Options()
    opts.add_argument('--headless=new')
    opts.add_argument('--disable-gpu')
    opts.add_argument('--no-sandbox')
    opts.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome(options=opts)
    For JavaScript (Puppeteer), pass the flags in args when launching.
  4. If using Puppeteer, adjust the launch call. Example:
    const browser = await puppeteer.launch({
      headless: 'new',
      args: ['--disable-gpu', '--no-sandbox', '--disable-dev-shm-usage']
    });
    Make sure you're using a recent version of Puppeteer that supports the new headless mode.
  5. Test the configuration. Run a simple script that opens a page and performs a basic action (like getting the title). If the error no longer appears, the fix works.
  6. If the error persists, try additional flags. Add these one at a time to isolate the cause:
    • --disable-software-rasterizer – forces Chrome to avoid software GPU emulation
    • --disable-features=VizDisplayCompositor – disables the new display compositor
    • --use-gl=swiftshader – uses SwiftShader for software WebGL (if needed)
  7. If running in Docker or CI, ensure shared memory is sufficient. The --disable-dev-shm-usage flag helps, but you can also mount a larger /dev/shm with --shm-size=1g in Docker.
  8. Update Chrome and your driver. Outdated versions may have bugs that cause context loss. Update to the latest stable Chrome and the latest Selenium/Puppeteer.
  9. If using ChromeDriver, ensure it matches your Chrome version. Mismatched versions can cause unexpected errors. Download the matching ChromeDriver from the official site.

Common Mistakes

  • Using --headless instead of --headless=new. The old headless mode is more prone to context issues. Always use the new mode if your Chrome version supports it (Chrome 112+).
  • Forgetting --disable-gpu. Even in headless mode, Chrome may try to use GPU acceleration, leading to the error. This flag is essential.
  • Not applying flags to the correct place. In Selenium, flags must be added via options, not as command-line arguments to the driver executable. In Puppeteer, they go in the args array.
  • Ignoring the --no-sandbox flag in containers. Without it, Chrome may fail to start entirely, which can mask the context error.
  • Not restarting the browser after changing flags. Some flags only take effect on a fresh browser instance. Always restart your script.

Tips

  • Add a retry mechanism in your automation code. If the error occurs occasionally, catching it and relaunching the browser can be a practical workaround.
  • Use the --enable-logging flag to get more detailed logs. This can help you see if the error is related to GPU or something else.
  • In CI environments, set CHROME_HEADLESS environment variable to 1 if your framework relies on it.
  • Consider using a lightweight alternative like Chromium instead of full Chrome, as it may have fewer background processes.
  • Keep your automation code isolated from the browser's crash. Use try-catch blocks and ensure the driver quits properly.

Troubleshooting

If the error still appears after applying the steps, try the following:

  • Check if the error occurs only on certain pages. If so, the page may use WebGL or heavy graphics. Add --disable-webgl or --disable-3d-apis to disable these features.
  • Try running Chrome with --single-process. This is not recommended for production but can help isolate the issue. Note that this flag can cause instability, so use it only for testing.
  • Look for related errors in the system logs. On Linux, check dmesg for GPU-related errors. On Windows, check the Event Viewer.
  • If using a remote webdriver (like Selenium Grid), ensure the node has the same flags configured. The error may originate from the node, not your local machine.
  • Try a different Chrome version. Sometimes a specific version has a regression. Install a previous stable version and test.

Frequently Asked Questions

What exactly causes the "Lost UI shared context" error?

It occurs when Chrome's GPU process loses the shared context used for rendering. In headless mode, this often happens because the GPU process fails to initialize properly, especially in virtualized environments without GPU support.

Is it safe to disable GPU in headless mode?

Yes. Headless mode is designed for automation and doesn't require GPU acceleration for most tasks. Disabling GPU forces software rendering, which is stable and sufficient for scraping, testing, and screenshots.

Will these flags affect page rendering?

They may affect performance slightly, but not the output. Pages will render correctly, though complex WebGL content might not work without GPU support. If you need WebGL, you can try --use-gl=swiftshader instead of disabling GPU entirely.

Do I need to use --no-sandbox?

Only if you're running as root or in a container without proper user permissions. It's not required on a normal desktop. Using it can reduce security, so avoid it unless necessary.

Can this error happen in non-headless mode?

Yes, but it's rare. In headless mode, the lack of a visible window can trigger GPU initialization issues more often. The same flags can be used in non-headless mode if you encounter the error there.

Why does the error appear intermittently?

It may be related to memory pressure or resource limits. The --disable-dev-shm-usage flag helps, and ensuring your system has enough free memory can reduce frequency.