2 Commits

Author SHA1 Message Date
Ingo Oppermann
36470072f4 Add timeout for waiting for state 2025-07-22 16:02:09 +02:00
Ingo Oppermann
5e1295b4c3 Check for error from Signal() 2025-07-22 15:52:58 +02:00

View File

@@ -372,10 +372,6 @@ func (p *process) setState(state stateType) (stateType, error) {
prevState := p.state.state
failed := false
if prevState == state {
return prevState, nil
}
if p.state.state == stateFinished {
switch state {
case stateStarting:
@@ -806,7 +802,12 @@ func (p *process) stop(wait bool, reason string) error {
}
// If the process in starting state, wait until the process has been started
start := time.Now()
for {
if time.Since(start) > 5*time.Second {
return nil
}
if p.getState() == stateStarting {
time.Sleep(100 * time.Millisecond)
continue
@@ -816,10 +817,12 @@ func (p *process) stop(wait bool, reason string) error {
}
// If the process is already in the finishing state, don't do anything
if state, _ := p.setState(stateFinishing); state == stateFinishing {
if p.getState() == stateFinishing {
return nil
}
p.setState(stateFinishing)
p.stopReasonLock.Lock()
p.stopReason = reason
p.stopReasonLock.Unlock()
@@ -857,26 +860,37 @@ func (p *process) stop(wait bool, reason string) error {
var err error
if runtime.GOOS == "windows" {
// Windows doesn't know the SIGINT
p.cmd.Process.Kill()
err = p.cmd.Process.Kill()
} else {
// First try to kill the process gracefully. On a SIGINT ffmpeg will exit
// normally as if "q" has been pressed.
p.cmd.Process.Signal(os.Interrupt)
err = p.cmd.Process.Signal(os.Interrupt)
}
// Set up a timer to kill the process with SIGKILL in case SIGINT didn't have
// an effect.
p.killTimerLock.Lock()
p.killTimer = time.AfterFunc(5*time.Second, func() {
p.cmd.Process.Kill()
p.stdout.Close()
})
p.killTimerLock.Unlock()
if err == nil {
p.killTimerLock.Lock()
p.killTimer = time.AfterFunc(5*time.Second, func() {
p.cmd.Process.Kill()
p.stdout.Close()
})
p.killTimerLock.Unlock()
}
if wait {
if err == nil && wait {
wg.Wait()
}
if err != nil {
p.parser.Parse([]byte(err.Error()))
p.debuglogger.WithFields(log.Fields{
"state": p.getStateString(),
"order": p.getOrder(),
"error": err,
}).Debug().Log("Stopping failed")
}
return err
}