Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@cangming
Copy link
Contributor

@cangming cangming commented Nov 23, 2025

Add RWMutex to protect concurrent map access in Set, Unset, and ForEach methods. Change New() to return *U pointer type for proper synchronization.

1. Why is this pull request needed and what does it do?

This PR fixes a data race in plugin/pkg/uniq that occurs during reload operations when multiple goroutines concurrently access the internal map without synchronization.

Problem: The Set(), Unset(), and ForEach() methods in plugin/pkg/uniq access a shared map without proper locking, causing race conditions detected by Go's race detector during TestReadme.

Solution: Added sync.RWMutex to protect all map operations:

  • Set(): Uses RLock for existence check, then Lock for write with double-check pattern
  • Unset(): Uses Lock for safe deletion
  • ForEach(): Uses Lock for iteration and modification
  • Changed New() to return *U pointer instead of U value for proper synchronization

Testing: All tests pass with -race flag enabled, including the previously failing TestReadme.

2. Which issues (if any) are related?

#7703

3. Which documentation changes (if any) need to be made?

None. This is an internal package (plugin/pkg/uniq) with no user-facing documentation.

4. Does this introduce a backward incompatible change or deprecation?

Yes, this introduces a breaking change:

New() now returns *U instead of U.

Internal compatibility: ✅ All CoreDNS internal usage (plugin/metrics and plugin/ready) is fully compatible because they use type inference (u := uniq.New()).

External impact: ⚠️ Third-party plugins using explicit type declarations may be affected:

var u uniq.U = uniq.New()  // ❌ Won't compile with new version
var u = uniq.New()         // ✅ Works (type inference)

Since plugin/pkg/uniq is an internal package not intended for external use, I believe this breaking change is acceptable. However, if backward compatibility is required, I can provide alternative solutions (e.g., deprecation period or dual API).

Add RWMutex to protect concurrent map access in Set, Unset, and ForEach methods.
Change New() to return *U pointer type for proper synchronization.

Signed-off-by: Cangming H <[email protected]>
Copy link
Collaborator

@thevilledev thevilledev left a comment

Choose a reason for hiding this comment

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

Hey,

Github Actions has been hitting this issue today super often:

Details WARNING: DATA RACE Write at 0x00c000486b40 by goroutine 296388: runtime.mapdelete_faststr() /opt/hostedtoolcache/go/1.25.4/x64/src/internal/runtime/maps/runtime_faststr_swiss.go:401 +0x0 github.com/coredns/coredns/plugin/pkg/uniq.U.Unset() /home/runner/work/coredns/coredns/plugin/pkg/uniq/uniq.go:28 +0xa4 github.com/coredns/coredns/plugin/metrics.(*Metrics).OnRestart() /home/runner/work/coredns/coredns/plugin/metrics/metrics.go:122 +0x56 github.com/coredns/coredns/plugin/metrics.(*Metrics).OnRestart-fm() :1 +0x1f github.com/coredns/caddy.(*Instance).Restart() /home/runner/go/pkg/mod/github.com/coredns/[email protected]/caddy.go:212 +0x33e github.com/coredns/coredns/plugin/reload.hook.func1() /home/runner/work/coredns/coredns/plugin/reload/reload.go:112 +0x584

Previous write at 0x00c000486b40 by goroutine 296380:
runtime.mapdelete_faststr()
/opt/hostedtoolcache/go/1.25.4/x64/src/internal/runtime/maps/runtime_faststr_swiss.go:401 +0x0
github.com/coredns/coredns/plugin/pkg/uniq.U.Unset()
/home/runner/work/coredns/coredns/plugin/pkg/uniq/uniq.go:28 +0xa4
github.com/coredns/coredns/plugin/metrics.(*Metrics).OnRestart()
/home/runner/work/coredns/coredns/plugin/metrics/metrics.go:122 +0x56
github.com/coredns/coredns/plugin/metrics.(*Metrics).OnRestart-fm()
:1 +0x1f
github.com/coredns/caddy.(*Instance).Restart()
/home/runner/go/pkg/mod/github.com/coredns/[email protected]/caddy.go:212 +0x33e
github.com/coredns/coredns/plugin/reload.hook.func1()
/home/runner/work/coredns/coredns/plugin/reload/reload.go:112 +0x584

LGTM, lets merge once the pipeline has finished and unblock failing pipelines on other PRs.

Thank you for the contribution!

@thevilledev thevilledev merged commit 0ef4b55 into coredns:master Nov 24, 2025
11 checks passed
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.

2 participants