Chromedp: Hanging Chromium processes

Created on 13 Feb 2021  路  5Comments  路  Source: chromedp/chromedp

What versions are you running?

$ go list -m github.com/chromedp/chromedp
github.com/chromedp/chromedp v0.6.5
$ chromium --version
Chromium 89.0.4388.0
$ go version
go version go1.15.7 darwin/amd64

What did you do? Include clear steps.

I'm running this simple program:

func main() {
    ctx, cancel := chromedp.NewContext(context.Background())
    defer cancel()

    if err := chromedp.Run(
        ctx,
        chromedp.Navigate("https://github.com"),
    ); err != nil {
        panic(err)
    }
}

What did you expect to see?

The Chromium processes should be killed when the Go process stops.

What did you see instead?

The number of Chromium processes grows after each time I run the Go program.

Most helpful comment

I have read your blog post and tried your code, but in my case (within the Docker container), the zombie processes cannot be killed without killing the main process (PID 1). Moreover, just killing off Chromiums would do harm since multiple go routines might have spawned Chromium processes and just killing all will lead to errors.

As a consequence, I am looking for another solution, hopefully one which can be done in the code.

All 5 comments

I can second this behavior. I have written a screenshot service based on chromedp (https://github.com/mkalus/goggler) and experience the same problem when running the Docker image.

Try the following:

docker run -d --rm -p8080:8080 --name goggler ronix/goggler
# before:
docker exec goggler ps -Af
# get image
curl -o /dev/null http://localhost:8080/?url=https://www.google.com/
# after
docker exec goggler ps -Af

The last column will look something like that:

UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 13:58 ?        00:00:00 /opt/google/chrome/goggler
root          28       1  0 13:59 ?        00:00:00 [cat] <defunct>
root          29       1  0 13:59 ?        00:00:00 [cat] <defunct>
root          31       1  0 13:59 ?        00:00:00 [chrome] <defunct>
root          32       1  0 13:59 ?        00:00:00 [chrome] <defunct>
root          44       1  0 13:59 ?        00:00:00 [chrome] <defunct>
root          50       1  0 13:59 ?        00:00:00 [chrome] <defunct>
root          72       0  0 14:01 ?        00:00:00 ps -Af

I cannot get rid of the zombies without killing the parent process which shuts down the container of course.

@mkalus I've written a blog post explaining how to mitigate the issue: https://aymericbeaumet.com/prevent-chromedp-chromium-zombie-processes-from-stacking.

_TLDR_

func main() {
    ctx, cancel := chromedp.NewContext(context.Background())
    defer func() {
        cancel()
        // Prevent Chromium processes from hanging
        if _, err := exec.Command("pkill", "-g", "0", "Chromium").Output(); err != nil {
            log.Println("[warn] Failed to kill Chromium processes", err)
        }
    }()

    // ...
}

I have read your blog post and tried your code, but in my case (within the Docker container), the zombie processes cannot be killed without killing the main process (PID 1). Moreover, just killing off Chromiums would do harm since multiple go routines might have spawned Chromium processes and just killing all will lead to errors.

As a consequence, I am looking for another solution, hopefully one which can be done in the code.

Thanks to you @aymericbeaumet, I have had another look and found a solution described in https://github.com/chromedp/docker-headless-shell

I need to initialize my container using dumb-init or tini to get rid of zombie processes. Thanks for pushing me to think again ;-)

Issue #774 should've been given as a comment here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AmrAlfoly picture AmrAlfoly  路  3Comments

howesteve picture howesteve  路  3Comments

t-blake picture t-blake  路  3Comments

pmurley picture pmurley  路  5Comments

iMaxopoly picture iMaxopoly  路  3Comments