{"id":"XE-13397","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"559","source_url":"https://github.com/xeprotocol/xe/issues/559","reporter_id":2381,"title":"mDNS discovery service leaks 7 goroutines per Node.Stop() (production default has mDNS on)","area":"p2p-sync","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.133935+00:00","updated_at":"2026-11-20T19:47:47.641417+00:00","source_created_at":"2026-11-14T11:49:16+00:00","told_at":"2026-11-20T19:46:19+00:00","status_changed_at":"2026-11-20T19:45:44.651336+00:00","url":"https://bounty.xe.network/XE-13397","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\nWith mDNS enabled — the production default — every `Node.Stop()` leaks **7 goroutines** (libp2p mDNS + zeroconf + socket pollers) plus their UDP sockets, for process lifetime. Measured deterministically at HEAD: after Stop + 15 s settle, goroutines **2 → 9 (+7)**: `p2p/discovery/mdns` +1, `libp2p/zeroconf/v2` +2, `internal/poll.runtime_pollWait` +4. In-process Stop→Start cycles accumulate +7 per cycle.\n\nThe impact is bounded: a single-run production process exits cleanly and the OS reclaims everything, so the leak bites embedders, test harnesses, and repeated in-process restarts. No remote trigger, no funds, no state impact. That bound is why this is Minor.\n\n## 2. Root cause\n\n`xenet.SetupDiscovery` (`net/host.go:166-171`) starts the mDNS service and throws the handle away:\n\n```go\nsvc := mdns.NewMdnsService(h, mdnsServiceTag, n)\nreturn svc.Start()\n```\n\nNo `Close()` call site for the service exists anywhere in the repo. `Node.Stop()` (`node/node.go:1301-1308`) closes the libp2p host, but that does not stop the mDNS service — the service is library-owned, started without retaining a handle, and no shutdown path reaches it.\n\n## 3. Scenario (how it happens)\n\n1. A node runs with mDNS enabled (the production default, `DisableMDNS=false`).\n2. The node is stopped (`Node.Stop()`), e.g. during a restart, an upgrade, or a test-harness cycle. `Stop()` closes the host; the mDNS service keeps running.\n3. After Stop + 15 s, 7 goroutines survive: 1 mDNS, 2 zeroconf, 4 socket pollers — plus the UDP sockets they hold. The next in-process Start→Stop cycle adds 7 more. The control (T3-6, mDNS + discovery disabled) returns exactly to baseline after Stop + 15 s on both MemStore and Badger — the leak is mDNS-specific, and the DHT internals the lifecycle map predicted would survive do NOT (they exit on `Host.Close()`).\n\n## 4. Proof of concept (local, deterministic)\n\nPoC: `hunt-v3/stage3/tests/` — `TestT36b_MDNSEnabled` (`node/`, `-race`, 15.10 s), re-run byte-identical at HEAD in an isolated worktree copy with temp data dirs. The test only logs `LEAK-SIGNAL`; the verdict is read from the numbers.\n\nRe-run result at HEAD (2026-11-14), identical to V3 (2026-11-11):\n\n| Observation | V3 | Re-run at HEAD |\n|---|---|---|\n| Goroutines before → after Stop + 15 s | 2 → 9 (+7) | 2 → 9 (+7) |\n| Breakdown | mdns +1, zeroconf +2, pollWait +4 | mdns +1, zeroconf +2, pollWait +4 |\n| Stop latency | — | 8.13 ms |\n| Control (mDNS off) | baseline | baseline (V3 control) |\n| Race detector | 0 `DATA RACE` | 0 `DATA RACE` |\n\nLog: `hunt-v4-followup/stage3-minors/logs/t36b-reverify.log`. The test files were copied byte-identical (`cmp` clean; sha256 `430e05fd525fe5d7188bed468589b4b174661b1f1ef9537ac9b22e529b5b7af8` for `node/stage3_lifecycle2_test.go`).\n\n## 5. Attacker prerequisites\n\n- **None — local only, no attacker.** The trigger is a local `Stop()` with mDNS on (the default). No remote step, no privilege on the victim. Accumulation needs repeated in-process Stop→Start cycles.\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 exact same count and the exact same goroutine breakdown. The survivors are named from `runtime.Stack`, not inferred.\n- **Disproof attempted:** the lifecycle map predicted DHT internals would also survive — disproved by the T3-6 control (mDNS off returns exactly to baseline); mDNS confirmed by the T3-6b delta. The leak is mDNS-specific.\n- **Duplicate check (fresh, DB 2,421 entries as of 2026-11-14 ~17:00 WIB):** the only mDNS DB hit is xe#434 — an *ingress* flood (`discoveryNotifee` without rate-limit: every peer found via mDNS is immediately dialled), a different function (`HandlePeerFound`) and the opposite direction from this *lifecycle* defect. All other goroutine-leak reports (xe#330/XE-3444 tunnel proxy loops, XE-5430 SetupCertSync churn, net/sync and gossip ctx reports) name different functions. Open PR #207/#239 (body + files list read) counts outbound peers against inbound sybil discovery suppression in `net/discovery.go`; it does not touch `SetupDiscovery` or any mDNS handle. Classification: **NOVEL on public data** (private Critical/Severe reports may exist; uniqueness is not guaranteed). Full analysis: `duplicate-intel/reports/v4f-t36b-mdns-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 (a local Stop); complexity is Low (every Stop with mDNS on leaks); no privileges or user interaction; availability impact is Low (7 goroutines + sockets per Stop, accumulating only across in-process restarts; 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 small bounded lifecycle leak with no remote trigger and no production single-run impact — as Minor, and the CVSS number is informational here.\n\nWhy not higher: a single-run production process is unaffected beyond its own exit; the leak needs in-process restarts to accumulate.\n\n## 8. Fix suggestion\n\nRetain the mDNS service handle from `SetupDiscovery` (return it alongside the error, or store it on the host/node) and call `Close()` on it from `Node.Stop()` (or from `Host.Close()` if libp2p owns the service lifetime). One retained handle + one close call closes the finding; no other call site needs to change.\n\n## 9. Caveats (kept in the report)\n\n1. Impact is bounded to embedders / test harnesses / repeated in-process restarts; a single-run production process exits cleanly.\n2. Production default has mDNS on, so the leak occurs on every normal Stop — but only accumulates across in-process cycles.\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>/node\ngo test -race -count=1 -timeout 5m -run 'TestT36b_MDNSEnabled$' -v ./...\n# Expected: PASS (exit 0) — read the LEAK-SIGNAL numbers from the log:\n# goroutines 2 -> 9 after Stop + 15 s (mdns +1, zeroconf +2, pollWait +4).\n```\n\nPoC: `hunt-v3/stage3/tests/` (`node/stage3_lifecycle2_test.go`). Log: `hunt-v4-followup/stage3-minors/logs/t36b-reverify.log`. Full analysis: `hunt-v3/stage3/STAGE3_VERDICT.md` §T3-6b; duplicate analysis: `duplicate-intel/reports/v4f-t36b-mdns-duplicate-analysis.md`.","patch_refs":[],"thread":[{"id":15198,"source":"github","source_ref":"7049040678","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:19+00:00"}]}