Skip to content

core: synchronize Stop with concurrent config reloads - #8038

Merged
steadytao merged 1 commit into
caddyserver:masterfrom
H-XX-D:fix-stop-context-race
Sep 19, 2026
Merged

steadytao merged 1 commit into
caddyserver:masterfrom
H-XX-D:fix-stop-context-race

Conversation

@H-XX-D

@H-XX-D H-XX-D commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Synchronizes Stop() with concurrent configuration reloads and changes to prevent orphaning active server contexts.

Problem

In caddy.go, Stop() previously read currentCtx under currentCtxMu.RLock() prior to acquiring rawCfgMu.Lock():

func Stop() error {
	currentCtxMu.RLock()
	ctx := currentCtx
	currentCtxMu.RUnlock()

	rawCfgMu.Lock()
	unsyncedStop(ctx)

	currentCtxMu.Lock()
	currentCtx = Context{}
	currentCtxMu.Unlock()

If a concurrent reload (such as caddy.Load() or an admin API config change) executes between releasing currentCtxMu and acquiring rawCfgMu:

  1. Stop() snapshots the old context.
  2. The reload acquires rawCfgMu.Lock(), transitions currentCtx to the new configuration, and stops the old context.
  3. Stop() then acquires rawCfgMu.Lock(), calls unsyncedStop(ctx) on the already-stopped old context, and proceeds to wipe currentCtx = Context{}.
  4. The newly loaded server instances remain running in the background, but the reference in currentCtx is wiped, orphaning them without a mechanism to shut them down.

Solution

Acquire rawCfgMu.Lock() at the start of Stop(), and snapshot and reset currentCtx = Context{} atomically under currentCtxMu.Lock(). Because all reloads and config mutations serialize on rawCfgMu, this guarantees that Stop() operates on the true active context and cannot interleave destructively with reloads.

Verification

  • Ran go test -v . (all tests passed).
  • Built full repository tree via go build ./....

Assistance Disclosure

I authored and tested this fix, using AI assistance for concurrency analysis and verification.

Signed-off-by: hendrixx-cnc <tjhendrx@icloud.com>
@CLAassistant

CLAassistant commented Sep 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@steadytao
steadytao enabled auto-merge (squash) September 19, 2026 15:56
@steadytao
steadytao merged commit 128b9e7 into caddyserver:master Sep 19, 2026
25 checks passed
@H-XX-D

H-XX-D commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review and merge @steadytao
Appreciate the time and effort you've invested in this project, glad I could help.

@steadytao

Copy link
Copy Markdown
Member

No worries. Thank you for the PR.

SillyZir added a commit to SillyZir/caddy that referenced this pull request Oct 8, 2026
caddyserver#8038 moved the clearing of currentCtx ahead of unsyncedStop, so during
shutdown Cleanup no longer finds a serving TLS app: it takes its teardown
path and stops the published cache itself. The two tests that prove a cache
is still being maintained do so by stopping it, and had therefore already
consumed it, so shutdown stopped it a second time and panicked on the
closed channel.

Detach a consumed cache from both the app that owns it and the
package-level publication before stopping it, so neither teardown path has
anything of its own left to stop. That also makes the per-test consumed
bookkeeping redundant, since shutdown now zeroes the published cache on its
own.

Only the tests change. Both negative controls were re-run and still fail as
they should: reintroducing publish-and-stop inside Provision fails the
liveness assertion, and dropping Cleanup's ownCache != publishedCache guard
makes shutdown double-stop the published cache.
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