Repository navigation
fix(proxy): make Caddy admin writes single and safe to repeat - #555
Merged
Merged
Conversation
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
There was a problem hiding this comment.
🔵 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
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.
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.
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 inrequest()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).registerUnlockednow POSTs the server with its listener mode and its route, and stops there. The follow-upautomatic_https/ TLS-policy write and the routes GET+PATCH were redundant changes right after Caddy added its first listener, and that's wherekool startfails withPost "http://127.0.0.1:2019/...": EOFwhen it adds a new port to kool-proxy #550 failed most often. Existing servers keep the previous path.@id.registerTLSUnlockednow 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 whenautomationor the app is missing). The previous DELETE plus appending POST would leave two policies with the same@idif a resent POST had already been applied.replaceObjectByIDnow collapses duplicate IDs into one, which also cleans up duplicates an older kool may have left.Notes
TestRegisterTLSReplacesPolicyByIDsends 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.go test ./...,go test -race ./services/proxy/...,go vetand golangci-lint v2.15.0 pass locally.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.@ids at load, but two policies with the same subjects fail withcannot apply more than one automation policy to host. Caddy 2.11+ rejects duplicate IDs (caddy.go: Check whether @id is unique caddyserver/caddy#7002).Remove/RemoveProjectand the no-HTTPS path still delete the policy withDELETE /id/…, which removes only one of any duplicates an older kool left.setTLSPolicytreats only a 404 as missing, but Caddy answersnull. kool never creates a server that reaches that branch.kool startloop fromkool startfails withPost "http://127.0.0.1:2019/...": EOFwhen 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.