Skip to content

auth: fix tailscaled connection leak from Authenticate's value receiver - #140

Open
Lockyc wants to merge 2 commits into
tailscale:mainfrom
Lockyc:fix/auth-localclient-caching
Open

Lockyc wants to merge 2 commits into
tailscale:mainfrom
Lockyc:fix/auth-localclient-caching

Conversation

@Lockyc

@Lockyc Lockyc commented Aug 21, 2026 •

Copy link
Copy Markdown

The bug

Auth.Authenticate has a value receiver, so it runs against a copy of the module. client() (pointer receiver) then fills ta.localclient on that copy, which is discarded when Authenticate returns. localclient is unexported and there is no Provision, so the cache can never be warm: every request through a tailscale_auth route builds a fresh local.Client, whose fresh http.Transport dials a new connection to tailscaled's LocalAPI socket and is then dropped without CloseIdleConnections(). The transport's readLoop goroutine 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.sock connections, with net/http.(*Transport).dialConn accounting for 58% of live heap bytes under local.(*Client).DoLocalRequest.

The fix

Give Authenticate a pointer receiver so the cache sticks (local.Client builds its http.Client under a sync.Once and is safe to share — one pooled connection then serves every request). The now genuinely shared lazy fill in client() needs a mutex, since Authenticate runs concurrently; the mutex makes Auth non-copyable, so CaddyModule takes a pointer receiver and the two sites that copied an Auth value (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 Authenticate into Auth's method set, making the value type satisfy caddyauth.Authenticator. It fails on the unfixed code.

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
Lockyc force-pushed the fix/auth-localclient-caching branch from 785bb52 to 1cfd8f3 Compare August 21, 2026 06:59
@hanuele

hanuele commented Sep 17, 2026

Copy link
Copy Markdown

Independent confirmation from a production deployment — this leak took one of our hosts to full swap.

Setup: caddy v2.11.4 with caddy-tailscale v0.0.0-20260826180304-de41b249af4f (the latest published version today), tailscale.com v1.102.2, tailscale_auth in front of a reverse_proxy, talking to the system tailscaled.

Before (2 days 7 hours uptime, light traffic from one user):

  • caddy RSS 4.7 GB; 515,226 goroutines, 257,593 of them in net/http.(*persistConn).writeLoop
  • live heap 3.4 GB, 2.33 GB cumulative under net/http.(*Transport).dialConn, reached via caddy-tailscale.Auth.Authenticate -> local.(*Client).sendWithHeaders
  • tailscaled RSS 3.7 GB; after restarting caddy alone it fell to 0.47 GB, so tailscaled's growth was the far end of the leaked connections

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 Authenticate, lazy localclient init under a mutex), rebuilt with otherwise identical dependency versions:

  • A/B on side ports, 300 authenticated requests each, all 200: unpatched 17 -> 618 goroutines, patched 17 -> 20.
  • In production for 15 minutes after the swap: goroutines 21..64 with no trend, RSS 51 -> 56 MB.

We are running this patch locally until it lands. Would be great to see it merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants