Fix unsafe client selection in ClientPool - #265
Merged
pk910 merged 2 commits intoJul 31, 2026
Conversation
GetClient's round-robin branch read the shared cursor and indexed the current candidate slice with it before checking that the cursor was still in range for that slice. Because the candidate set can be smaller on a later call than it was when the cursor last advanced (a different client group, a type exclusion, a client toggling enabled), this could index past the end of the slice and panic. The cursor is now clamped before it is read, and the duplicate round-robin logic that lived under both the explicit case and the default fallback is merged into one path. Also fixed the client health check writing goodClients without holding selectionMutex, while GetClient only reads it under that lock. A write with no lock at all gives the reader no real protection, so the two could race on the slice header and hand back a stale length paired with a reallocated backing array.
pk910
approved these changes
Jul 31, 2026
pk910
enabled auto-merge
July 31, 2026 00:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
GetClient's round-robin branch read the shared cursor and indexed the current candidate slice with it before checking that the cursor was still in range for that slice. Because the candidate set can be smaller on a later call than it was when the cursor last advanced (a different client group, a type exclusion, a client toggling enabled), this could index past the end of the slice and panic. The cursor is now clamped before it is read, and the duplicate round-robin logic that lived under both the explicit case and the default fallback is merged into one path.
Also fixes the client health check writing goodClients without holding selectionMutex, while GetClient only reads it under that lock. A write with no lock at all gives the reader no real protection, so the two could race on the slice header and hand back a stale length paired with a reallocated backing array.
Adds tests covering round-robin selection across a smaller group, a shrinking pool, the byIndex control path, and a concurrent write/read check for the lock fix.