Skip to content

[Code Quality] Migrate collectAuditAnalysisResults from sync.WaitGroup to errgroup.WithContext #46942

Description

@github-actions

Description

collectAuditAnalysisResults in pkg/cli/audit.go uses a raw sync.WaitGroup to fan out ~14 goroutines. This has two problems:

  1. Goroutine errors are swallowed — there's no way for the caller to distinguish success from silent failures
  2. When combined with context propagation (task for adding ctx), errors from the errgroup context cancellation won't surface

The codebase already uses errgroup.WithContext correctly in pkg/cli/mcp_inspect_inspector.go, and golang.org/x/sync/errgroup is already a direct dependency.

Suggested Changes

  1. Replace var wg sync.WaitGroup in collectAuditAnalysisResults with g, gctx := errgroup.WithContext(ctx)
  2. Change all wg.Go(func() { ... }) callsites to g.Go(func() error { ...; return nil })
  3. Pass gctx into all launch* helpers so cancellation propagates
  4. Add import "golang.org/x/sync/errgroup" to audit.go
  5. Remove sync import if no longer needed

Soft failures (goroutine I/O errors) should log and return nil to preserve current non-fatal behavior; hard cancellations surface via g.Wait().

Files Affected

  • pkg/cli/audit.gocollectAuditAnalysisResults and all launch* helpers (lines 656–805)

Success Criteria

  • errgroup.WithContext used in place of raw sync.WaitGroup
  • All goroutines receive gctx for cancellation
  • make build and make test pass

Source

Extracted from Audit Goroutine Context Blindness discussion #46848

Priority

Medium — improves error propagation and cancellability

🔍 Task mining by Discussion Task Miner - Code Quality Improvement Agent · 46.7 AIC · ⌖ 7.94 AIC · ⊞ 7K ·

  • expires on Jul 21, 2026, 5:17 PM UTC-08:00

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions