From dd2785ff083590679dd08c06f05055ea7c6e006a Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 6 May 2019 12:26:22 +0200 Subject: [PATCH] BuildCommand.Run: avoid triggering a cancellation on termination --- command/build.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/command/build.go b/command/build.go index ddd606049..9fac4b9db 100644 --- a/command/build.go +++ b/command/build.go @@ -26,19 +26,22 @@ type BuildCommand struct { } func (c *BuildCommand) Run(args []string) int { - buildCtx, cancelCtx := context.WithCancel(context.Background()) + buildCtx, cancelBuildCtx := context.WithCancel(context.Background()) // Handle interrupts for this build sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) defer func() { + cancelBuildCtx() signal.Stop(sigCh) close(sigCh) }() go func() { - sig := <-sigCh - - c.Ui.Error(fmt.Sprintf("Cancelling build after receiving %s", sig)) - cancelCtx() + select { + case sig := <-sigCh: + c.Ui.Error(fmt.Sprintf("Cancelling build after receiving %s", sig)) + cancelBuildCtx() + case <-buildCtx.Done(): + } }() return c.RunContext(buildCtx, args)