Skip to content

fix: make Server.isRegistered atomic - #2532

Merged
henderkes merged 3 commits into
php:refactor/phpserverfrom
nicolas-grekas:server-registered-atomic
Jul 20, 2026
Merged

fix: make Server.isRegistered atomic#2532
henderkes merged 3 commits into
php:refactor/phpserverfrom
nicolas-grekas:server-registered-atomic

Conversation

@nicolas-grekas

Copy link
Copy Markdown
Contributor

Follow-up to #2499, targeting refactor/phpserver.

Server.isRegistered is written by Init()/Shutdown() while in-flight ServeHTTP() calls read it concurrently, e.g. during an admin API reload under traffic; the race detector flags the plain bool. This makes it an atomic.Bool.

The pre-existing global isRunning flag has the same pattern, but Server is the surface library users now interact with, so it seemed worth fixing here; isRunning can be handled separately if wanted.

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.
@henderkes

Copy link
Copy Markdown
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.
@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

Done!

Comment thread server.go Outdated
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.
@henderkes
henderkes merged commit 83cd944 into php:refactor/phpserver Jul 20, 2026
@nicolas-grekas
nicolas-grekas deleted the server-registered-atomic branch July 20, 2026 08:58
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.

3 participants