Skip to content

Commit

Permalink
fix: improve the signal handling (#795)
Browse files Browse the repository at this point in the history
exec.CommandContext exits a command by SIGKILL immediately, so the command can't exit gracefully.
So let's use suzuki-shunsuke/go-timeout instead.
go-timeout exits a command by SIGINT gracefully.
  • Loading branch information
suzuki-shunsuke authored May 23, 2023
1 parent 57c3da0 commit 86a03ac
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/suzuki-shunsuke/github-comment-metadata v0.1.0
github.com/suzuki-shunsuke/go-ci-env/v3 v3.0.1
github.com/suzuki-shunsuke/go-findconfig v1.1.1
github.com/suzuki-shunsuke/go-timeout v1.0.0
github.com/urfave/cli/v2 v2.25.3
golang.org/x/oauth2 v0.8.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMK
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand All @@ -63,6 +64,7 @@ github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/suzuki-shunsuke/github-comment-metadata v0.1.0 h1:89uGvBINoWZ4p2dGj7S695bm/L1H175eMp/D47ltSPs=
Expand All @@ -71,6 +73,8 @@ github.com/suzuki-shunsuke/go-ci-env/v3 v3.0.1 h1:deCm9of48iwRq0Axg3ne8mx/26h3ok
github.com/suzuki-shunsuke/go-ci-env/v3 v3.0.1/go.mod h1:VmLj5u0w7Yf/IJIzZ+TWiB7mVT3pRKPMeb0Jssk7YsA=
github.com/suzuki-shunsuke/go-findconfig v1.1.1 h1:7jBTjf7RjfvRC6XNpCKCJC6y8hHVYPVVefOQHQPHRVw=
github.com/suzuki-shunsuke/go-findconfig v1.1.1/go.mod h1:u/0Zz6/GDE6G0gofzVhR9UPOIKLSUoDMjUoFWqOoVlg=
github.com/suzuki-shunsuke/go-timeout v1.0.0 h1:SWiJc8HuLWK4N01x76XUg75rbbwDiHiFoyPbwHcNI4Q=
github.com/suzuki-shunsuke/go-timeout v1.0.0/go.mod h1:ZX3QIig4tvTVXnZUJE3ivC9qy5TJy9Uk5sIcvwjwSNE=
github.com/urfave/cli/v2 v2.25.3 h1:VJkt6wvEBOoSjPFQvOkv6iWIrsJyCrKGtCtxXWwmGeY=
github.com/urfave/cli/v2 v2.25.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"

"github.com/mattn/go-colorable"
"github.com/suzuki-shunsuke/go-timeout/timeout"
"github.com/suzuki-shunsuke/tfcmt/pkg/apperr"
"github.com/suzuki-shunsuke/tfcmt/pkg/notifier"
"github.com/suzuki-shunsuke/tfcmt/pkg/platform"
Expand All @@ -33,7 +34,7 @@ func (ctrl *Controller) Apply(ctx context.Context, command Command) error {
return errors.New("no notifier specified at all")
}

cmd := exec.CommandContext(ctx, command.Cmd, command.Args...) //nolint:gosec
cmd := exec.Command(command.Cmd, command.Args...) //nolint:gosec
cmd.Stdin = os.Stdin
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
Expand All @@ -43,7 +44,7 @@ func (ctrl *Controller) Apply(ctx context.Context, command Command) error {
uncolorizedCombinedOutput := colorable.NewNonColorable(combinedOutput)
cmd.Stdout = io.MultiWriter(os.Stdout, uncolorizedStdout, uncolorizedCombinedOutput)
cmd.Stderr = io.MultiWriter(os.Stderr, uncolorizedStderr, uncolorizedCombinedOutput)
_ = cmd.Run()
_ = timeout.NewRunner(0).Run(ctx, cmd)

return apperr.NewExitError(ntf.Apply(ctx, &notifier.ParamExec{
Stdout: stdout.String(),
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"

"github.com/mattn/go-colorable"
"github.com/suzuki-shunsuke/go-timeout/timeout"
"github.com/suzuki-shunsuke/tfcmt/pkg/apperr"
"github.com/suzuki-shunsuke/tfcmt/pkg/notifier"
"github.com/suzuki-shunsuke/tfcmt/pkg/platform"
Expand All @@ -33,7 +34,7 @@ func (ctrl *Controller) Plan(ctx context.Context, command Command) error {
return errors.New("no notifier specified at all")
}

cmd := exec.CommandContext(ctx, command.Cmd, command.Args...) //nolint:gosec
cmd := exec.Command(command.Cmd, command.Args...) //nolint:gosec
cmd.Stdin = os.Stdin
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
Expand All @@ -43,7 +44,7 @@ func (ctrl *Controller) Plan(ctx context.Context, command Command) error {
uncolorizedCombinedOutput := colorable.NewNonColorable(combinedOutput)
cmd.Stdout = io.MultiWriter(os.Stdout, uncolorizedStdout, uncolorizedCombinedOutput)
cmd.Stderr = io.MultiWriter(os.Stderr, uncolorizedStderr, uncolorizedCombinedOutput)
_ = cmd.Run()
_ = timeout.NewRunner(0).Run(ctx, cmd)

return apperr.NewExitError(ntf.Plan(ctx, &notifier.ParamExec{
Stdout: stdout.String(),
Expand Down

0 comments on commit 86a03ac

Please sign in to comment.