fix(healthcheck): guard target status fields against data race#412
Open
lafoush wants to merge 1 commit into
Open
fix(healthcheck): guard target status fields against data race#412lafoush wants to merge 1 commit into
lafoush wants to merge 1 commit into
Conversation
monitorTarget/performHealthCheck mutate a Target's Status, LastError, LastCheck, CheckCount and consecutive counters from the target's monitor goroutine, while getAllTargetsUnsafe copied the whole struct (targetCopy := *target) for status reporting. The map lock protects the map, not the target contents, so these overlap without synchronization. Confirmed by the race detector on a concurrent monitor + GetTargets workload. Add a per-Target mutex guarding the mutable status fields and replace the whole-struct copy with a snapshot() taken under that lock (copying only the reported fields; runtime-only fields timer/ctx/cancel/client are omitted). The lock is not held across the health-check network request, so snapshots are never blocked for the check timeout. DisableTarget's Status write is guarded too. Adds healthcheck_test.go with a regression test that fails under -race without the fix.
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
monitorTarget/performHealthCheckmutate aTarget'sStatus,LastError,LastCheck,CheckCount, and consecutive-success/failure counters from the target's monitor goroutine. MeanwhilegetAllTargetsUnsafe(called byGetTargets, used for status reporting to the server) copied the whole struct withtargetCopy := *target.The monitor mutex protects the
targetsmap, not the contents of eachTarget, so these accesses overlap without synchronization. The race detector flags it on any concurrent monitor +GetTargetsworkload — e.g. the reconnect path, where the status reporter snapshots targets while their monitor goroutines are updating status.Fix
Targetsync.Mutexguarding the mutable status fields.snapshot()taken under that lock, copying only the reported fields (Config,Status,LastCheck,LastError,CheckCount). Runtime-only fields (timer/ctx/cancel/client) are intentionally omitted — callers ofGetTargetsonly read the reported fields, and this also avoids copying async.Mutex.DisableTarget'sStatuswrite is guarded too.Test
Adds
healthcheck_test.gowith a regression test that exercises concurrent monitoring + snapshotting. It fails under-racewithout the fix and passes with it:go build,go vet ./healthcheck/, andgofmtare clean.No functional/behavioral change to health checking — this is purely a concurrency-safety fix.