Repository navigation
🐛 robot: lock the server cache against concurrent use - #91
Merged
batistein merged 1 commit intoNov 6, 2026
Merged
Conversation
The service, node and route controllers call the cached Robot client from several goroutines. ServerGetList, ServerGet and ServerGetListForceRefresh wrote the cache map and list without a lock, which crashed the CCM with "fatal error: concurrent map writes" when several Services reconciled at once after a leader change. A mutex now guards all cache state, and the Robot API call happens under it, so concurrent callers share one refresh instead of each sending a request. The sync builds a new list and map and swaps them in, so slices and pointers handed out earlier stay valid. SetCredentials set the forced-refresh map to nil, so the next forced refresh panicked with "assignment to entry in nil map". It now resets the map instead. The RobotClient mock ignored SetCredentials arguments and returned args.Error(3); it now passes the arguments and returns args.Error(0).
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.
Problem
The cached Robot client is shared by the service, node and route controllers. They call it from several goroutines, but
ServerGetList,ServerGetandServerGetListForceRefreshwrite the cache map, list and timestamp without a lock. During the 1-36 → 1-37 upgrade test (cluster-stacks#1965) this crashed the CCM: on its first lease acquire, 5 Service workers reconciled at once and the process hitfatal error: concurrent map writesatinternal/robot/client/cache/client.go:142.There was a second bug.
SetCredentialssetforcedRefreshServerNamesto nil, so the next forced refresh for a missing server name panicked withassignment to entry in nil map.Change
sync.Mutexguards all cache state. Each public method takes it, and the sharedsyncIfNeededruns under it. The Robot API call also runs under the lock, so concurrent callers share one refresh instead of each hitting the Robot rate limit.SetCredentialsresets the forced-refresh map to an empty map instead of nil.mocks.RobotClient.SetCredentialsnow passes its arguments and returnsargs.Error(0). Before, it returnedargs.Error(3).Tests
TestConcurrentAccesscalls all four methods from 80 goroutines with an always-expired cache. On the old code it fails under-racewith 12 data races. With this change it passes (-race -count=3).TestForcedRefreshAfterSetCredentialspanics on the old code (nil map) and passes now. It also checks that a repeated forced refresh within the window uses the cache.go test ./...passes and golangci-lint reports 0 issues.Stacked on #90, so both changes can ship in one test image.