d2lang/d2 · error

failed to start new Playwright browser context: %w

Error message

failed to start new Playwright browser context: %w

What it means

After launching Chromium, startPlaywright creates an isolated browser context with a device scale factor of 2.0. If browser.NewContext fails, this error wraps the driver's message. Without a context, no page can be created, so rendering cannot proceed.

Source

Thrown at lib/png/png.go:68

	// Optimizations for a very tightly scoped Playwright instance
	browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
		Args: []string{
			"--no-sandbox",                             // Removes security overhead
			"--disable-dev-shm-usage",                  // Prevents /dev/shm issues
			"--disable-background-timer-throttling",    // Prevents CPU throttling
			"--disable-backgrounding-occluded-windows", // Keeps rendering active
			"--disable-features=TranslateUI",           // Reduces feature overhead
			"--disable-ipc-flooding-protection",        // Removes IPC limits
		},
	})
	if err != nil {
		return Playwright{}, fmt.Errorf("failed to launch Chromium: %w", err)
	}
	context, err := browser.NewContext(playwright.BrowserNewContextOptions{
		DeviceScaleFactor: playwright.Float(2.0),
	})
	if err != nil {
		return Playwright{}, fmt.Errorf("failed to start new Playwright browser context: %w", err)
	}
	page, err := context.NewPage()
	if err != nil {
		return Playwright{}, fmt.Errorf("failed to start new Playwright page: %w", err)
	}
	return Playwright{
		PW:      pw,
		Browser: browser,
		Page:    page,
	}, nil
}

func InitPlaywright() (Playwright, error) {
	err := playwright.Install(&playwright.RunOptions{
		Verbose:  false,
		Browsers: []string{"chromium"},
	})
	if err != nil {

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Check the wrapped driver error and Chromium crash logs; fix the crash cause (raise memory/shm limits, adjust flags)
  2. Retry via RestartBrowser — transient post-launch crashes are common under resource pressure
  3. Ensure Launch succeeded against a healthy browser (verify with playwright doctor-equivalent / manual launch)
  4. Keep playwright-go and browser builds in sync; reinstall browsers if versions drifted
Defensive patterns

Strategy: retry

Try / catch

pw, err := startPlaywright(p)
if err != nil {
	if strings.Contains(err.Error(), "browser context") {
		time.Sleep(retryDelay)
		pw, err = startPlaywright(p) // transient crash right after launch
	}
}

Prevention

When it happens

Trigger: browser.NewContext returns an error from the driver — typically the browser process died immediately after launch (crash, OOM), or the driver connection was lost between Launch and NewContext.

Common situations: Chromium crashing seconds after startup due to limited /dev/shm or memory in containers; driver instability right after launch; invalid option combinations rejected by the driver.

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/b531a441f3e9d3d2. Report an issue: GitHub.