Skip to content

Conversation

bn-smayr
Copy link
Contributor

@bn-smayr bn-smayr commented Jul 1, 2025

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

This fixes a data race where one goroutine refreshes a cached message while another one serves the same message from the cache. Since the refreshed message is modified after insertion into the cache, this leads to the following data race:

WARNING: DATA RACE
Write at 0x00c000ba7d14 by goroutine 1894:
  github.com/coredns/coredns/plugin/cache.filterRRSlice()
      coredns/plugin/cache/dnssec.go:20 +0x264
  github.com/coredns/coredns/plugin/cache.(*ResponseWriter).WriteMsg()
      coredns/plugin/cache/cache.go:219 +0x938
  github.com/coredns/coredns/plugin/forward.(*Forward).ServeDNS()
      coredns/plugin/forward/forward.go:218 +0x1879
  github.com/coredns/coredns/plugin.NextOrFailure()
      coredns/plugin/plugin.go:80 +0x2dd
  github.com/coredns/coredns/plugin/cache.(*Cache).doRefresh()
      coredns/plugin/cache/handler.go:107 +0x158e
  github.com/coredns/coredns/plugin/cache.(*Cache).ServeDNS()
      coredns/plugin/cache/handler.go:41 +0x1503
  github.com/coredns/coredns/plugin.NextOrFailure()
      coredns/plugin/plugin.go:80 +0x2dd
  github.com/coredns/coredns/plugin/log.Logger.ServeDNS()
      coredns/plugin/log/log.go:36 +0x3b3
  github.com/coredns/coredns/plugin/log.(*Logger).ServeDNS()
      <autogenerated>:1 +0x104
  github.com/coredns/coredns/core/dnsserver.(*Server).ServeDNS()
      coredns/core/dnsserver/server.go:364 +0xcf5
  ...

Previous read at 0x00c000ba7d10 by goroutine 1896:
  github.com/miekg/dns.(*A).copy()
      pkg/mod/github.com/miekg/dns@v1.1.66/ztypes.go:833 +0xe4
  github.com/miekg/dns.Copy()
      pkg/mod/github.com/miekg/dns@v1.1.66/msg.go:1065 +0x194
  github.com/coredns/coredns/plugin/cache.filterRRSlice()
      coredns/plugin/cache/dnssec.go:16 +0x7e
  github.com/coredns/coredns/plugin/cache.(*item).toMsg()
      coredns/plugin/cache/item.go:90 +0x668
  github.com/coredns/coredns/plugin/cache.(*Cache).ServeDNS()
      coredns/plugin/cache/handler.go:79 +0x1064
  github.com/coredns/coredns/plugin.NextOrFailure()
      coredns/plugin/plugin.go:80 +0x2dd
  github.com/coredns/coredns/plugin/log.Logger.ServeDNS()
      coredns/plugin/log/log.go:36 +0x3b3
  github.com/coredns/coredns/plugin/log.(*Logger).ServeDNS()
      <autogenerated>:1 +0x104
  github.com/coredns/coredns/core/dnsserver.(*Server).ServeDNS()
      coredns/core/dnsserver/server.go:364 +0xcf5
  ...

It's pretty trivial to fix by mutating the refreshed message before insertion into the cache.

2. Which issues (if any) are related?

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

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

Avoid a data race when refreshing cached messages by making all
necessary adjustments before insertion into the cache.

Signed-off-by: Sebastian Mayr <smayr@barracuda.com>
Copy link

codecov bot commented Jul 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.00%. Comparing base (93c57b6) to head (f1284aa).
Report is 1518 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7398      +/-   ##
==========================================
+ Coverage   55.70%   60.00%   +4.30%     
==========================================
  Files         224      273      +49     
  Lines       10016    17982    +7966     
==========================================
+ Hits         5579    10790    +5211     
- Misses       3978     6564    +2586     
- Partials      459      628     +169     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yongtang yongtang merged commit ae5e03a into coredns:master Jul 3, 2025
13 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