-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: add graceful shutdown with signal handling #9242
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/aquasecurity/trivy/pkg/log" | ||
| ) | ||
|
|
||
| // NotifyContext returns a context that is canceled when SIGINT or SIGTERM is received. | ||
| // It also ensures cleanup of temporary files when the signal is received. | ||
| // | ||
| // When a signal is received, Trivy will attempt to gracefully shut down by canceling | ||
| // the context and waiting for all operations to complete. If users want to force an | ||
| // immediate exit, they can send a second SIGINT or SIGTERM signal. | ||
| func NotifyContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
| ctx, stop := signal.NotifyContext(parent, os.Interrupt, syscall.SIGTERM) | ||
|
|
||
| // Start a goroutine to handle cleanup when context is done | ||
| go func() { | ||
| <-ctx.Done() | ||
|
|
||
| // Log that we're shutting down gracefully | ||
| log.Info("Received signal, attempting graceful shutdown...") | ||
| log.Info("Press Ctrl+C again to force exit") | ||
|
|
||
| // TODO: Add any necessary cleanup logic here | ||
|
|
||
| // Clean up signal handling | ||
| // After calling stop(), a second signal will cause immediate termination | ||
| stop() | ||
| }() | ||
|
|
||
| return ctx, stop | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ import ( | |||||||||
| "github.com/aquasecurity/trivy/pkg/log" | ||||||||||
| "github.com/aquasecurity/trivy/pkg/remote" | ||||||||||
| "github.com/aquasecurity/trivy/pkg/version/doc" | ||||||||||
| xio "github.com/aquasecurity/trivy/pkg/x/io" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| const ( | ||||||||||
|
|
@@ -188,7 +189,7 @@ func (a *Artifact) download(ctx context.Context, layer v1.Layer, fileName, dir s | |||||||||
| }() | ||||||||||
|
|
||||||||||
| // Download the layer content into a temporal file | ||||||||||
| if _, err = io.Copy(f, pr); err != nil { | ||||||||||
| if _, err = xio.Copy(ctx, f, pr); err != nil { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to overwrite trivy/pkg/fanal/walker/cached_file.go Lines 46 to 49 in 13e72ec
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, possibly, as I mentioned in the PR description. Since |
||||||||||
| return xerrors.Errorf("copy error: %w", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package io | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCopy(t *testing.T) { | ||
| t.Run("successful copy", func(t *testing.T) { | ||
| ctx := t.Context() | ||
| src := strings.NewReader("hello world") | ||
| dst := &bytes.Buffer{} | ||
|
|
||
| n, err := Copy(ctx, dst, src) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, int64(11), n) | ||
| assert.Equal(t, "hello world", dst.String()) | ||
| }) | ||
|
|
||
| t.Run("context canceled before read", func(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| cancel() // Cancel immediately | ||
|
|
||
| src := strings.NewReader("hello world") | ||
| dst := &bytes.Buffer{} | ||
|
|
||
| n, err := Copy(ctx, dst, src) | ||
| require.ErrorIs(t, err, context.Canceled) | ||
| assert.Equal(t, int64(0), n) | ||
| assert.Empty(t, dst.String()) | ||
| }) | ||
|
|
||
| t.Run("context canceled during read", func(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(t.Context()) | ||
|
|
||
| // Create a reader that will be canceled after first read | ||
| reader := &dummyReader{ | ||
| cancel: cancel, // Cancel after first read | ||
| } | ||
| dst := &bytes.Buffer{} | ||
|
|
||
| n, err := Copy(ctx, dst, reader) | ||
| require.ErrorIs(t, err, context.Canceled) | ||
| // Should have written first chunk before cancellation | ||
| assert.Equal(t, int64(5), n) | ||
| assert.Equal(t, "dummy", dst.String()) | ||
| }) | ||
| } | ||
|
|
||
| // dummyReader returns the same data on every Read call | ||
| type dummyReader struct { | ||
| cancel context.CancelFunc | ||
| } | ||
|
|
||
| func (r *dummyReader) Read(p []byte) (int, error) { | ||
| n := copy(p, "dummy") | ||
| if r.cancel != nil { | ||
| r.cancel() // Simulate cancellation after first read | ||
| } | ||
| return n, nil | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.