Repository navigation
Conversation
Authenticate had a value receiver, so it ran on a copy of Auth: the localclient cache filled by client() landed in that copy and was lost on return. localclient is unexported and there is no Provision, so the cache could never be warm — every gated request built a fresh local.Client, dialled a new connection to tailscaled's LocalAPI socket, and dropped it without CloseIdleConnections(). The transport's readLoop goroutine keeps the dropped connection alive, so each request permanently costs a socket, two goroutines, and buffers — in caddy and in tailscaled, which holds the far end. Measured live: ~56 KiB of anon memory per gated request; after 3,000 requests, caddy held 7,792 goroutines and 3,835 open tailscaled.sock connections. Give Authenticate a pointer receiver so the cache sticks; local.Client builds its http.Client under a sync.Once and is safe to share. The now-shared lazy fill needs a mutex, which makes Auth non-copyable, so CaddyModule and the two sites that copied an Auth value take pointers too. go vet copylocks is clean. The test asserts the method set — a value receiver is exactly what puts Authenticate back into Auth's method set — and fails on the unfixed code. Signed-off-by: Lachlan Collins <1438799+Lockyc@users.noreply.github.com>
Lockyc
force-pushed
the
fix/auth-localclient-caching
branch
from
August 21, 2026 06:59
785bb52 to
1cfd8f3
Compare
|
Independent confirmation from a production deployment — this leak took one of our hosts to full swap. Setup: caddy v2.11.4 with caddy-tailscale Before (2 days 7 hours uptime, light traffic from one user):
Regrowth after a plain caddy restart (unpatched): 113 -> 1,636 goroutines in 7 minutes (about 218/min), RSS 56 -> 84 MB. With exactly the change in this PR (pointer receiver on
We are running this patch locally until it lands. Would be great to see it merged. |
This was referenced Oct 31, 2026
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.
The bug
Auth.Authenticatehas a value receiver, so it runs against a copy of the module.client()(pointer receiver) then fillsta.localclienton that copy, which is discarded whenAuthenticatereturns.localclientis unexported and there is noProvision, so the cache can never be warm: every request through atailscale_authroute builds a freshlocal.Client, whose freshhttp.Transportdials a new connection to tailscaled's LocalAPI socket and is then dropped withoutCloseIdleConnections(). The transport'sreadLoopgoroutine keeps the dropped connection reachable forever, so each gated request permanently costs a unix socket, two goroutines, and a bufio pair — in caddy and in tailscaled, which holds the far end.Measured on a live deployment: ~56 KiB of anon memory per gated request (vs ~1.8 KiB for the same backend via a non-gated route). After 3,000 gated requests the caddy process held 7,792 goroutines in two matched stacks and 3,835 open
tailscaled.sockconnections, withnet/http.(*Transport).dialConnaccounting for 58% of live heap bytes underlocal.(*Client).DoLocalRequest.The fix
Give
Authenticatea pointer receiver so the cache sticks (local.Clientbuilds itshttp.Clientunder async.Onceand is safe to share — one pooled connection then serves every request). The now genuinely shared lazy fill inclient()needs a mutex, sinceAuthenticateruns concurrently; the mutex makesAuthnon-copyable, soCaddyModuletakes a pointer receiver and the two sites that copied anAuthvalue (RegisterModule,parseAuthConfig) use a pointer.go vet(copylocks) is clean.The test
A behavioural test would need a live tailscaled, so the added test asserts the method set instead — a value receiver is exactly what puts
AuthenticateintoAuth's method set, making the value type satisfycaddyauth.Authenticator. It fails on the unfixed code.