{"id":"XE-13398","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"560","source_url":"https://github.com/xeprotocol/xe/issues/560","reporter_id":2381,"title":"API rate-limiter sweep goroutines are immortal: +3 per Handler.Mux(), never stopped","area":"node-api","claimed_tier":"minor","assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":null,"status":"triaging","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T20:42:17.164705+00:00","updated_at":"2026-11-20T19:47:47.361955+00:00","source_created_at":"2026-11-14T11:50:06+00:00","told_at":"2026-11-20T19:46:18+00:00","status_changed_at":"2026-11-20T19:45:44.593399+00:00","url":"https://bounty.xe.network/XE-13398","body":"**Severity: Minor** (XE tier ceiling 650 XE). **CVSS 3.1: 4.0 (AV:L/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L)** — CVSS Medium at the boundary; XE tier Minor per the bounty tier table (see §7).\n\n**Target:** `github.com/xeprotocol/xe` @ `89a542ea51d38375299aee54c6c99102e9bfa666` (\"release: testnet-0005 quorum withhold fix\", 2026-11-10). Go 1.25.0.\n\n## 1. Impact (read this first)\n\nEvery `Handler.Mux()` spawns **3 rate-limiter sweep goroutines with no exit signal**; they live for process lifetime and accumulate: **+3 per `Mux()`**, **+6 per process** as `cmd` builds it (2 Handlers). Measured deterministically at HEAD: goroutines **69 → 73 after `Mux()` #1, → 76 after `Mux()` #2, still 76 after a 5 s settle** — immortal. Every in-process handler rebuild adds 3 more, plus 3 tickers each.\n\nThe impact is small per instance and bounded: 3 goroutines + 3 tickers per `Mux()` for process lifetime, accumulating only across in-process handler rebuilds. No remote trigger (a remote client cannot create `Mux()` calls), no funds, no state impact. That is why this is Minor.\n\n## 2. Root cause\n\n`newIPRateLimiter` (`api/handler.go:324`) spawns the sweep with no lifecycle:\n\n```go\ngo rl.sweepLoop()\n```\n\n`sweepLoop` (`api/handler.go:350`) ranges over a `time.Ticker` with no done channel, no ctx, and the ticker is never stopped. `Mux()` (`:226`) calls `newRateLimiters()` per invocation (`:238`), and `newRateLimiters()` (`:306-310`) builds 3 limiters (read/write/probe) — so each `Mux()` adds 3 sweeps. This is the only `sweepLoop` in the tree with no exit path (contrast `store/runGC`, which has `gcQuit`/`gcDone` and is joined by `Close()`). No `Close`/`Stop`/done path exists for `ipRateLimiter` anywhere in `api/`.\n\n## 3. Scenario (how it happens)\n\n1. The node builds an API handler and calls `Mux()` — 3 sweep goroutines start (read, write, probe limiters).\n2. `cmd` builds a second handler — 3 more sweeps (6 per process).\n3. Any in-process handler rebuild (new Handler + `Mux()`, e.g. in tests, embedders, or a restart path that rebuilds the API layer) adds 3 more. Nothing ever stops any of them: after a 5 s settle the count is flat, and it stays flat for process lifetime — the sweeps touch only their own `entries` map + `mu`, ticking forever.\n\n## 4. Proof of concept (local, deterministic)\n\nPoC: `hunt-v3/stage3/tests/` — `TestT39a_RateLimiterSweeps` (`api/`, `-race`, 5.06 s), re-run byte-identical at HEAD in an isolated worktree copy with temp data dirs. The test fails only if a `Mux()` adds fewer than 3 goroutines or if the count drops after the settle.\n\nRe-run result at HEAD (2026-11-14), same as V3 (2026-11-11):\n\n| Observation | V3 | Re-run at HEAD |\n|---|---|---|\n| Goroutines before → after `Mux()` #1 → after `Mux()` #2 | 70 → 73 → 76 | 69 → 73 → 76 |\n| After 5 s settle | still 76 | still 76 |\n| Race detector | 0 `DATA RACE` | 0 `DATA RACE` |\n\nNote: the first `Mux()` shows +4 rather than V3's +3 (69 → 73 vs 70 → 73). The second `Mux()` is exactly +3 and the settle count is flat at 76, so the extra +1 on the first call is a transient goroutine counted in the pre/post snapshot, not a change in sweep behaviour. The mechanism (3 immortal sweeps per `Mux()`) is unchanged.\n\nLog: `hunt-v4-followup/stage3-minors/logs/t39a-reverify.log`. The test file was copied byte-identical (`cmp` clean; sha256 `1af7719d4784bc1d6c3affb816f22856d50809f3d67a498b44a17c14421c1604` for `api/stage3_t39_test.go`).\n\n## 5. Attacker prerequisites\n\n- **None — local only, no attacker.** A remote client cannot create `Mux()` calls; the trigger is local handler construction. No remote step, no privilege on the victim.\n\n## 6. Independent validation and false-positive assessment\n\n- **Deterministic reproduction, twice:** V3 (2026-11-11) and the HEAD re-run (2026-11-14) give the same counts (second `Mux()` exactly +3, settle flat at 76). The claim is a measured goroutine count, not a pattern match.\n- **Disproof attempted:** checked whether any `Close`/`Stop`/done path exists for `ipRateLimiter` — none in `api/`; the sweeps touch only their own `entries` map + `mu`, so no data race, only the lifetime leak.\n- **Duplicate check (fresh, DB 2,421 entries as of 2026-11-14 ~17:00 WIB):** the closest filed items are a *different defect in the same file*: xe#172 / XE-0112 / XE-6671 / XE-10121 — the rate-limiter map hits `maxRateLimiterIPs` and every NEW client IP is denied by a permanent 0/0 limiter (a fail-closed map-capacity DoS, not goroutine lifetime). Its fix PRs were read in full this run: **PR #410** (\"evict LRU entries on rate limiter capacity and group IPv6 prefixes\") and **PR #413** (\"implement LRU eviction for rate limiter IP map\") both fix the 0/0 capacity defect — and both merely *optimize the sweep's scan* (\"sweepLoop() now scans from the LRU tail backwards and terminates immediately upon encountering the first unexpired entry\"). Neither stops the sweep, adds a done channel/ctx, stops the ticker, or changes the one-sweep-per-`newIPRateLimiter` spawn — and both are open/unmerged in any case. XE-12304 (`syncRateLimiter.last` map grows without bound) is a different limiter (`net/sync.go`) and a map-growth defect, not sweep lifetime. Classification: **NOVEL on public data** (private Critical/Severe reports may exist; uniqueness is not guaranteed). Full analysis: `duplicate-intel/reports/v4f-t39a-ratelimiter-duplicate-analysis.md`.\n\n## 7. Severity justification (Minor, ceiling 650 XE)\n\nCVSS 3.1: **4.0 (AV:L/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L)**. Attack vector is local (handler construction); complexity is Low (every `Mux()` adds 3); no privileges or user interaction; availability impact is Low (3 goroutines + 3 tickers per `Mux()`, accumulating only across in-process rebuilds; no crash, no state impact, no funds). The CVSS score sits exactly on the Low/Medium boundary (4.0); the XE tier is Minor because the bounty tier table prices this class — a tiny bounded lifecycle leak with no remote trigger — as Minor, and the CVSS number is informational here.\n\nWhy not higher: the per-instance cost is negligible (3 goroutines); it needs in-process handler rebuilds to accumulate to anything noticeable.\n\n## 8. Fix suggestion\n\nGive `sweepLoop` an exit signal: a done channel (or a `Mux()`-scoped ctx) closed by a `Handler.Close()`/`Mux()`-owned teardown, with the ticker stopped on exit — or move the limiters to a single shared, closable instance instead of building 3 new sweeps per `Mux()`. Either is a one-line lifecycle; note that PR #410/#413 (LRU eviction) do not fix this and must not be cited as fixing it.\n\n## 9. Caveats (kept in the report)\n\n1. Per-instance leak is tiny (3 goroutines + 3 tickers per `Mux()`); accumulation needs in-process handler rebuilds.\n2. PR #410 and PR #413 fix the adjacent 0/0 map-capacity defect (xe#172/#396), not this defect — and both are open/unmerged.\n3. NOVEL means no matching *public* finding as of the 2026-11-14 ~17:00 WIB check (DB 2,421 entries); private reports may exist.\n\n## 10. Reproduction (for the committee)\n\nLocal only, worktree copy at `89a542e`, isolated temp data dirs:\n\n```bash\nexport PATH=~/workspace/go/go/bin:$PATH\nexport TMPDIR=~/workspace/xe-bounty/.tmp\ncd <worktree>/api\ngo test -race -count=1 -timeout 5m -run 'TestT39a_RateLimiterSweeps$' -v ./...\n# Expected: PASS (exit 0) — read the goroutine counts from the log:\n# 69 -> 73 -> 76, still 76 after a 5 s settle.\n```\n\nPoC: `hunt-v3/stage3/tests/` (`api/stage3_t39_test.go`). Log: `hunt-v4-followup/stage3-minors/logs/t39a-reverify.log`. Full analysis: `hunt-v3/stage3/STAGE3_VERDICT.md` §T3-9a; duplicate analysis: `duplicate-intel/reports/v4f-t39a-ratelimiter-duplicate-analysis.md`.","patch_refs":[],"thread":[{"id":15195,"source":"github","source_ref":"7049040369","author":"Joseph Denne","team":true,"body":"Thanks for the report. We have read it against the code and confirmed the behaviour you describe. It is being handled, and this ticket will be updated when there is a change to check.","created_at":"2026-11-20T19:46:18+00:00"}]}