Skip to content

Commit

Permalink
fix: Correct SIGTERM handling. Fixes #10518 #10337 #10033 #10490 (#10523
Browse files Browse the repository at this point in the history
)

Signed-off-by: Alex Collins <alex_collins@intuit.com>
  • Loading branch information
alexec authored and terrytangyuan committed Mar 29, 2023
1 parent 4978d3b commit fe522b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
24 changes: 8 additions & 16 deletions cmd/argoexec/commands/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package commands

import (
"context"
"os/signal"
"syscall"
"time"

"github.com/argoproj/pkg/stats"
Expand All @@ -16,7 +14,7 @@ func NewWaitCommand() *cobra.Command {
Use: "wait",
Short: "wait for main container to finish and save artifacts",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
ctx := cmd.Context()
err := waitContainer(ctx)
if err != nil {
log.Fatalf("%+v", err)
Expand All @@ -32,20 +30,14 @@ func waitContainer(ctx context.Context) error {
defer stats.LogStats()
stats.StartStatsTicker(5 * time.Minute)

// use a function to constrain the scope of ctx
func() {
// this allows us to gracefully shutdown, capturing artifacts
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM)
defer cancel()

// Wait for main container to complete
err := wfExecutor.Wait(ctx)
if err != nil {
wfExecutor.AddError(err)
}
}()
// Wait for main container to complete
err := wfExecutor.Wait(ctx)
if err != nil {
wfExecutor.AddError(err)
}
ctx = context.Background() // don't allow cancellation to impact capture of results, parameters,or artifacts
// Capture output script result
err := wfExecutor.CaptureScriptResult(ctx)
err = wfExecutor.CaptureScriptResult(ctx)
if err != nil {
wfExecutor.AddError(err)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/argoexec/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/argoproj/argo-workflows/v3/util/errors"

Expand All @@ -13,7 +16,9 @@ import (
)

func main() {
err := commands.NewRootCommand().Execute()
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer stop()
err := commands.NewRootCommand().ExecuteContext(ctx)
if err != nil {
if exitError, ok := err.(errors.Exited); ok {
if exitError.ExitCode() >= 0 {
Expand Down

0 comments on commit fe522b6

Please sign in to comment.