Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add a cancel cause to job cancellation (#1728)
Miccah522b2fab2959
diff --git a/pkg/sources/job_progress.go b/pkg/sources/job_progress.goindex 9ed3b588d..4acbdd3c4 100644--- a/pkg/sources/job_progress.go+++ b/pkg/sources/job_progress.go@@ -71,11 +71,11 @@ func (r *JobProgressRef) Done() <-chan struct{} {// CancelRun requests that the job this is referencing is cancelled and stops// running. This method will have no effect if the job does not allow// cancellation.-func (r *JobProgressRef) CancelRun() {+func (r *JobProgressRef) CancelRun(cause error) {if r.jobProgress == nil || r.jobProgress.jobCancel == nil {return}- r.jobProgress.jobCancel()+ r.jobProgress.jobCancel(cause)}// Fatal is a wrapper around error to differentiate non-fatal errors from fatal@@ -108,7 +108,7 @@ type JobProgress struct {ctx context.Contextcancel context.CancelFunc// Requests to cancel the job.- jobCancel context.CancelFunc+ jobCancel context.CancelCauseFunc// Metrics.metrics JobProgressMetricsmetricsLock sync.Mutex@@ -150,7 +150,7 @@ func WithHooks(hooks ...JobProgressHook) func(*JobProgress) {}// WithCancel allows cancelling the job by the JobProgressRef.-func WithCancel(cancel context.CancelFunc) func(*JobProgress) {+func WithCancel(cancel context.CancelCauseFunc) func(*JobProgress) {return func(jp *JobProgress) { jp.jobCancel = cancel }}diff --git a/pkg/sources/source_manager.go b/pkg/sources/source_manager.goindex 199762758..383b8feb5 100644--- a/pkg/sources/source_manager.go+++ b/pkg/sources/source_manager.go@@ -170,7 +170,7 @@ func (s *SourceManager) asyncRun(ctx context.Context, handle handle) (JobProgresreturn JobProgressRef{SourceID: int64(handle), SourceName: sourceName}, err}// Create a JobProgress object for tracking progress.- ctx, cancel := context.WithCancel(ctx)+ ctx, cancel := context.WithCancelCause(ctx)progress := NewJobProgress(jobID, int64(handle), sourceName, WithHooks(s.hooks...), WithCancel(cancel))s.pool.Go(func() error {atomic.AddInt32(&s.currentRunningCount, 1)@@ -180,7 +180,7 @@ func (s *SourceManager) asyncRun(ctx context.Context, handle handle) (JobProgres"source_manager_worker_id", common.RandomID(5),)defer common.Recover(ctx)- defer cancel()+ defer cancel(nil)return s.run(ctx, handle, jobID, progress)})return progress.Ref(), nil@@ -247,6 +247,12 @@ func (s *SourceManager) run(ctx context.Context, handle handle, jobID int64, repreport.Start(time.Now())defer func() { report.End(time.Now()) }()+ defer func() {+ if err := context.Cause(ctx); err != nil {+ report.ReportError(Fatal{err})+ }+ }()+// Initialize the source.sourceInfo, ok := s.getSourceInfo(handle)if !ok {diff --git a/pkg/sources/source_manager_test.go b/pkg/sources/source_manager_test.goindex 9a7456b3a..9a196a897 100644--- a/pkg/sources/source_manager_test.go+++ b/pkg/sources/source_manager_test.go@@ -312,10 +312,12 @@ func TestSourceManagerCancelRun(t *testing.T) {ref, err := mgr.ScheduleRun(context.Background(), handle)assert.NoError(t, err)- ref.CancelRun()+ cancelErr := fmt.Errorf("abort! abort!")+ ref.CancelRun(cancelErr)<-ref.Done()assert.Error(t, ref.Snapshot().FatalError())assert.True(t, errors.Is(ref.Snapshot().FatalError(), returnedErr))+ assert.True(t, errors.Is(ref.Snapshot().FatalErrors(), cancelErr))}func TestSourceManagerAvailableCapacity(t *testing.T) {