Skip to content

refactor: replace creachadair/taskgroup with x/sync/errgroup and ban it - #1333

Merged
lklimek merged 1 commit into
v1.6-devfrom
refactor/replace-taskgroup-errgroup
Jun 12, 2026
Merged

lklimek merged 1 commit into
v1.6-devfrom
refactor/replace-taskgroup-errgroup

Conversation

@lklimek

@lklimek lklimek commented Jun 12, 2026 •

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

golang.org/x/sync/errgroup is already a direct dependency
(go.mod has golang.org/x/sync v0.20.0), so swapping to it and removing
taskgroup is a net dependency removal — one fewer module in go.mod/go.sum.

What was done?

Replaced taskgroup with golang.org/x/sync/errgroup at the only two call sites,
dropped the dependency, and added a CI-enforced ban so it can't return.

1. internal/mempool/mempool.go (recheck loop) — trivial drop-in.
taskgroup.New(nil).Limit(2*runtime.NumCPU()) becomes a zero-value
errgroup.Group with SetLimit(2*runtime.NumCPU()); start(...) becomes
g.Go(...). errgroup.Go blocks until a worker slot frees up, exactly
reproducing taskgroup's Limit+start blocking-when-full behavior. The
launch-loop → Flush → Wait ordering and the task body are unchanged, and
the result is still discarded (_ = g.Wait()).

2. scripts/keymigrate/migrate.go (Migrate) — one concurrency subtlety.
taskgroup invoked its error handler serially from a single goroutine, which
is why the original errs = append(...) aggregation was lock-free. errgroup runs
task funcs concurrently, so the aggregation is now guarded by a sync.Mutex
to avoid a data race. The point of the original handler was to collect all
errors (not just the first), so that behavior is preserved — every failing task
appends its error before returning. The exact error message format
("encountered errors during migration: %q") is unchanged. Verified clean under
go test -race.

