Chromedp: Chrome is not shutting down when running with headless mode in docker

Created on 29 Nov 2018  路  4Comments  路  Source: chromedp/chromedp

I am trying out below code to run the screenshot/printToPdf tasks after starting the chrome in a headless mode. I refer the examples and they are using same api calls to start and end the chrome instance. What I observe is that chromedp is not able to kill the chrome instance properly and I see new chrome processes in my container for every request( I am making http call to trigger below logic).

   `// create chrome instance
    c, err := chromedp.New(ctx, 
    chromedp.WithLog(log.Printf),
    chromedp.WithRunnerOptions(
        runner.Flag("headless", true),
        runner.Flag("remote-debugging-port", 9222),
        runner.Flag("disable-web-security", true),
        runner.Flag("disable-gpu", true)),
            )

    // Run some Tasks now
     log.Info("Fired tasks")

    // shutdown chrome
     err = c.Shutdown(ctx)
     if err != nil {
       log.Fatal(err)
     }
         log.Info("Post Shutdown")
    // wait for chrome to finish
     err = c.Wait()
     if err != nil {
      log.Fatal(err) //failing here
     }`

I see below logs for my service -

  • msg="Fired tasks"
  • msg="Post Shutdown"
  • msg="-> { "method": "Inspector.detached", "params": { "reason": "target_closed"} }"
  • msg="signal: killed"

I tried without any tasks, just start and shutdown logic but result was same.

Environment -
minikube version: v0.30.0
Docker with hyperkit

Most helpful comment

I have a similiar problem on Windows and Linux with headless mode. I just use the following as workaround.

err = c.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}

// wait for chrome to finish
// Wait() will hang on Windows and Linux with Chrome headless mode
// we'll need to exit the program when this happens
ch := make(chan error)
go func() {
c.Wait()
ch <- nil
}()

select {
case err = <-ch:
log.Println("chrome closed")
case <-time.After(10 * time.Second):
log.Println("chrome didn't shutdown within 10s")
}

All 4 comments

I have a similiar problem on Windows and Linux with headless mode. I just use the following as workaround.

err = c.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}

// wait for chrome to finish
// Wait() will hang on Windows and Linux with Chrome headless mode
// we'll need to exit the program when this happens
ch := make(chan error)
go func() {
c.Wait()
ch <- nil
}()

select {
case err = <-ch:
log.Println("chrome closed")
case <-time.After(10 * time.Second):
log.Println("chrome didn't shutdown within 10s")
}

ya its bug i think

the shutdown function just closed all windows in chrome, but its worked at UI chrome , not worked at headless mode, u should add

if r.cmd != nil && r.cmd.Process != nil{

return r.cmd.Process.Signal(syscall.SIGTERM)

}

at the end of the shutdown function in runner.go

Through debugging, I found that calling the headless Chrome's Shutdown method can only close the page process, and the headless Chrome will run in the background after the program exits. Under Windows, the solution I found was to see r.Cmd.Process.Pid representing the process ID of headless Chrome under the runner.go file tracked by debugging, and to kill the headless Chrome process through the taskkill of exec.cmd through the process number.I think In Linux you can also use this method.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aymericbeaumet picture aymericbeaumet  路  5Comments

jafriyie1 picture jafriyie1  路  3Comments

vdobler picture vdobler  路  4Comments

pmurley picture pmurley  路  5Comments

orlangure picture orlangure  路  3Comments