Skip to content

Make Client safe for concurrent use - #60

Open
Mzack9999 wants to merge 2 commits into
openrdap:mainfrom
Mzack9999:fix-concurrent-client
Open

Mzack9999 wants to merge 2 commits into
openrdap:mainfrom
Mzack9999:fix-concurrent-client

Conversation

@Mzack9999

@Mzack9999 Mzack9999 commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #31

A shared rdap.Client races on lazy init, the bootstrap Verbose swap and the bootstrap registry map, and can panic under concurrent Do calls. This adds a mutex to Client and bootstrap.Client, and makes Request.URL() copy the server URL instead of modifying it in place. Includes a test that runs concurrent Do calls against a shared client under -race.

Summary by CodeRabbit

  • New Features
    • Bootstrap lookups can now accept a per-call verbose callback, allowing callers to receive lookup details without changing the shared client’s callback setting.
  • Bug Fixes
    • Improved reliability when multiple requests use the same client concurrently, including requests that rely on bootstrap lookup.
    • Fixed URL construction so existing server query parameters and fragments are preserved when building a domain request URL.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

Client operations now lock shared initialization, bootstrap registry, and cache state. Bootstrap lookups accept per-call verbose callbacks without changing the shared callback. Request.URL copies the server URL before building the request URL. New tests cover concurrent requests, bootstrap lookups, and preservation of the server URL query and fragment.

Severity of issue fixed: Medium

Merge Risk: Low · up to abf8f

The change makes the client safe for concurrent use and removes the earlier deadlock and stall risks. Concurrent lookups on a cold or just-refreshed cache may still download the same bootstrap file more than once. That adds some network traffic but does not produce wrong results, so this is acceptable with owner awareness.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed Title fits the main change. PR makes Client safe for concurrent use. Changes add mutexes to Client and bootstrap.Client, add LookupVerbose, fix Request.URL to copy server URL, and add concurrency test…
Linked Issues check Passed [#31] Requirement: make client.Do() safe for concurrent use, or document the limit.

Source read at head (not only summary):
- client.go: Client has mu sync.Mutex. Do() takes mu only while it sets def…
Out of Scope Changes check Passed All production changes serve the concurrency objective of #31:
- client.go: mutex around lazy init and Verbose selection. Verbose swap on shared bootstrap client removed.
- bootstrap/client.go: mutex …
Full details: Title check

Explanation

Title fits the main change. PR makes Client safe for concurrent use. Changes add mutexes to Client and bootstrap.Client, add LookupVerbose, fix Request.URL to copy server URL, and add concurrency tests. Linked issue #31 asks for the same thing. Title is short, specific, and clear.

Full details: Linked Issues check

Explanation

[#31] Requirement: make client.Do() safe for concurrent use, or document the limit.

Source read at head (not only summary):

  • client.go: Client has mu sync.Mutex. Do() takes mu only while it sets defaults for HTTP, Bootstrap, and Verbose. Verbose is no longer swapped on the shared bootstrap client. Do() calls c.Bootstrap.LookupVerbose(question, c.Verbose), which removes the old race on shared Verbose state (the reported client.go:96 write).
  • bootstrap/client.go: Client has mu sync.Mutex guarding init, registries map, Cache state, and reloadFromCache. Downloads run without mu (downloadAndStore), then store under mu. Verbose is never called with mu held. This removes the reported map race in Lookup (bootstrap/client.go:340 path).
  • request.go: URL() copies *r.Server into tempURL and clears RawQuery/Fragment on the copy. Query() returns a new map and does not mutate the shared Server. This removes the shared-pointer write.

Not verified: test files (client_concurrency_test.go, bootstrap/concurrency_test.go) were not read. Test pass and -race results were not run. The PASS rests on source of the three production files. Issue #31 does not require a test, so this does not change the verdict.

Full details: Out of Scope Changes check

Explanation

All production changes serve the concurrency objective of #31:

  • client.go: mutex around lazy init and Verbose selection. Verbose swap on shared bootstrap client removed.
  • bootstrap/client.go: mutex around registry map, cache access, and lazy init. New exported LookupVerbose(question, verbose) is the per-call callback that replaces the shared Verbose swap. It is a direct part of the fix. Lookup now delegates to it with nil. Exported API addition is a necessary consequence of the fix, not unrelated scope.
  • request.go: URL() copy fix. Raw-request branch is unchanged in behavior.

No unrelated feature or behavior change seen. Test files were not read directly, so their content is judged from summary only. Supporting tests for this fix are in scope.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 164aaf14-c1e8-48db-afb4-e7aaf7f55570
Commits

Reviewing files that changed from the base of the PR and between 64c8c7d and b884322.

Files selected for processing: 4

  • bootstrap/client.go
  • client.go
  • client_concurrency_test.go
  • request.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Comment thread bootstrap/client.go Outdated
Comment on lines +334 to +335
c.mu.Lock()
defer c.mu.Unlock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major · Stability & Availability · Heavy lift

Run Verbose callbacks outside the bootstrap mutex.

If b.Verbose calls b.DNS(), b.Lookup() now deadlocks. Lookup holds b.mu when it invokes Verbose. DNS then waits for the same mutex. Before this change, that callback returned normally.

Capture the needed state under the mutex. Release the mutex before invoking caller callbacks.

Comment thread client.go Outdated

// The Verbose swap writes to the shared bootstrap client, so it and
// the lookup run under the lock.
c.mu.Lock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major · Stability & Availability · Heavy lift

Do not hold the client mutex across bootstrap network I/O.

If one bootstrap download stalls, this mutex blocks every later Do, including requests with an explicit server. Those requests wait at Line 112. An expired request context cannot release that wait. The default HTTP client has no timeout, so the wait can outlast the 30-second doQuickRequest timeout indefinitely.

Release state locks before network I/O. Address the bootstrap mutex held across the download too. Make required waiting context-aware.

Comment thread client.go Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d0378976-d005-4b2e-bb68-daf8ef7f0aa7
Commits

Reviewing files that changed from the base of the PR and between b884322 and abf8f98.

Files selected for processing: 4

  • bootstrap/client.go
  • bootstrap/concurrency_test.go
  • client.go
  • client_concurrency_test.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

Comment thread bootstrap/client.go
if c.registries[registry] == nil || forceDownload {
c.Verbose(fmt.Sprintf(" bootstrap: Downloading %s", registry.Filename()))
c.mu.Lock()
needDownload := c.registries[registry] == nil || forceDownload

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Performance & Scalability · Quick win

Recheck expiry before starting a refresh.

Caller A can record Expired, then pause in the verbose callback. Caller B can refresh the registry during that pause. When A resumes, its old forceDownload=true still forces another download despite the fresh registry.

Recheck cache state under mu before the download decision. Keep cache-load failure as a separate refresh reason. This avoids a second network wait after a successful refresh.

Comment thread bootstrap/client.go
if needDownload {
verbose(fmt.Sprintf(" bootstrap: Downloading %s", registry.Filename()))

if err := c.downloadAndStore(question.Context(), registry); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Performance & Scalability · Heavy lift

Share one in-flight refresh per registry.

With an empty cache, all 16 concurrent lookups can pass Line 384 before any download completes. Each then downloads the same registry. Bootstrap traffic grows with request concurrency. This violates the documented once-per-24-hours cache behavior. (raw.githubusercontent.com)

Reserve the registry refresh under mu. Let other callers wait outside mu, with their own contexts. Clear the reservation on success and failure. Add a concurrent cold-cache test that counts bootstrap requests.

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.

client.Do() is not safe for concurrent use

1 participant