Repository navigation
fix(acme): make the custom DNS server rotation counter atomic (#9273) - #9274
Conversation
|
Hi @shashankvarma499. Thanks for your PR. I'm waiting for a cert-manager member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
/kind bug |
There was a problem hiding this comment.
🟢 Approval recommended
The focused atomic-counter fix resolves the race with no unresolved issues.
Pull request overview
Fixes a data race in ACME HTTP-01 checks using custom DNS servers.
Changes:
- Uses
atomic.Int32for DNS-server rotation. - Preserves zero-based round-robin selection.
File summaries
| File | Description |
|---|---|
pkg/issuer/acme/http/http.go |
Makes concurrent DNS-server rotation race-safe. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
wallrj
left a comment
There was a problem hiding this comment.
Thank you for the fix. The change is correct. I confirmed that go test -race ./pkg/issuer/acme/http/ fails on master and passes at 74e6731. Two things before I approve.
Please add a test that uses two DNS servers. The only custom-nameserver test passes one server, so every index is 0 and the rotation is never exercised. CI does not run this package with -race either, so a later edit that reverts the atomic or breaks the index arithmetic would pass make test. The existing dns.Server and ServeMux scaffolding in http_test.go makes this cheap: start a second server on another port and assert that both servers receive a query. The A and AAAA lookups always produce at least two Dial calls, so both servers will be hit. Please do not assert the order, because it depends on goroutine scheduling.
Please rewrite the comment. See the inline comment. The existing sentence gives the wrong reason for the counter, and the PR adds a second sentence rather than correcting it.
Two optional suggestions follow inline. Neither blocks the merge.
[Claude Fable 5.1]
| // we need to increment a counter to iterate through the dns servers as the dialer will not | ||
| // return an error if the dns server is not responding. | ||
| counter := 0 | ||
| // The counter must be atomic: the resolver calls Dial from concurrent | ||
| // goroutines (parallel A/AAAA lookups). |
There was a problem hiding this comment.
The first sentence is wrong and the second does not replace it. The Go resolver does surface timeouts from Dial and retries on its own. The counter exists because this Dial ignores the address argument the resolver passes, so without our own rotation every resolver retry would hit the same server.
Evidence:
- The resolver passes the server address to
Dialand expects it to be used: https://github.com/golang/go/blob/go1.26.0/src/net/lookup.go#L695-L696 - A and AAAA lookups run in parallel goroutines, which is why the counter must be atomic: https://github.com/golang/go/blob/go1.26.0/src/net/dnsclient_unix.go#L664-L669
Suggested replacement for the whole comment block:
| // we need to increment a counter to iterate through the dns servers as the dialer will not | |
| // return an error if the dns server is not responding. | |
| counter := 0 | |
| // The counter must be atomic: the resolver calls Dial from concurrent | |
| // goroutines (parallel A/AAAA lookups). | |
| // This Dial ignores the resolv.conf address that the resolver passes | |
| // in, so we rotate through dnsServers ourselves. Without this, every | |
| // resolver retry would hit the same server. The counter is atomic | |
| // because the resolver dials from parallel A and AAAA goroutines. |
There was a problem hiding this comment.
Thanks, that is a much better description. Applied your suggested comment verbatim.
| counter := 0 | ||
| // The counter must be atomic: the resolver calls Dial from concurrent | ||
| // goroutines (parallel A/AAAA lookups). | ||
| var counter atomic.Int32 |
There was a problem hiding this comment.
Optional. atomic.Uint32 avoids a negative index if the counter ever wraps. It is the same size, and it matches the counter the standard library uses for its own server rotation: https://github.com/golang/go/blob/go1.26.0/src/net/dnsconfig.go#L52-L57
| var counter atomic.Int32 | |
| var counter atomic.Uint32 |
with dnsServers[int(counter.Add(1)-1)%len(dnsServers)] unchanged.
There was a problem hiding this comment.
Done, switched to atomic.Uint32. Agreed on the wrap behavior, and it matches the counter the standard library uses for its own server rotation.
| @@ -274,7 +275,9 @@ func testReachability(ctx context.Context, url *url.URL, key string, dnsServers | |||
| transport.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, err error) { | |||
There was a problem hiding this comment.
Optional. The outer closure exists only to build a fresh counter, Dialer and Resolver on every connection. You could move those three into the if block and assign transport.DialContext = dialer.DialContext directly. net.Dialer.DialContext already has the signature http.Transport.DialContext expects. That removes the closure, the unused named results and the shadowed ctx and network.
I built and tested that form against master and the existing tests pass. The only behavioural change is that the rotation continues across redirect hops within one testReachability call instead of restarting at dnsServers[0] on each hop. The transport and client are still created per call, so there is no shared state between requests.
Happy for this to go in a follow-up if you would rather keep this PR to the race fix.
There was a problem hiding this comment.
Agreed, that form is cleaner. I kept this PR limited to the race fix plus the comment correction, so I left the closure restructure for a follow up. Also thanks for filing #9280; the rewritten comment no longer implies the nameserver list fails over.
|
I filed #9280 for a pre-existing problem this PR does not need to fix: the nameserver list does not fail over when one server stops responding. The rewritten comment I suggested above avoids restating the old fallback promise. |
74e6731 to
ec557ca
Compare
|
All three points addressed: rewrote the comment block, switched the counter to atomic.Uint32, and kept the closure restructure for a follow up. Verification: go test -race ./pkg/issuer/acme/http/ -count=1 passes, go build and go vet clean. |
…anager#9273) net.Resolver calls Dial from concurrent goroutines (parallel A/AAAA lookups), and the plain int counter captured by the custom DNS server Dial closure raced between the counter%len read and the counter++ write, failing TestReachabilityCustomDnsServers under -race. Use an atomic.Uint32 and derive the round-robin index with counter.Add(1)-1, preserving the rotation order. Fixes cert-manager#9273 Signed-off-by: Shashank Varma <[email protected]>
ec557ca to
0f58fa7
Compare
|
Thanks for the nudge, the two-server test was the missing piece. TestReachabilityCustomDnsServers now starts a second UDP server on 127.0.0.1:15354, and a "two custom dns servers" case asserts that both servers receive at least one query, with no assumption about which server answers first. The comment rewrite and atomic.Uint32 from your earlier review are in place too. Verification: go test -race ./pkg/issuer/acme/http/ -count=1 passes, go build and go vet clean. |
wallrj
left a comment
There was a problem hiding this comment.
Thank you. The two-server test, the comment rewrite and the switch to atomic.Uint32 are all in. I ran go test -race ./pkg/issuer/acme/http/ on 0f58fa7 and it passes.
/approve
/lgtm
/ok-to-test
/label tide/merge-method-squash
[Claude Fable 5.1]
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wallrj The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/cherry-pick release-1.21 Backporting for v1.21.2 (tracker: #9237). This fixes a data race in the ACME HTTP-01 self-check when custom DNS servers are configured, which is production code on v1.21.x. The change is two files and cherry-picks cleanly onto [Claude Fable 5.1] |
|
@wallrj: new pull request created: #9313 DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
@shashankvarma499 This change has been included in v1.21.2, which is now published. If you are able to install the release and verify that the change works as expected in your environment, that would be much appreciated. Thank you for the contribution. |
What
go test -race ./pkg/issuer/acme/http/fails on TestReachabilityCustomDnsServers: the DNS-server rotation counter intestReachabilityis a plain int captured by theResolver.Dialclosure, and Go's resolver callsDialfrom concurrent goroutines (parallel A/AAAA lookups), so the read atcounter%len(dnsServers)races with thecounter++write.Fix
The counter is now an
atomic.Uint32and the round-robin index is derived withcounter.Add(1)-1, preserving the rotation order (first dial usesdnsServers[0], second usesdnsServers[1], and so on). The counter is scoped to a single DialContext call, so no wider state change is needed. The comment above the counter now states why it exists: the Dial ignores the resolv.conf address the resolver passes in, so the rotation is done here, and the counter must be atomic because the resolver dials from parallel A and AAAA goroutines.Test
TestReachabilityCustomDnsServers now starts a second UDP server on 127.0.0.1:15354 and a "two custom dns servers" case asserts that both servers receive at least one query (no assumption about which server answers first, since the A/AAAA dial order depends on goroutine scheduling).
Verification
go test -race ./pkg/issuer/acme/http/ -count=1passes.-racein under a second with the same data race.gofmtandgo vetclean.Part of #9266 (pkg/issuer/acme/http is the 1-race entry in its table).
Fixes #9273