From 3f9b0ccd0f535fda0dec64aa65a7c91dc69bfbad Mon Sep 17 00:00:00 2001 From: Tom Sightler Date: Fri, 5 Jul 2024 17:27:50 -0400 Subject: [PATCH] Fix exec race/hang on exit --- internal/exec/closer.go | 12 +++++++++++- internal/exec/exec.go | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/exec/closer.go b/internal/exec/closer.go index 66d0e3ac..bcd8756f 100644 --- a/internal/exec/closer.go +++ b/internal/exec/closer.go @@ -15,6 +15,7 @@ import ( type closer struct { cmd *exec.Cmd query url.Values + done chan error } func (c *closer) Close() (err error) { @@ -35,5 +36,14 @@ func (c *closer) Close() (err error) { defer timer.Stop() // stop timer if Wait ends before timeout } - return errors.Join(err, c.cmd.Wait()) + if c.done != nil { + select { + case doneErr := <-c.done: + err = errors.Join(err, doneErr) + } + } else { + err = errors.Join(err, c.cmd.Wait()) + } + + return err } diff --git a/internal/exec/exec.go b/internal/exec/exec.go index 035317d9..2b7a3b00 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -158,6 +158,8 @@ func handleRTSP(source string, cmd *exec.Cmd, cl io.Closer, path string) (core.P done <- cmd.Wait() }() + cl.(*closer).done = done + select { case <-time.After(time.Minute): log.Error().Str("source", source).Msg("[exec] timeout")