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

XE-13398 · Bounty

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

Triaging
claimed Minor
[xe#560](https://github.com/xeprotocol/xe/issues/560)
filed 2026-11-14 11:50 UTC
by Aurora Belle

Verdict
:   published when the fix ships

Tier
:   published when the fix ships

Claimed
:   Minor

Area
:   Node / API

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-20 19:47 UTC

Told
:   2026-11-20 19:46 UTC

### Patches

None linked.

### History

- [627234](https://bounty.xe.network/log/627234) 2026-11-20 19:46 UTC proposal.adopted
- [627233](https://bounty.xe.network/log/627233) 2026-11-20 19:46 UTC ticket.award
- [627187](https://bounty.xe.network/log/627187) 2026-11-20 19:46 UTC reply.mirrored
- [626872](https://bounty.xe.network/log/626872) 2026-11-20 19:46 UTC github.labels
- [625873](https://bounty.xe.network/log/625873) 2026-11-20 19:45 UTC proposal.adopted
- [625872](https://bounty.xe.network/log/625872) 2026-11-20 19:45 UTC reply.sent
- [625515](https://bounty.xe.network/log/625515) 2026-11-20 19:45 UTC proposal.adopted
- [625514](https://bounty.xe.network/log/625514) 2026-11-20 19:45 UTC ticket.status new → triaging
- [624251](https://bounty.xe.network/log/624251) 2026-11-20 19:45 UTC proposal.adopted
- [624250](https://bounty.xe.network/log/624250) 2026-11-20 19:45 UTC ticket.facts area
- [624248](https://bounty.xe.network/log/624248) 2026-11-20 19:45 UTC proposal.adopted
- [624247](https://bounty.xe.network/log/624247) 2026-11-20 19:45 UTC ticket.facts

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

### Report

**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).

**Target:** `github.com/xeprotocol/xe` @ `89a542ea51d38375299aee54c6c99102e9bfa666` ("release: testnet-0005 quorum withhold fix", 2026-11-10). Go 1.25.0.

## 1\. Impact (read this first)

Every `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.

The 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.

## 2\. Root cause

`newIPRateLimiter` (`api/handler.go:324`) spawns the sweep with no lifecycle:

```
go rl.sweepLoop()
```

`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/`.

## 3\. Scenario (how it happens)

1. The node builds an API handler and calls `Mux()` — 3 sweep goroutines start (read, write, probe limiters).
2. `cmd` builds a second handler — 3 more sweeps (6 per process).
3. 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.

## 4\. Proof of concept (local, deterministic)

PoC: `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.

Re-run result at HEAD (2026-11-14), same as V3 (2026-11-11):

| Observation | V3 | Re-run at HEAD |
| --- | --- | --- |
| Goroutines before → after `Mux()` #1 → after `Mux()` #2 | 70 → 73 → 76 | 69 → 73 → 76 |
| After 5 s settle | still 76 | still 76 |
| Race detector | 0 `DATA RACE` | 0 `DATA RACE` |

Note: 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.

Log: `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`).

## 5\. Attacker prerequisites

- **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.

## 6\. Independent validation and false-positive assessment

- **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.
- **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.
- **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`.

## 7\. Severity justification (Minor, ceiling 650 XE)

CVSS 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.

Why not higher: the per-instance cost is negligible (3 goroutines); it needs in-process handler rebuilds to accumulate to anything noticeable.

## 8\. Fix suggestion

Give `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.

## 9\. Caveats (kept in the report)

1. Per-instance leak is tiny (3 goroutines \+ 3 tickers per `Mux()`); accumulation needs in-process handler rebuilds.
2. PR #410 and PR #413 fix the adjacent 0/0 map-capacity defect (xe#172/#396), not this defect — and both are open/unmerged.
3. NOVEL means no matching *public* finding as of the 2026-11-14 \~17:00 WIB check (DB 2,421 entries); private reports may exist.

## 10\. Reproduction (for the committee)

Local only, worktree copy at `89a542e`, isolated temp data dirs:

```
export PATH=~/workspace/go/go/bin:$PATH
export TMPDIR=~/workspace/xe-bounty/.tmp
cd <worktree>/api
go test -race -count=1 -timeout 5m -run 'TestT39a_RateLimiterSweeps$' -v ./...
# Expected: PASS (exit 0) — read the goroutine counts from the log:
# 69 -> 73 -> 76, still 76 after a 5 s settle.
```

PoC: `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`.

### Thread

**Joseph Denne**team2026-11-20 19:46 UTCgithub

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.
