Fix: make Engine.Routes() concurrency-safe (#4457) #4459
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fix: make Engine.Routes() concurrent-safe (#4457)
Description
This PR addresses a data race in
Engine.Routes()that occurs when routes are read concurrently with route registration.Problem
Engine.Routes()while registering new routes could cause simultaneous reads and writes to the internaltreesstructure.go test -race.Solution
-Introduced a sync.RWMutex in Engine to protect access to the route trees.
-Reads (Routes()) acquire a read lock, while route registrations acquire a write lock.
-Ensures thread-safe access to route trees without blocking unrelated reads.
-Added a unit test (TestRoutesConcurrent) to reproduce the race condition and verify the fix.
Testing
-Run go test ./... -race locally; the previous race warnings no longer appear.
-The test confirms that concurrent reads and writes to routes are handled safely.
References
-Fixes issue: #4457