$ 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
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)
}
}
The Chromium processes should be killed when the Go process stops.
The number of Chromium processes grows after each time I run the Go program.
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.
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.