Put child processes in their own session

The core can be gracefully shutdown by sending a SIGINT to its process.
However, this signal is also propagated to all child processes, i.e. the
forked ffmpeg processes. They will also be stopped and might reconnect.
This is not wanted. The core has to stop these processes.

The child process will now get their own session ID with setsid() before
replacing themselves with ffmpeg. This way they will not receive a SIGINT
that was meant only for the parent.
This commit is contained in:
Ingo Oppermann 2023-12-12 21:29:12 +01:00
parent 3af049785c
commit 88605a1f1e
No known key found for this signature in database
GPG Key ID: 2AB32426E9DD229E
3 changed files with 49 additions and 0 deletions

1
internal/.gitignore vendored
View File

@ -1,4 +1,5 @@
testhelper/ignoresigint/ignoresigint
testhelper/sigint/sigint
testhelper/sigintwait/sigintwait
testhelper/sigpropagate/sigpropagate
testhelper/ffmpeg/ffmpeg

View File

@ -0,0 +1,47 @@
package main
import (
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
)
func main() {
if len(os.Args) == 1 {
cmd := exec.Command(os.Args[0], "x")
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
stdout, _ := cmd.StdoutPipe()
cmd.Start()
go func() {
io.Copy(os.Stdout, stdout)
}()
}
sigcount := 0
// Wait for interrupt signal to gracefully shutdown the app
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-quit:
fmt.Printf("[%d] got SIGINT\n", os.Getpid())
sigcount++
if sigcount > 5 {
return
}
case <-ticker.C:
fmt.Printf("[%d] hello\n", os.Getpid())
}
}
}

View File

@ -564,6 +564,7 @@ func (p *process) start() error {
}
p.cmd = exec.Command(p.binary, args...)
p.cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
p.cmd.Env = []string{}
p.stdout, err = p.cmd.StderrPipe()