Skip to content

httpcaddyfile: give each adaptation its own directive order - #7995

Merged
francislavoie merged 1 commit into
caddyserver:masterfrom
faiyazrahmann:fix/directive-order-race
Sep 6, 2026
Merged

francislavoie merged 1 commit into
caddyserver:masterfrom
faiyazrahmann:fix/directive-order-race

Conversation

@faiyazrahmann

Copy link
Copy Markdown
Contributor

Fixes #7994.

parseOptOrder was modifying the global directiveOrder on every adapt. Since it could share the same underlying slice as defaultDirectiveOrder, concurrent adapts could permanently remove entries from the default order. After that, any adapt would fail until restart, even ones not using order.

What changed

  • directiveOrder is now only changed by RegisterDirectiveOrder during init.
  • parseOptOrder works with a local copy and returns the order instead of changing a global.
  • sortRoutes and buildSubroute now receive the order directly.
  • Setup clones the options map and drops order from tis untouched and reusing the same map is safe.
  • RegisterDirectiveOrder now inserts into a cloned slice.

No mutexes or exported API changes.

Tests

Added tests for sequential/concurrent adapts, reused options maps, recovery after a bad order, the original race reproducer, and plugin-registered directive order. All of them fail on master.

Disclosure

I used LLM to generate the tests.

Copilot AI lite review requested due to automatic review settings September 6, 2026 04:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The concurrency fix is well-scoped (per-adaptation state instead of global mutation) and is backed by targeted regression tests; only a minor test maintainability nit was found.

Pull request overview

Fixes a concurrency bug in the HTTP Caddyfile adapter where the order global option could mutate shared process-wide directive ordering, causing subsequent adaptations to fail until restart. The change scopes directive ordering to each adaptation while keeping plugin-registered directive order global and init-only.

Changes:

  • Make order handling per-adaptation: parseOptOrder now works on a local copy and returns the updated order instead of mutating a global slice.
  • Thread directive order explicitly through buildSubroute/sortRoutes, and add directiveOrderFor(options) to resolve per-adaptation order vs. plugin order.
  • Clone the adapter options map in Setup to avoid cross-adaptation leakage and make options-map reuse/concurrent reuse safe; add regression tests for sequential/concurrent adapts and plugin-registered ordering.
File summaries
File Description
caddyconfig/httpcaddyfile/options.go Stops mutating global directive order during order option parsing; returns per-adaptation order.
caddyconfig/httpcaddyfile/httptype.go Clones options map per adaptation and removes accumulated order; passes per-adaptation order into subroute building.
caddyconfig/httpcaddyfile/directives.go Clarifies directiveOrder as init-only, adds directiveOrderFor, and updates sorting to accept explicit order.
caddyconfig/httpcaddyfile/directives_test.go Adds tests covering isolation, reuse, error recovery, concurrency, and plugin-registered directive ordering.
caddyconfig/httpcaddyfile/builtins.go Updates route parsing to use the new buildSubroute signature (no sorting when order is nil).
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@faiyazrahmann
faiyazrahmann force-pushed the fix/directive-order-race branch from 4fb8212 to bccf07d Compare September 6, 2026 04:35

@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 added the feature ⚙️ New feature or request label Sep 6, 2026
@steadytao steadytao added this to the v2.11.5 milestone Sep 6, 2026
Comment thread caddyconfig/httpcaddyfile/directives.go Resolved
parseOptOrder wrote to the package level directiveOrder on every adapt,
and slices.DeleteFunc edits the slice in place. Since directiveOrder
starts out aliasing defaultDirectiveOrder, two adapts at once could
drop entries from the baseline for the rest of the process.

The order is now kept per adaptation in options[order] and passed
into sortRoutes and buildSubroute, so nothing writes to the global
while adapting. No lock, and no exported signatures change.

Fixes caddyserver#7994
@francislavoie
francislavoie merged commit 9dd286c into caddyserver:master Sep 6, 2026
26 checks passed
jeffyaw added a commit to YawLabs/caddy-mcp that referenced this pull request Nov 30, 2026
…ddy 2.11.6

The caddy_adapt description, the README and the code comments said both
fixes were unreleased / milestoned v2.11.5. They shipped in 2.11.6; the
README's caddy_load entry now names the 2.11.6+ 400 shape. The <= 2.11.4
caveats stay, since they still hold for those versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature ⚙️ New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent caddyfile adaptation can corrupt global directive ordering

4 participants