Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: [chrome] <defunct> left behind #490

Open
morten-holm opened this issue Sep 25, 2024 · 1 comment
Open

[Bug]: [chrome] <defunct> left behind #490

morten-holm opened this issue Sep 25, 2024 · 1 comment
Labels
p2-bug Something isn't working

Comments

@morten-holm
Copy link

Environments

  • playwright-go Version: [email protected]
  • Browser: chromium
  • OS and version: Ubuntu 24.04.1 LTS

Bug description
The provided example leaves behind a defunct chromium process.

To Reproduce
This code reproduces the problem:

package main

import (
	"fmt"
	"net/http"

	"github.com/playwright-community/playwright-go"
)

func pingHandler(w http.ResponseWriter, r *http.Request) {
	pw, err := playwright.Run()
	if err != nil {
		panic(err)
	}
	defer func() {
		err = pw.Stop()
		if err != nil {
			panic(err)
		}
	}()

	browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
		Args: []string{
			"--allow-running-insecure-content",
		},
		Headless: playwright.Bool(true),
	})
	if err != nil {
		panic(err)
	}
	defer func() {
		err = browser.Close()
		if err != nil {
			panic(err)
		}
	}()

	baseUrl := "https://playwright.dev/"
	permissions := []string{"clipboard-read", "clipboard-write", "storage-access"}

	page, err := browser.NewPage(playwright.BrowserNewPageOptions{
		BaseURL:           playwright.String(baseUrl),
		BypassCSP:         playwright.Bool(true),
		JavaScriptEnabled: playwright.Bool(true),
		Permissions:       permissions,
		Viewport: &playwright.Size{
			Width:  1240,
			Height: 800,
		},
	})
	if err != nil {
		panic(err)
	}
	defer func() {
		err = page.Close()
		if err != nil {
			panic(err)
		}
	}()

	webErrors := make([]string, 0)

	page.OnPageError(func(e error) {
		webErrors = append(webErrors, e.Error())
	})

	page.OnConsole(func(e playwright.ConsoleMessage) {
		webErrors = append(webErrors, e.Text())
	})

	_, err = page.Goto(baseUrl, playwright.PageGotoOptions{
		WaitUntil: playwright.WaitUntilStateNetworkidle,
	})
	if err != nil {
		panic(err)
	}

	for _, webError := range webErrors {
		fmt.Printf("%s\n", webError)
	}

	_, err = fmt.Fprintf(w, "pong")
	if err != nil {
		panic(err)
	}
}

func main() {
	err := playwright.Install(&playwright.RunOptions{
		Browsers: []string{"chromium"},
	})

	if err != nil {
		panic(err)
	}

	http.HandleFunc("/ping", pingHandler)

	fmt.Println("Starting server at :8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		fmt.Printf("Error starting server: %s\n", err)
	}
}
$ git clone https://github.com/morten-holm/playwright-demo.git
$ docker build -t playwright-demo .
$ docker run --rm -p 8080:8080 playwright-demo
$ http localhost:8080/ping

After this running 'ps xua' will give a result like this:

# ps xua | grep chrome
root       128  0.0  0.0      0     0 ?        Z    06:16   0:00 [chrome] <defunct>
root       129  0.0  0.0      0     0 ?        Z    06:16   0:00 [chrome] <defunct>
root       198  0.0  0.0   3124  1384 pts/0    S+   06:16   0:00 grep chrome
@morten-holm morten-holm added the p2-bug Something isn't working label Sep 25, 2024
@canstand
Copy link
Collaborator

You just need to start a browser and close it when the program exits. Just create a new BrowserContext for each request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants