Skip to content

Commit f42f9b7

Browse files
authored
Cancel context on os.Interrupt (#169)
This fixes #117 by cancelling the global context when Ctrl-C is hit. The cancelation propagates down to `runAction` where containers that are still running are killed.
1 parent 3011e0c commit f42f9b7

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

cmd/src/actions_exec.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io/ioutil"
1111
"os"
1212
"os/exec"
13+
"os/signal"
1314
"path/filepath"
1415
"regexp"
1516
"runtime"
@@ -183,7 +184,20 @@ Format of the action JSON files:
183184
return errors.Wrap(err, "invalid JSON action file")
184185
}
185186

186-
ctx := context.Background()
187+
ctx, cancel := context.WithCancel(context.Background())
188+
c := make(chan os.Signal, 1)
189+
signal.Notify(c, os.Interrupt)
190+
defer func() {
191+
signal.Stop(c)
192+
cancel()
193+
}()
194+
go func() {
195+
select {
196+
case <-c:
197+
cancel()
198+
case <-ctx.Done():
199+
}
200+
}()
187201

188202
logger := newActionLogger(*verbose, *keepLogsFlag)
189203

0 commit comments

Comments
 (0)