{"id":"XE-12834","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":827,"title":"API rate-limiter sweep goroutines are immortal: +3 per Handler.Mux(), never stopped","area":"node-api","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-13398","tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T13:01:06.261696+00:00","updated_at":"2026-11-20T19:45:56.378646+00:00","source_created_at":null,"told_at":"2026-11-20T19:45:56.405589+00:00","status_changed_at":"2026-11-20T19:45:56.387576+00:00","url":"https://bounty.xe.network/XE-12834","body":"# [Minor] API rate-limiter sweep goroutines are immortal: +3 per Handler.Mux(), never stopped\n\n**Area:** node-api\n**Repo pin:** `xeprotocol/xe` @ `89a542e` (HEAD, verified 2026-11-14)\n**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)\n**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)\n\n## Summary\n\nEvery `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.\n\n## Root cause\n\n- `api/handler.go:324-331` — `newIPRateLimiter` spawns the sweep with no handle and no done channel:\n  ```go\n  go rl.sweepLoop()\n  ```\n- `api/handler.go:350-362` — `sweepLoop` ranges `ticker.C` forever; the ticker (`rateLimiterTTL = 5 * time.Minute`, `:291`) is never stopped and there is no `done` channel or context check.\n- `api/handler.go:306-310` — `newRateLimiters` builds 3 limiters (read, write, probe).\n- `api/handler.go:238` — `Mux()` calls `newRateLimiters()` on every invocation and passes the set into the middleware; the set is never retained anywhere stoppable.\n\n## Reproduction (our own Go test, `api/zz_r56_m3_sweep_b56_test.go`)\n\nWhite-box test against the real `newRateLimiters` / `Mux()` code. Measured at HEAD, 2026-11-14:\n\n| Step | Goroutines | Delta | `sweepLoop` stacks |\n|---|---|---|---|\n| baseline | 2 | — | 0 |\n| 5× `newRateLimiters()` (handles discarded) | 17 | **+15 (exactly 5×3)** | 15 |\n| + 3× `Handler.Mux()` | 26 | **+24 total (exactly 8 sets × 3)** | 24 |\n| + 2 s wait | 26 | +0 — **none exited** | 24 |\n\nThe 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.)\n\n## Impact / worst case\n\n- **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 per `Mux()`, 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.\n\n## Duplicate check (re-verified at filing time, 2026-11-14 ~12:52–13:0x UTC)\n\n- 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 hits `maxRateLimiterIPs`, new IPs get a permanent `rate.NewLimiter(0,0)`). XE-0112 (triaging Medium, read in full) treats `sweepLoop` as 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.\n- `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.\n- 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 still `89a542e`, pull-refs still 186, max PR #556 — no fix PR.\n\n## Remediation suggestion\n\nGive `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.","patch_refs":[],"thread":[{"id":15361,"source":"desk","source_ref":"reply:13252","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-13398, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-12834 and says what differs; it is read like any other.","created_at":"2026-11-20T19:45:56.378646+00:00"}]}