fix(healthcheck): guard periodic checks against divide-by-zero when config is cleared#3400
Open
mvanhorn wants to merge 1 commit into
Open
fix(healthcheck): guard periodic checks against divide-by-zero when config is cleared#3400mvanhorn wants to merge 1 commit into
mvanhorn wants to merge 1 commit into
Conversation
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.
Description
The healthcheck goroutine can panic with
panic: runtime error: integer divide by zero(stack atinternal/healthcheck/checker.goinfullPeriodicCheck).fullPeriodicCheckindexed the shared config slice directly withc.tlsDialAddrs[try%len(c.tlsDialAddrs)].Checker.Stop()clears the config by settingc.tlsDialAddrs = nil(andicmpTargetIPs/smallCheckType) — but did so without holdingc.configMutex. AfullPeriodicCheckstill in flight during or just afterStop()then evaluatestry % len(c.tlsDialAddrs)withlen == 0and panics. It is also a data race:fullPeriodicCheckreadc.tlsDialAddrswithout the mutex whileStop/SetConfigwrote it.smallPeriodicCheckalready copiesc.icmpTargetIPsunder the mutex, but itsicmpTargetIPs[try%len(icmpTargetIPs)]had the same latent divide-by-zero when that copy is empty (StopnilsicmpTargetIPstoo).This change:
Stop()now takesc.configMutexwhile clearing the config, synchronizing the clear with the readers.fullPeriodicCheck()copiesc.tlsDialAddrsinto a local slice underc.configMutex(mirroringsmallPeriodicCheck) and returns early when the copy is empty — there is nothing to dial — instead of dividing by zero. The check closure indexes the local copy.smallPeriodicCheck()returns early when the (already-copied) ICMP target slice is empty and the check type is not DNS, so the copied-slice modulo cannot divide by zero either. The DNS-fallback path is unchanged.Behavior is identical for a normally-running checker with non-empty config; the guards only affect the teardown / empty-config window.
Verified with
go test ./internal/healthcheck/...,go test -race ./internal/healthcheck/...,go build ./..., andgofmt -l(clean). A regression test exercises the periodic checks with empty target slices and asserts they no longer panic.Issue
Closes #3398
Assertions