API rate-limiter sweep goroutines are immortal: +3 per Handler.Mux(), never stopped
Report
[Minor] API rate-limiter sweep goroutines are immortal: +3 per Handler.Mux(), never stopped
Area: node-api
Repo pin: xeprotocol/xe @ 89a542e (HEAD, verified 2026-11-14)
Suggested tier: Minor (CVSS 4.0 — local only, no remote trigger; each leaked goroutine is one 5-minute ticker, so the cost per leak is small; repeated Mux() construction only)
Related public issue: xeprotocol/xe#560 (same ground, filed by another researcher; this report is an independent reproduction with our own Go test and our own measurements)
Summary
Every Handler.Mux() call leaks 3 goroutines that run forever. newIPRateLimiter spawns go rl.sweepLoop(), and sweepLoop ranges over a ticker with no done channel — the ticker is never stopped and the loop has no exit condition. Mux() builds a fresh set of 3 limiters (read/write/probe) on every invocation, so each call adds 3 immortal goroutines. There is no Close/Stop on the limiter or the limiter set, so nothing can ever retire them.
Root cause
api/handler.go:324-331—newIPRateLimiterspawns the sweep with no handle and no done channel:go rl.sweepLoop()api/handler.go:350-362—sweepLooprangesticker.Cforever; the ticker (rateLimiterTTL = 5 * time.Minute,:291) is never stopped and there is nodonechannel or context check.api/handler.go:306-310—newRateLimitersbuilds 3 limiters (read, write, probe).api/handler.go:238—Mux()callsnewRateLimiters()on every invocation and passes the set into the middleware; the set is never retained anywhere stoppable.
Reproduction (our own Go test, api/zz_r56_m3_sweep_b56_test.go)
White-box test against the real newRateLimiters / Mux() code. Measured at HEAD, 2026-11-14:
| Step | Goroutines | Delta | sweepLoop stacks |
|---|---|---|---|
| baseline | 2 | — | 0 |
5× newRateLimiters() (handles discarded) |
17 | +15 (exactly 5×3) | 15 |
+ 3× Handler.Mux() |
26 | +24 total (exactly 8 sets × 3) | 24 |
| + 2 s wait | 26 | +0 — none exited | 24 |
The counts are exact (not approximate): every limiter set contributes exactly 3 goroutines, every one of them is a sweepLoop stack, and none exits over time — there is no code path that can stop them. (Note: runtime.Stack(all=true) was used for attribution because pprof debug=1 coalesces identical stacks into a single "N goroutines" entry.)
Impact / worst case
- Honestly bounded: in the normal single-
Mux()production shape this leaks exactly 3 goroutines for the life of the process — negligible. The impact is on code that builds muxes repeatedly (tests, embedders, API servers rebuilt per reconfiguration): 3 immortal goroutines perMux(), each holding a 5-minute ticker and a reference to its limiter's IP map, growing without bound. No remote trigger, no crash, no state corruption, no funds. Hence Minor.
Duplicate check (re-verified at filing time, 2026-11-14 ~12:52–13:0x UTC)
- Board search
q=sweepLoop→ XE-0112, XE-6540, XE-10121 — all three file the 0/0 map-capacity deny defect (same file, different defect: when the limiter map hitsmaxRateLimiterIPs, new IPs get a permanentrate.NewLimiter(0,0)). XE-0112 (triaging Medium, read in full) treatssweepLoopas the eviction mechanism, not as a leak; none of the three mentions goroutine lifetime. XE-6540 and XE-10121 are the same deny defect filed by reporter 730. q="goroutine leak"→ 8 hits, all different functions: XE-3444 and XE-5064 (tunnel proxy loops), XE-5430 (cert-sync pull churn), XE-9847 and XE-12232 (P2P sync), XE-3023 (tunnel lease), XE-10008 (cert handler copy/sort), XE-3910 (logging writer). None touches the API rate limiter.- No board ticket names an immortal rate-limiter sweep.
q=aurorabelle788→ 1 hit, XE-12805 (our own earlier filing) — the #560 author has not filed on the board. No new public filings since XE-12805 (XE-12806–XE-12825 all 404); xe HEAD still89a542e, pull-refs still 186, max PR #556 — no fix PR.
Remediation suggestion
Give ipRateLimiter a done channel (or a context) checked in sweepLoop, stop the ticker on exit, and add a Close() on the rateLimiters set that Mux() callers (or a Handler.Close) can invoke. Alternatively, build the limiter set once per Handler instead of once per Mux() call.
Thread
Thanks. This is the same finding as https://bounty.xe.network/XE-13398, which was filed first; the programme pays the first verifiable submission, so this one closes as a duplicate. If you believe it is a different bug, file a new report that links https://bounty.xe.network/XE-12834 and says what differs; it is read like any other.