Skip to content

fix(proxy): make Caddy admin writes single and safe to repeat - #555

Merged
fabriciojs merged 1 commit into
mainfrom
fix/proxy-admin-requests
Dec 1, 2026
Merged

fabriciojs merged 1 commit into
mainfrom
fix/proxy-admin-requests

Conversation

@fabriciojs

Copy link
Copy Markdown
Member
Issue #554
🪲 Bug Fix Yes
🧰 Improvement Yes
🏆 Feature No
📝 Refactor No
❌ Removed No
📖 Documentation No
⚠️ Break Change No

Description

Follow-up to #551. Every config change makes Caddy replace its admin server, and #551 resends an Admin API request once when the old server drops it. This PR makes that resend fire less often and keeps it harmless when it does.

  • No connection reuse. Every admin request is sent with Connection: close, so kool never sends a request on a connection the old admin server is closing (race A in Proxy: make Caddy admin requests robust beyond the #551 retry #554). This is set per request in request() rather than on the client's transport, so it also applies when tests replace the transport. The fix: retry Caddy admin requests that get no answer #551 retry stays for a new connection dropped while the old server shuts down (race B).
  • New server in one change. For a port without a listener, registerUnlocked now POSTs the server with its listener mode and its route, and stops there. The follow-up automatic_https / TLS-policy write and the routes GET+PATCH were redundant changes right after Caddy added its first listener, and that's where kool start fails with Post "http://127.0.0.1:2019/...": EOF when it adds a new port to kool-proxy #550 failed most often. Existing servers keep the previous path.
  • TLS policy written by @id. registerTLSUnlocked now reads the tls app, replaces or adds the project's policy by @id, and writes it back with one PATCH of the list (or a PATCH/POST of the app when automation or the app is missing). The previous DELETE plus appending POST would leave two policies with the same @id if a resent POST had already been applied. replaceObjectByID now collapses duplicate IDs into one, which also cleans up duplicates an older kool may have left.

Notes

  • Tests: new tests check that connections are not reused, that a dropped server POST is resent unchanged, and the exact request sequence for new and existing HTTP/HTTPS servers. TestRegisterTLSReplacesPolicyByID sends the TLS write twice from 8 starting states and checks one policy remains per @id, with other projects' policies kept. Each new test fails with its fix reverted.
  • Checks: go test ./..., go test -race ./services/proxy/..., go vet and golangci-lint v2.15.0 pass locally.
  • Real Caddy: against caddy:2.10-alpine, new HTTP/HTTPS servers come out byte-identical to the previous multi-step sequence, and TLS registration ends with exactly one kool policy from 9 starting states. 5 of those states (e.g. {}, automation: null, policies: null) left a stale duplicate or failed with the old code.
  • Caddy versions: Caddy 2.10 accepts duplicate @ids at load, but two policies with the same subjects fail with cannot apply more than one automation policy to host. Caddy 2.11+ rejects duplicate IDs (caddy.go: Check whether @id is unique caddyserver/caddy#7002).
  • Not covered here:
    • Remove/RemoveProject and the no-HTTPS path still delete the policy with DELETE /id/…, which removes only one of any duplicates an older kool left.
    • setTLSPolicy treats only a 404 as missing, but Caddy answers null. kool never creates a server that reaches that branch.
  • Still to verify: the Docker-in-Docker kool start loop from kool start fails with Post "http://127.0.0.1:2019/...": EOF when it adds a new port to kool-proxy #550 hasn't been re-run on this branch. The races didn't reproduce through Docker for Mac port forwarding, with or without this change.

Every config change makes Caddy replace its admin server, and the old
server can close connections while kool is still sending requests.
#551 resends a request once when that happens; this makes the resend
fire less often and keeps it harmless when it does.

- Send every admin request with Connection: close. The old admin server
  closes the connections it served, so a reused keep-alive connection
  could fail a POST or PATCH with EOF. The retry still covers a new
  connection dropped while the old server shuts down.
- Create a new listener's server with its route in a single POST.
  The server body already sets automatic_https or
  tls_connection_policies, so the follow-up listener-mode write and the
  routes GET+PATCH were redundant changes right after Caddy added its
  first listener. Existing servers keep the previous path.
- Write the project's TLS automation policy by @id: read the tls app,
  replace or add the policy, and PATCH the list back (or set the app or
  its automation when missing). The previous DELETE plus appending POST
  would add a second policy with the same @id if a resent POST had
  already been applied. Duplicates already present collapse into one.

Closes #554
Copilot AI balanced review requested due to automatic review settings December 1, 2026 01:16
@github-actions github-actions Bot added release-drafter:changed Release drafter: Changed release-drafter:fixed Release drafter: Fixed release-drafter:minor Release drafter: Minor release-drafter:patch Release drafter: Patch labels Dec 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The required Docker-in-Docker start loop remains unverified, so human runtime validation is needed to confirm that the timing-sensitive Caddy admin-server shutdown races are resolved.

No open issues 🎉

What changed in this PR

This PR strengthens Caddy Admin API handling in the proxy manager, building on #551’s retry logic to reduce dropped requests and make TLS policy writes safe to repeat.

Changes:

  • Disables connection reuse for admin requests while retaining the retry.
  • Creates new servers with their listener settings and route in a single write.
  • Replaces TLS policies by @id, removes matching duplicates, and adds regression tests for request sequences and retries.
File Description
services/​proxy/​manager.go Avoids connection reuse, consolidates server creation, and makes TLS policy writes repeatable.
services/​proxy/​manager_test.go Tests connection isolation, retried writes, server request sequences, and TLS policy replacement.

Review effort: Balanced

@fabriciojs
fabriciojs merged commit 12d7a25 into main Dec 1, 2026
10 checks passed
@fabriciojs
fabriciojs deleted the fix/proxy-admin-requests branch December 1, 2026 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-drafter:changed Release drafter: Changed release-drafter:fixed Release drafter: Fixed release-drafter:minor Release drafter: Minor release-drafter:patch Release drafter: Patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants