-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly handle context cancellation/deadline and kill
- Loading branch information
Showing
20 changed files
with
129 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// +build !linux | ||
|
||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error { | ||
var errBuf strings.Builder | ||
|
||
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 && cmd.ProcessState != nil { | ||
err := cmd.Process.Kill() | ||
if err != nil { | ||
tf.logger.Printf("error from kill: %s", err) | ||
} | ||
} | ||
} | ||
}() | ||
|
||
// check for early cancellation | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
} | ||
|
||
err := cmd.Run() | ||
if err == nil && ctx.Err() != nil { | ||
err = ctx.Err() | ||
} | ||
if err != nil { | ||
return tf.parseError(err, errBuf.String()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error { | ||
var errBuf strings.Builder | ||
|
||
cmd.Stdout = mergeWriters(cmd.Stdout, tf.stdout) | ||
cmd.Stderr = mergeWriters(cmd.Stderr, tf.stderr, &errBuf) | ||
|
||
cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
// kill children if parent is dead | ||
Pdeathsig: syscall.SIGKILL, | ||
// set process group ID | ||
Setpgid: true, | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
if ctx.Err() == context.DeadlineExceeded || ctx.Err() == context.Canceled { | ||
if cmd != nil && cmd.Process != nil && cmd.ProcessState != 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(): | ||
return ctx.Err() | ||
default: | ||
} | ||
|
||
err := cmd.Run() | ||
if err == nil && ctx.Err() != nil { | ||
err = ctx.Err() | ||
} | ||
if err != nil { | ||
return tf.parseError(err, errBuf.String()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters