Skip to content

Commit

Permalink
Use CommandContext to kill instead of manually doing it
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarbs authored and lornasong committed May 6, 2022
1 parent 2f70061 commit 27f77eb
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 28 deletions.
2 changes: 1 addition & 1 deletion tfexec/cmd.go
Expand Up @@ -171,7 +171,7 @@ func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
}

func (tf *Terraform) buildTerraformCmd(ctx context.Context, mergeEnv map[string]string, args ...string) *exec.Cmd {
cmd := exec.Command(tf.execPath, args...)
cmd := exec.CommandContext(ctx, tf.execPath, args...)

cmd.Env = tf.buildEnv(mergeEnv)
cmd.Dir = tf.workingDir
Expand Down
12 changes: 0 additions & 12 deletions tfexec/cmd_default.go
Expand Up @@ -15,18 +15,6 @@ func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
cmd.Stdout = mergeWriters(cmd.Stdout, tf.stdout)
cmd.Stderr = mergeWriters(cmd.Stderr, tf.stderr, &errBuf)

go func() {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded || ctx.Err() == context.Canceled {
if cmd != nil && cmd.Process != nil {
err := cmd.Process.Kill()
if err != nil {
tf.logger.Printf("error from kill: %s", err)
}
}
}
}()

// check for early cancellation
select {
case <-ctx.Done():
Expand Down
15 changes: 0 additions & 15 deletions tfexec/cmd_linux.go
Expand Up @@ -20,21 +20,6 @@ func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
Setpgid: true,
}

go func() {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded || ctx.Err() == context.Canceled {
if cmd != nil && cmd.Process != nil {
// send SIGINT to process group
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGINT)
if err != nil {
tf.logger.Printf("error from SIGINT: %s", err)
}
}

// TODO: send a kill if it doesn't respond for a bit?
}
}()

// check for early cancellation
select {
case <-ctx.Done():
Expand Down

0 comments on commit 27f77eb

Please sign in to comment.