Skip to content

fix: fix two thread-safety issues#53

Merged
lzap merged 5 commits intoosbuild:mainfrom
lzap:race1
Apr 30, 2026
Merged

fix: fix two thread-safety issues#53
lzap merged 5 commits intoosbuild:mainfrom
lzap:race1

Conversation

@lzap
Copy link
Copy Markdown
Collaborator

@lzap lzap commented Apr 30, 2026

I am working on running tests in parallel in CRC and race detector revealed that the random generator used for trace IDs is not thread-safe. This fixes it, Claude also found one more issue. The first commit refactors tests a bit this was needed because previously it relied on fixed random generator with seed value of 0 now that is not possible.

Commit 1: Fix randString race condition in strc package (355cd27)

  • Root cause: The global rand.Source in pkg/strc/context.go:167 was not thread-safe
  • Solution: Replaced math/rand with math/rand/v2, which provides a thread-safe global generator
  • Why this approach: Cleaner and more efficient than using a mutex - no global mutable state and no lock contention
  • Test coverage: Added TestConcurrentIDGeneration that spawns 100 goroutines each generating 100 IDs to verify thread safety
  • Test updates: Updated tests to verify format/length instead of specific random values (since math/rand/v2's global generator cannot be seeded)

Commit 2: Fix echo proxy race condition (759db83)

  • Root cause: The global proxy variable in pkg/echo/proxy.go:19 could have races if SetDefault() and Default() were called concurrently
  • Solution: Replaced plain pointer with sync/atomic.Pointer[Proxy]
  • Consistency: This matches the pattern already used in pkg/logrus/proxy.go and pkg/strc/trace.go

Complete codebase review findings:


On top of that I noticed this has quite old Go version so upgraded Go version, ran gobump tool to update deps and also dropped the warning in prepare-source.sh I think that turned not to be very useful I will be slowly removing this from all the projects. I introduced it... :-)

lzap and others added 2 commits April 30, 2026 14:52
The global rand.Source in pkg/strc/context.go was not thread-safe,
causing potential race conditions when NewTraceID() or NewSpanID()
were called concurrently from multiple goroutines.

This commit:
- Replaces math/rand with math/rand/v2 which has a thread-safe global generator
- Removes the global rand.Source and mutex approach
- Updates tests to verify format/length instead of specific random values
  (since math/rand/v2 global generator cannot be seeded for deterministic tests)
- Adds TestConcurrentIDGeneration to verify thread safety

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
The global proxy variable in pkg/echo/proxy.go could have race
conditions if SetDefault() and Default() were called concurrently.

This commit replaces the plain pointer with sync/atomic.Pointer
to ensure thread-safe access, consistent with the approach used
in pkg/logrus/proxy.go and pkg/strc/trace.go.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@lzap lzap requested a review from a team as a code owner April 30, 2026 12:54
@lzap lzap requested review from croissanne and schuellerf April 30, 2026 12:54
@lzap lzap merged commit 72f2c52 into osbuild:main Apr 30, 2026
2 checks passed
@lzap lzap deleted the race1 branch April 30, 2026 13:15
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