fix: make Server.isRegistered atomic - #2532
Merged
henderkes merged 3 commits intoJul 20, 2026
Merged
Conversation
The flag is written by Init()/Shutdown() while in-flight ServeHTTP() calls read it concurrently, e.g. during a Caddy admin reload under traffic. The race detector flags the plain bool; use atomic.Bool. Same pattern as the pre-existing global isRunning flag, but Server instances are the surface library users now interact with.
Contributor
|
both should be changed when they're possibly accessed concurrently, if only to keep the test suite green |
Init() now uses CompareAndSwap, which also closes the window where two concurrent Init() calls could both pass the running check.
Contributor
Author
|
Done! |
registerServers() replaces fallbackServer.logger on every Init() while in-flight requests going through the package-level ServeHTTP() read it when building the request context. Store it in an atomic.Pointer and publish it before flipping isRegistered. Regular servers set their logger once in NewServer() and never mutate it afterwards; the globalLogger variable itself has the same pre-existing pattern as isRunning had and is left out of scope here.
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.
Follow-up to #2499, targeting
refactor/phpserver.Server.isRegisteredis written byInit()/Shutdown()while in-flightServeHTTP()calls read it concurrently, e.g. during an admin API reload under traffic; the race detector flags the plain bool. This makes it anatomic.Bool.The pre-existing global
isRunningflag has the same pattern, butServeris the surface library users now interact with, so it seemed worth fixing here;isRunningcan be handled separately if wanted.