Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Run stopProcesses in a goroutine when called from the main goroutine
Browse files Browse the repository at this point in the history
The errors channel is unbuffered, so stopProcesses cannot deliver
errors if it is running from the same goroutine as the receiver.

Signed-off-by: Jan Dubois <[email protected]>
  • Loading branch information
jandubois committed May 11, 2021
1 parent 3b5b6bb commit 2776822
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions pkg/containerrun/containerrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ func RunWithTestChan(
// signals.

active = false
stopProcesses(processRegistry, errors)
// Run asynchronously so we can receive errors
go stopProcesses(processRegistry, errors)
}
case ProcessStart:
if !active {
Expand Down Expand Up @@ -277,12 +278,6 @@ func handlePacket(
}

func stopProcesses(processRegistry *ProcessRegistry, errors chan<- error) {
// Run the code in a separate goroutine because the errors channel is
// unbuffered, so we can't write to it from the same goroutine that
// will be reading from it.
var wg sync.WaitGroup
wg.Add(1)
go func() {
log.Debugln("sending SIGTERM")
for _, err := range processRegistry.SignalAll(syscall.SIGTERM) {
errors <- err
Expand All @@ -293,9 +288,6 @@ func stopProcesses(processRegistry *ProcessRegistry, errors chan<- error) {
log.Debugln("timeout SIGTERM")
processRegistry.KillAll()
})
wg.Done()
}()
wg.Wait()
}

func startProcesses(
Expand Down

0 comments on commit 2776822

Please sign in to comment.