Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct SIGTERM handling. Fixes #10518 #10337 #10033 #10490 #10520

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 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 Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we consider using the cmd.Context() as the root-context ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been working with this, and I'm not sure my PR will fix the issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope my work can help you 😄

#10490 (comment)

func() {
// this allows us to gracefully shutdown, capturing artifacts
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM)
defer cancel()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sxllwx I think you're suggesting:

  1. NotifyContext catches a SIGTERM (probably sent from the controller).
  2. This block will exit.
  3. stop() will be invoked, clearing the signal handler.
  4. Another SIGTERM occurs (controller again), but without a handler we get default behaviour.
  5. Default behaviour is to exit (does this mean crash?).

If that is what you think is correct, then my fix should work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I tried to look at the default processing method of Golang's runtime, and the relevant code:

https://github.com/golang/go/blob/master/src/runtime/signal_unix.go#L1068-L1074

https://github.com/golang/go/blob/master/src/runtime/signal_unix.go#L869-L875

Here's a test workflow I've been using for the past weekend, available for your use.

apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
   generateName: etcd-code-artifact-
spec:
   schedule: "*/1 * * * *"
   concurrencyPolicy: "Forbid"
   startingDeadlineSeconds: 0
   workflowSpec:
    entrypoint: run
    imagePullSecrets:
    - name: ccr-secret
    templates:
      - name: run
        inputs:
            artifacts:
            - name: repo-artifact
              path: /src
              git:
                repo: "https://github.com/etcd-io/etcd.git"
        container:
          image: "python:3.9-buster"
          command: [ sh, -c ]
          args: [ "git status && ls && pwd" ]
          workingDir: /src
        outputs:
          artifacts:
            - name: repo-artifact
              path: /src


// 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