Skip to content

🐛 robot: lock the server cache against concurrent use - #91

Merged
batistein merged 1 commit into
batistein/restrict-node-port-sourcefrom
batistein/robot-cache-race
Nov 6, 2026
Merged

batistein merged 1 commit into
batistein/restrict-node-port-sourcefrom
batistein/robot-cache-race

Conversation

@batistein

Copy link
Copy Markdown

Problem

The cached Robot client is shared by the service, node and route controllers. They call it from several goroutines, but ServerGetList, ServerGet and ServerGetListForceRefresh write 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 hit fatal error: concurrent map writes at internal/robot/client/cache/client.go:142.

There was a second bug. SetCredentials set forcedRefreshServerNames to nil, so the next forced refresh for a missing server name panicked with assignment to entry in nil map.

Change

  • A sync.Mutex guards all cache state. Each public method takes it, and the shared syncIfNeeded runs 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.
  • The sync builds a new list and map and swaps them in. Slices and pointers returned earlier are never modified.
  • SetCredentials resets the forced-refresh map to an empty map instead of nil.
  • mocks.RobotClient.SetCredentials now passes its arguments and returns args.Error(0). Before, it returned args.Error(3).

Tests

  • TestConcurrentAccess calls all four methods from 80 goroutines with an always-expired cache. On the old code it fails under -race with 12 data races. With this change it passes (-race -count=3).
  • TestForcedRefreshAfterSetCredentials panics 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.

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).
@batistein
batistein added this pull request to stack #92 November 5, 2026 19:02
@batistein batistein changed the title Lock the Robot server cache against concurrent use 🐛 robot: lock the server cache against concurrent use Nov 5, 2026
@batistein
batistein merged commit 744ec64 into main Nov 6, 2026
3 checks passed
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.

1 participant