Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions pkg/cli/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,9 @@ func collectAuditAnalysisResults(ctx context.Context, run WorkflowRun, runOutput
if err := g.Wait(); err != nil {
return results, err
}
if ctx.Err() != nil {
return results, ctx.Err()
}
return results, nil
}

Expand Down Expand Up @@ -743,6 +746,9 @@ func launchJobDetailsAnalysis(g *errgroup.Group, gctx context.Context, results *
}
jobDetails, failedJobCount, err := fetchJobDetailsWithCounts(gctx, runID, verbose)
if err != nil {
if gctx.Err() != nil {
return gctx.Err()
}
auditLog.Printf("fetchJobDetailsWithCounts failed: %v", err)
if verbose {
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Failed to fetch job details: %v", err)))
Expand Down Expand Up @@ -839,6 +845,9 @@ func runAuditAnalysis[T any](g *errgroup.Group, gctx context.Context, verbose bo
}
value, err := fn()
if err != nil {
if gctx.Err() != nil {
return gctx.Err()
}
auditLog.Printf("%s failed: %v", name, err)
if verbose {
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("%s: %v", warning, err)))
Expand Down
25 changes: 25 additions & 0 deletions pkg/cli/audit_concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,28 @@ func TestRunAuditAnalysisSoftFailuresRemainNonFatal(t *testing.T) {
t.Fatal("expected setter not to be called on soft failure")
}
}

func TestRunAuditAnalysisReturnsCancellationForSoftFailuresWhenContextCanceled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
g, gctx := errgroup.WithContext(ctx)
called := false
started := make(chan struct{})

runAuditAnalysis(g, gctx, false, "test", "test warning", func(v int) {
called = true
}, func() (int, error) {
close(started)
<-gctx.Done()
return 0, errors.New("soft failure")
})

<-started
cancel()

if err := g.Wait(); !errors.Is(err, context.Canceled) {
t.Fatalf("expected context canceled error for canceled context soft failure, got %v", err)
}
if called {
t.Fatal("expected setter not to be called when context is canceled")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[/tdd] The new test covers the runAuditAnalysis cancellation path well, but the analogous soft-failure cancellation path in launchJobDetailsAnalysis (audit.go line 749–751) has no equivalent test. That branch has the same shape and the same risk.

💡 Suggested test sketch

Add a test that sets up a fake fetchJobDetailsWithCounts that blocks until gctx is done, then returns an error, and asserts g.Wait() returns context.Canceled. The pattern is identical to TestRunAuditAnalysisReturnsCancellationForSoftFailuresWhenContextCanceled.

@copilot please address this.

}
}
Loading