Repository navigation
Description
Claude an AI agent posting this on behalf of the account owner,
Shkeats, who will review it and follow up.
Shkeatshas been trying to use Kool in a multi-agent worktree environment: several agents work in parallel git worktrees of the same project, and Kool'sworkspacesandproxysupport gives each worktree its own stack. An earlier problem with that setup came up on Fix DNS-unsafe proxy upstream aliases (#543). This is the next one.
Describe the bug
kool start sometimes fails while it registers a proxy route on a port that has no listener in kool-proxy yet. The error names the Admin API request that failed, usually:
Post "http://127.0.0.1:2019/config/apps/http/servers/kool-8000/automatic_https": EOF
- The number in
kool-8000is the route's listen port. - Less often the failed request is a PATCH, for example
Patch "http://127.0.0.1:2019/config/apps/http/servers/kool-8000/routes": EOF. - Running
kool startagain usually works. - On the Mac described below it failed on 4 of 6 starts that added a new port during project setup.
- We found no existing issue about it.
Kool version and environment
kool -v:kool version 3.9.0- macOS, bash 5.3, OrbStack 2.2.5 (Docker Engine 29.8.2, client 29.10.0, Docker Compose v5.5.0), not DinD
- Also reproduced in Docker-in-Docker (
docker:29-dind, Docker Engine 29.10.0) on the same Mac - Proxy image
caddy:2.10-alpine(Caddy v2.10.2) kool infoenvironment variables (project name replaced):
KOOL_DATABASE_PORT=33062
KOOL_MAILHOG_SMTP_PORT=1035
KOOL_APP_PORT=8000
KOOL_MAILHOG_PORT=8035
KOOL_WORKSPACES_ENABLED=true
KOOL_WORKSPACE_SOURCE_PROJECT=myproject-agents
KOOL_PROXY_ENABLED=true
KOOL_PROXY_DOMAIN=localhost
KOOL_PROXY_HOST=localhost
KOOL_NAME=myproject
KOOL_GLOBAL_NETWORK=kool_global
KOOL_ASUSER=501
To Reproduce
These steps remove kool-proxy and its saved routes, so use a machine or Docker-in-Docker engine where no other project uses the Kool proxy.
- Create a project with a
docker-compose.ymlthat has one serviceapp(we usednginx:1-alpine) and thiskool.yml:
workspaces:
- app
proxy:
domain: localhost
routes:
app:
ports: ["8000:80"]
hosts: ["@"]- Remove the proxy and its saved routes:
docker rm -f kool-proxy && rm -f ~/.kool/proxy/caddy.json - Run
kool start. - Run
kool stop, then repeat steps 2 and 3. About 1 start in 4 fails.
We first saw the failure in a different situation: kool-proxy already ran for other worktrees, and kool start added a port it did not publish yet. We could not reproduce that case in isolation (see "How we checked").
Expected behavior
kool start registers the route on the first attempt.
Screenshots
kool start (3.9.0) in Docker-in-Docker, following the steps above. The first start fails and the second succeeds.
The same session as text:
$ kool -v
kool version 3.9.0
$ kool start
e000d866571009da00802625cfcdeb45694d0ffd6e9c201f7421976201c1e1c8
Post "http://127.0.0.1:2019/config/apps/http/servers/kool-8000/automatic_https": EOF
$ echo $?
1
$ kool start
Container first-app-1 Creating
Container first-app-1 Created
Container first-app-1 Starting
Container first-app-1 Started
$ echo $?
0
Additional context
What happens, from Kool's and Caddy's code and the tests below:
- For a port without a listener,
registerUnlockedcreates the Caddy server with a POST (L908), then sends a POST to itsautomatic_https(L921, L1345), then reads and PATCHes its routes. - Each configuration change makes Caddy start a new admin server and stop the old one in a goroutine (caddy.go L555-L561, admin.go L390-L403). After answering, the old server closes the connection that carried the change, and any other idle connection.
- Go's HTTP server adds
Connection: closeto the answer only if the shutdown has already started when it writes the answer (server.go L1301, L1389; Caddy v2.10.2 is built with Go 1.25.0). Otherwise it closes the connection without telling the client (server.go L2127-L2133). - Kool's admin client keeps connections alive (L85-L87). For an answer without a body, Go's
Transportputs the connection back in its pool before it returns the answer (transport.go L2385-L2393; Kool 3.9.0 is built with Go 1.26.11), so Kool sends its next request on the connection Caddy is closing, and getsEOF. - Go's
Transportretries that request only if it is idempotent (transport.go L818). A GET is retried without an error; a POST or PATCH fails.
In our tests the answer lacked Connection: close only when the change added the first listener to a Caddy that had none. Recreating kool-proxy to publish a new port is not itself a change: Caddy logs config is unchanged for the PATCH that restores the saved routes (L617).
How we checked
Real kool start 3.9.0 in Docker-in-Docker, with a throwaway project that has one nginx:1-alpine service:
- Starting with no
kool-proxyand no saved routes: 6 of 20 starts failed, then 8 of 40. - Adding a new port to a
kool-proxythat already held another project's route: 0 of 25 starts failed. - With keep-alives disabled on the admin client: 1 of 40 starts still failed, and a second run failed again within 13 starts. Caddy's log of that failure shows that the failed request never reached a handler, so its new connection was dropped while the old admin server shut down. From the source: the old and new admin servers listen on the same port with
SO_REUSEPORT(listen_unix.go L135-L141), and Linux resets a connection still queued on a listener when that listener closes. - With the fix suggested below: 0 of 80 starts failed.
Kool's own register code in a Go test, against a disposable caddy:2.10-alpine container (not kool-proxy) that started with no listeners:
- First registration: 5 of 21 failed in Docker-in-Docker, 2 of 15 from macOS through OrbStack's port forwarding.
- Later registrations on the same container: 0 of 239 in Docker-in-Docker, 0 of 29 from macOS.
- Replaying Kool's whole sequence for adding a port (snapshot, recreate with the extra port, restore, register, register again after a pause) from macOS, with one listener already configured: 0 of 20 failed.
Raw HTTP requests to the same disposable container, for the first configuration change after it starts:
- No listeners configured: the answer lacked
Connection: closein 4 of 15 starts from macOS and 29 of 51 in Docker-in-Docker. - Four listeners configured: the answer had
Connection: closein 15 of 15 starts, and 15 of 15 again with the container under CPU load. - Changes after the first: the answer had
Connection: closein 30 of 30. - When the header was missing, a request sent straight away on the same connection failed in 12 of 15 tries.
Suggested fix
Draft pull request: fix: retry Caddy admin requests that get no answer (#551)
- In
request()(L1671-L1678), send an Admin API request once more, after 100 ms, when the connection closes without an answer (EOFor connection reset). - In both failure modes Caddy closes the connection before it reads the request: the reused connection after its answer (point 3 above), and the new connection before a handler sees it ("How we checked"). Sending the request again is what Kool intended. Kool's requests set, replace or delete a value, so a repeat is harmless even if Caddy had applied the first one. The exception is the POST to
/automation/policies(L1437), which appends: if Caddy had applied it, the repeat would add a second, identical TLS policy. - Disabling keep-alives on its own is not enough: it removes the reused connection but not the dropped new connection (1 of 40 starts still failed).
Activity
fix: retry Caddy admin requests that get no answer