Skip to content

Add tests to detect race conditions and fix two potential issues#214

Merged
mwennrich merged 8 commits intomasterfrom
race
Mar 6, 2026
Merged

Add tests to detect race conditions and fix two potential issues#214
mwennrich merged 8 commits intomasterfrom
race

Conversation

@chbmuc
Copy link
Contributor

@chbmuc chbmuc commented Mar 6, 2026

Description

I made Claude write tests to detect race conditions by simulating DNS load scenarios.

It found two potential issues, that are also fixed in this PR:

  • pkg/dns/dnscache.go: Added Lock()/Unlock() around updateDNSServerAddr() write, and RLock()/RUnlock() around dnsServerAddr read in loadDataFromDNSServer()
  • pkg/dns/dns_proxy_handler.go: Added sync.RWMutex to DNSProxyHandler, locked dnsServerAddr writes in UpdateDNSServerAddr() and reads in getDataFromDNS()

I also checked if it would have found #211, by reverting the commit.

chbmuc and others added 5 commits March 4, 2026 11:58
- Replace go.uber.org/mock with github.com/vektra/mockery/v2 for mock generation
- Update Makefile: replace mockgen target with mockery
- Update go:generate directive in pkg/nftables/firewall.go
- Regenerate mocks using mockery (now uses github.com/stretchr/testify/mock)
- Update test imports and mock usage from gomock API to testify/mock API

Mock API changes:
- Mock class: MockFQDNCache -> FQDNCache
- Constructor: NewMockFQDNCache(ctrl) -> NewFQDNCache(t)
- Matchers: gomock.Any() -> mock.Anything
- Expectations: .EXPECT().Method() -> .On("Method")
The test was using mock.Anything for both GetSetsForFQDN calls, causing
both expectations to match every call and return the same values. Now
matches on the actual FQDNSelector values (MatchName and MatchPattern)
to properly test the different return values for IPv4 and IPv6 sets.
- pkg/dns/dnscache.go: Added Lock()/Unlock() around
updateDNSServerAddr() write, and RLock()/RUnlock() around dnsServerAddr
read in
   loadDataFromDNSServer()
  - pkg/dns/dns_proxy_handler.go: Added sync.RWMutex to DNSProxyHandler,
locked dnsServerAddr writes in UpdateDNSServerAddr() and
  reads in getDataFromDNS()
Add concurrent access tests that detect data races using go test -race.
Tests cover concurrent updateIPEntry with getSetsForRendering,
getSetNameForRegex, getSetNameForFQDN, writeStateToConfigmap, and
updateDNSServerAddr, plus a multiple-readers test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chbmuc chbmuc requested a review from a team as a code owner March 6, 2026 06:23
@metal-robot metal-robot bot added this to Development Mar 6, 2026
@chbmuc chbmuc requested review from mwennrich and removed request for a team March 6, 2026 06:33
@mwennrich
Copy link
Contributor

The lock for dnsServerAddr is technically correct, but the dnsServerAddr gets literally never updated. (It's either the hard-coded default for regular clusters, or the partition-service proxy address for network-isolated clusters, which will never change).
Doesn't hurt to have it though, so we don't get this reported again by the next AI :-)

@majst01
Copy link
Contributor

majst01 commented Mar 6, 2026

Please wait til #213 is merged

@mwennrich
Copy link
Contributor

I ran the test code against the buggy 2.4.0 version, and it would have successfully crashed (and therefore detected) the race condition 👍

@mwennrich
Copy link
Contributor

mwennrich commented Mar 6, 2026

The loops in dnscache_test.go like this:
for i := 0; i < n; i++
should all be more modern like this:
for i := range n

go fix pkg/dns/dnscache_test.go pkg/dns/dnscache.go should fix these all

@majst01
Copy link
Contributor

majst01 commented Mar 6, 2026

Please wait til #213 is merged

Merged now

Copy link
Contributor

@majst01 majst01 left a comment

Choose a reason for hiding this comment

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

TODO: run in Test

@mwennrich mwennrich merged commit 50cca88 into master Mar 6, 2026
2 checks passed
@mwennrich mwennrich deleted the race branch March 6, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants