Skip to content

fix(acme): make the custom DNS server rotation counter atomic (#9273) - #9274

Merged
cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
shashankvarma499:fix-acme-http-dns-rotation-race
Sep 7, 2026
Merged

cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
shashankvarma499:fix-acme-http-dns-rotation-race

Conversation

@shashankvarma499

@shashankvarma499 shashankvarma499 commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

What

go test -race ./pkg/issuer/acme/http/ fails on TestReachabilityCustomDnsServers: the DNS-server rotation counter in testReachability is a plain int captured by the Resolver.Dial closure, and Go's resolver calls Dial from concurrent goroutines (parallel A/AAAA lookups), so the read at counter%len(dnsServers) races with the counter++ write.

Fix

The counter is now an atomic.Uint32 and the round-robin index is derived with counter.Add(1)-1, preserving the rotation order (first dial uses dnsServers[0], second uses dnsServers[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

  • With the fix: go test -race ./pkg/issuer/acme/http/ -count=1 passes.
  • Without the fix (plain int restored): TestReachabilityCustomDnsServers fails under -race in under a second with the same data race.
  • gofmt and go vet clean.

Part of #9266 (pkg/issuer/acme/http is the 1-race entry in its table).

Fixes #9273

Fixed a data race in the ACME HTTP-01 self-check that could occur when custom DNS servers were configured.

@cert-manager-prow cert-manager-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. area/acme Indicates a PR directly modifies the ACME Issuer code area/acme/http01 Indicates a PR modifies ACME HTTP01 provider code needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 4, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

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

Copy link
Copy Markdown
Contributor Author

/kind bug

@cert-manager-prow cert-manager-prow Bot added kind/bug Categorizes issue or PR as related to a bug. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Sep 4, 2026
@wallrj
wallrj requested a balanced review from Copilot September 4, 2026 15:49

Copilot AI 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.

🟢 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.Int32 for 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 wallrj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]

Comment thread pkg/issuer/acme/http/http.go Outdated
Comment on lines +276 to +279
// 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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

Suggested replacement for the whole comment block:

Suggested change
// 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that is a much better description. Applied your suggested comment verbatim.

Comment thread pkg/issuer/acme/http/http.go Outdated
counter := 0
// The counter must be atomic: the resolver calls Dial from concurrent
// goroutines (parallel A/AAAA lookups).
var counter atomic.Int32

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Suggested change
var counter atomic.Int32
var counter atomic.Uint32

with dnsServers[int(counter.Add(1)-1)%len(dnsServers)] unchanged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@wallrj

wallrj commented Sep 5, 2026

Copy link
Copy Markdown
Member

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.

@shashankvarma499
shashankvarma499 force-pushed the fix-acme-http-dns-rotation-race branch from 74e6731 to ec557ca Compare September 6, 2026 00:36
@cert-manager-prow cert-manager-prow Bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 6, 2026
@shashankvarma499

Copy link
Copy Markdown
Contributor Author

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]>
@shashankvarma499
shashankvarma499 force-pushed the fix-acme-http-dns-rotation-race branch from ec557ca to 0f58fa7 Compare September 7, 2026 05:31
@cert-manager-prow cert-manager-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 7, 2026
@shashankvarma499

Copy link
Copy Markdown
Contributor Author

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 wallrj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]

@cert-manager-prow cert-manager-prow Bot added tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. ok-to-test labels Sep 7, 2026
@cert-manager-prow cert-manager-prow Bot added lgtm Indicates that a PR is ready to be merged. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 7, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 7, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit 79dd67c into cert-manager:master Sep 7, 2026
6 checks passed
@wallrj

wallrj commented Sep 8, 2026

Copy link
Copy Markdown
Member

/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 release-1.21, and the pkg/issuer/acme/http tests pass there with -race.

[Claude Fable 5.1]

@cert-manager-bot

Copy link
Copy Markdown
Contributor

@wallrj: new pull request created: #9313

Details

In response to this:

/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 release-1.21, and the pkg/issuer/acme/http tests pass there with -race.

[Claude Fable 5.1]

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.

@wallrj

wallrj commented Sep 11, 2026

Copy link
Copy Markdown
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. area/acme/http01 Indicates a PR modifies ACME HTTP01 provider code area/acme Indicates a PR directly modifies the ACME Issuer code dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. ok-to-test release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkg/issuer/acme/http: testReachability DNS-server rotation races under -race

4 participants