3. Dependency drop. go mod tidy removes
github.com/creachadair/taskgroup v0.14.2 from both go.mod and go.sum
(the shared creachadair/mds transitive dep stays — it's pulled by other
creachadair/* modules).

4. CI-enforced ban (.golangci.yml). Enabled the gomodguard linter
(golangci-lint v2 schema, matching the pinned @v2.8) with a single
blocked-modules entry for taskgroup — recommending golang.org/x/sync/errgroup
and citing the trust reason. No allowed list, so no other module is affected.
Any future taskgroup import now fails lint.

How Has This Been Tested?

  • make build — green (BLS native dependency + tenderdash binary link).
  • go vet ./internal/mempool/... ./scripts/keymigrate/... — clean.
  • go test -race -count=1 -tags deadlock ./internal/mempool/... — pass.
  • go test -race -count=1 ./scripts/keymigrate/... — pass (mutex prevents the
    aggregation data race).
  • [email protected] config verify — config validates against the v2.8 schema.
  • [email protected] — no gomodguard violation (taskgroup is gone), and the
    ban was positively verified: temporarily re-adding the import produces
    import of package github.com/creachadair/taskgroup is blocked ... golang.org/x/sync/errgroup is a recommended module (gomodguard).
  • gofmt -l / goimports -local github.com/dashpay/tenderdash — clean on both
    edited files.
  • go mod tidy leaves no further diff; taskgroup absent from go.mod/go.sum.

Breaking Changes

None. Internal/tooling refactor only — no public API, wire format, or behavior
change. Concurrency semantics at both call sites are preserved.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Removed an external dependency and migrated concurrent task orchestration throughout the codebase to an alternative library.
    • Updated linting rules to prevent reintroduction of the removed dependency and enforce recommended practices.

Drop github.com/creachadair/taskgroup in favor of the already-direct
golang.org/x/sync/errgroup dependency, a net dependency removal. The team
no longer trusts taskgroup on supply-chain grounds (upstream ships an
anti-AI canary string in its README); a supply-chain audit found the code
clean but the project is being dropped on trust.

Two call sites:
- internal/mempool/mempool.go: trivial drop-in. SetLimit + Go reproduces
  taskgroup's Limit + start blocking-when-full behavior; launch/Flush/Wait
  ordering and the task body are unchanged.
- scripts/keymigrate/migrate.go: errgroup runs task funcs concurrently, so
  the error aggregation that taskgroup invoked serially is now guarded by a
  sync.Mutex to avoid a data race. Collect-all-errors semantics and the
  exact error message format are preserved.

Add a CI-enforced gomodguard ban so taskgroup cannot return.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@lklimek
lklimek requested a review from QuantumExplorer as a code owner June 12, 2026 10:39
@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR migrates the codebase away from the external github.com/creachadair/taskgroup dependency to the standard library-based golang.org/x/sync/errgroup. The change is enforced by linter configuration, removes the dependency declaration, and updates two implementation sites with corrected concurrency and error-handling patterns.

Changes

Taskgroup to errgroup migration

Layer / File(s) Summary
Linter enforcement and dependency removal
.golangci.yml, go.mod
Enable gomodguard linter to block taskgroup imports and recommend errgroup; remove taskgroup v0.14.2 from the require list.
Mempool concurrent recheck with errgroup
internal/mempool/mempool.go
Update mempool's transaction recheck phase to use errgroup.Group with SetLimit(2 * runtime.NumCPU()) for per-transaction concurrent tasks.
Key migration concurrent processing with error aggregation
scripts/keymigrate/migrate.go
Replace taskgroup with errgroup and mutex-protected errs slice; each migration goroutine checks context state, records failures to the shared slice, and returns errors to the group.

🎯 2 (Simple) | ⏱️ ~12 minutes

A rabbit hops through dependency trees,
Swapping taskgroup for errgroup with ease,
Mutex guards errors with careful lock,
Standard library replaces the creak and knock,
Concurrency flows through a cleaner flock. 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately summarizes the main change: replacing creachadair/taskgroup with x/sync/errgroup and adding a ban on the module.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/replace-taskgroup-errgroup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/mempool/mempool.go (1)

787-815: ⚠️ Potential issue | 🟠 Major

internal/mempool/mempool.go (lines 787-815): errgroup SetLimit backpressures spawning and delays Flush
With g.SetLimit(2 * runtime.NumCPU()), the loop that calls g.Go(...) can block once 2*NumCPU rechecks are in-flight, so txmp.proxyAppConn.Flush(ctx) is no longer guaranteed to run only after enqueueing all CheckTx rechecks (it effectively happens per batch).
Ensure the abciclient.Client implementation doesn’t rely on Flush to drive/complete queued CheckTx; otherwise this can deadlock or materially hurt recheck availability. Prefer moving concurrency control into the workers (worker pool / semaphore) so the submission loop can reach Flush deterministically.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/mempool/mempool.go` around lines 787 - 815, The current use of
errgroup.SetLimit(2 * runtime.NumCPU()) causes the g.Go spawning loop to block
once the limit is reached, so txmp.proxyAppConn.Flush(ctx) may run before all
CheckTx requests are enqueued; move concurrency throttling out of the errgroup
spawn path so the submission loop always enqueues all wtxs then Flush is called.
Specifically, remove or avoid using g.SetLimit for controlling CheckTx
parallelism and instead apply a worker-pool/semaphore inside the goroutine body
(e.g., a buffered channel or semaphore acquired before calling CheckTx and
released after handleRecheckResult completes), keep using g.Go/g.Wait only to
wait for completion, and ensure txmp.proxyAppConn.Flush(ctx) is called after the
for loop that enqueues all wtxs; reference symbols: g.SetLimit, g.Go, g.Wait,
txmp.proxyAppConn.Flush, wtxs, handleRecheckResult.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@internal/mempool/mempool.go`:
- Around line 787-815: The current use of errgroup.SetLimit(2 *
runtime.NumCPU()) causes the g.Go spawning loop to block once the limit is
reached, so txmp.proxyAppConn.Flush(ctx) may run before all CheckTx requests are
enqueued; move concurrency throttling out of the errgroup spawn path so the
submission loop always enqueues all wtxs then Flush is called. Specifically,
remove or avoid using g.SetLimit for controlling CheckTx parallelism and instead
apply a worker-pool/semaphore inside the goroutine body (e.g., a buffered
channel or semaphore acquired before calling CheckTx and released after
handleRecheckResult completes), keep using g.Go/g.Wait only to wait for
completion, and ensure txmp.proxyAppConn.Flush(ctx) is called after the for loop
that enqueues all wtxs; reference symbols: g.SetLimit, g.Go, g.Wait,
txmp.proxyAppConn.Flush, wtxs, handleRecheckResult.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 16761fcf-dace-4bd0-a64e-65d7da3d53d7

📥 Commits

Reviewing files that changed from the base of the PR and between 2504c1f and ee68e78.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (4)
  • .golangci.yml
  • go.mod
  • internal/mempool/mempool.go
  • scripts/keymigrate/migrate.go
💤 Files with no reviewable changes (1)
  • go.mod

@lklimek
lklimek merged commit 757877f into v1.6-dev Jun 12, 2026
24 of 28 checks passed
@lklimek
lklimek deleted the refactor/replace-taskgroup-errgroup branch June 12, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant