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

Disable SIGINT context cancelling on Linux #670

Closed
n-ozerov opened this issue Jul 25, 2020 · 3 comments
Closed

Disable SIGINT context cancelling on Linux #670

n-ozerov opened this issue Jul 25, 2020 · 3 comments

Comments

@n-ozerov
Copy link

n-ozerov commented Jul 25, 2020

My code is a pipeline with several workerpools. One of them is controlling headless chrome via chromedp. On windows code work flawless (i added manual graceful shutdown), on linux i just got context canceled on all pending tasks to browser.

Is there any way to disable force-kiliing Chrome process without using RemoteAllocator? I tried to comment allocateCmdOptions in allocate_linux with no luck.

UPD: checked kill command (sigterm) work just fine, but ctrl+c is not.

@n-ozerov n-ozerov changed the title Disable SIGTERM context cancelling on Linux Disable SIGINT context cancelling on Linux Jul 25, 2020
@pmurley
Copy link
Contributor

pmurley commented Jul 31, 2020

I am interested in this as well, as I'd like to gracefully handle SIGINT/SIGTERM in my harness program, but I can't right now because it squashes the browsers managed by chromedp.

There's a simple patch that could be applied in allocate.go that prevents the signals from hitting the browsers directly. As I understand it, you add them to the process group of the parent (harness) process:

diff --git a/allocate.go b/allocate.go
index 5920cff..bd128d1 100644
--- a/allocate.go
+++ b/allocate.go
@@ -12,6 +12,7 @@ import (
        "os/exec"
        "path/filepath"
        "sync"
+       "syscall"
        "time"
 )
 
@@ -181,6 +182,10 @@ func (a *ExecAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (*B
                cmd.Env = append(os.Environ(), a.initEnv...)
        }
 
+       cmd.SysProcAttr = &syscall.SysProcAttr{
+               Setpgid: true,
+       }
+
        // We must start the cmd before calling cmd.Wait, as otherwise the two
        // can run into a data race.
        if err := cmd.Start(); err != nil {

However, I'm not sure they will want to apply this, because it also has the effect of causing browsers to remain open after a typical harness is closed with SIGTERM/SIGINT, which might be a problem for some other users.

@mvdan -- Would you be open to a pull request for an ExecAllocatorOption that allows for adding the browser to the process group of the parent process in this way? If not, any thoughts on other ways to handle this situation?

@mvdan
Copy link
Contributor

mvdan commented Aug 2, 2020

I think we could expose an ExecAllocatorOption that would take a function to modify an *exec.Command. The default could be the same that we have today; set cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL on Linux and the equivalent elsewhere. Your option could reset the entire SysProcAttr or set whatever fields you like.

And yeah, a PR would be welcome.

@ZekeLu
Copy link
Member

ZekeLu commented May 9, 2021

#674 has been merged. Closing this issue.

@ZekeLu ZekeLu closed this as completed May 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants