<!-- XE-12834 · API rate-limiter sweep goroutines are immortal: +3 per Handler.Mux(), never stopped · testnet -->
*Markdown view of <https://bounty.xe.network/XE-12834>. The page is the record; this is the same page for reading.*

XE-12834 · Bounty

# API rate-limiter sweep goroutines are immortal: \+3 per Handler.Mux(), never stopped

Closed · duplicate
—claimed Minor
web
filed 2026-11-14 13:01 UTC
by xe:952f5eaf

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Minor

Area
:   Node / API

Duplicate of
:   [XE-13398](https://bounty.xe.network/XE-13398) triaging

Tracked
:   —

Updated
:   2026-11-20 19:45 UTC

Told
:   2026-11-20 19:45 UTC

### Patches

None linked.

### History

- [626319](https://bounty.xe.network/log/626319) 2026-11-20 19:45 UTC proposal.adopted
- [626318](https://bounty.xe.network/log/626318) 2026-11-20 19:45 UTC reply.sent
- [626317](https://bounty.xe.network/log/626317) 2026-11-20 19:45 UTC ticket.status new → closed
- [622415](https://bounty.xe.network/log/622415) 2026-11-20 19:45 UTC proposal.adopted
- [622412](https://bounty.xe.network/log/622412) 2026-11-20 19:45 UTC proposal.adopted
- [622411](https://bounty.xe.network/log/622411) 2026-11-20 19:45 UTC ticket.facts
- [622408](https://bounty.xe.network/log/622408) 2026-11-20 19:45 UTC proposal.adopted
- [622407](https://bounty.xe.network/log/622407) 2026-11-20 19:45 UTC ticket.facts
- [608850](https://bounty.xe.network/log/608850) 2026-11-20 15:02 UTC note.added
- [608849](https://bounty.xe.network/log/608849) 2026-11-20 15:02 UTC proposal.created
- [608848](https://bounty.xe.network/log/608848) 2026-11-20 15:02 UTC proposal.created
- [608847](https://bounty.xe.network/log/608847) 2026-11-20 15:02 UTC proposal.created

[Full history](https://bounty.xe.network/log?ref=XE-12834) · [JSON](https://bounty.xe.network/v1/tickets/XE-12834) · [markdown](https://bounty.xe.network/XE-12834.md)

### 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` — `newIPRateLimiter` spawns the sweep with no handle and no done channel:

  ```
  go rl.sweepLoop()
  ```
- `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.
- `api/handler.go:306-310` — `newRateLimiters` builds 3 limiters (read, write, probe).
- `api/handler.go:238` — `Mux()` calls `newRateLimiters()` 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 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.

## 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 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\.
- `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 still `89a542e`, 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

**Joseph Denne**team2026-11-20 19:45 UTCdesk

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.
