Repository navigation
Conversation
The loader writes Config.Targets under configLock while the leader's dispatch loop ranges over the same map with only dispatchLock held. With a few thousand targets and the loader adding targets during startup, the leader crashes with 'fatal error: concurrent map iteration and map write' and leadership moves to another instance, which hits the same race. Copy the targets under configLock.RLock and iterate the copy.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This branch has not been deployed
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
In clustered mode, the leader can crash with:
dispatchTargetsOnceranges overa.Config.Targetswhile holding onlydispatchLock.The loader writes the same map under
configLock(startLoader,a.Config.Targets[add.Name] = add).Since the loader releases
configLockbefore it dispatches (v0.49.1), it writes to the map more often while the dispatch loop runs.We hit it in production on v0.49.1 with a 6-instance cluster and about 6000 targets from the HTTP loader: the leader crashed during the initial load after a rollout, leadership moved to another instance, which crashed the same way. It settled once the loader stopped adding targets.
Fix
Copy the targets under
configLock.RLock()and iterate over the copy. The lock is held only for the copy, not acrossdispatchTarget, so the loader and the API handlers are never blocked by a dispatch pass.Test
TestDispatchTargetsOnceSafeAgainstConcurrentLoaderWritesrunsdispatchTargetsOncewhile another goroutine adds targets underconfigLock.go test -racereportsDATA RACEand the run dies withfatal error: concurrent map iteration and map write.go test -race ./pkg/app/passes.