{"id":"XE-12833","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":827,"title":"mDNS discovery service leaks ~8 goroutines per Node.Stop(); the MdnsService handle is discarded and never closed","area":"p2p-sync","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-13397","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:00:37.890401+00:00","updated_at":"2026-11-20T19:45:56.663320+00:00","source_created_at":null,"told_at":"2026-11-20T19:45:56.709052+00:00","status_changed_at":"2026-11-20T19:45:56.672471+00:00","url":"https://bounty.xe.network/XE-12833","body":"# [Minor] mDNS discovery service leaks ~8 goroutines per Node.Stop(); the MdnsService handle is discarded and never closed\n\n**Area:** p2p-sync / discovery\n**Repo pin:** `xeprotocol/xe` @ `89a542e` (HEAD, verified 2026-11-14)\n**Suggested tier:** Minor (CVSS 4.0 — local only, no remote trigger; embedders / test harnesses / repeated in-process restarts only; a single-run production process exits cleanly)\n**Related public issue:** `xeprotocol/xe#559` (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 `Node.Stop()` with mDNS enabled (the production default) leaks ~8 goroutines. `SetupDiscovery` starts the libp2p mDNS service and throws the handle away, so nothing ever calls `Close()` on it — and `Host.Close()` in `Stop()` does not stop it either, because the service owns its own multicast socket, not the swarm. The leaked goroutines (zeroconf browse/server loops) run forever, so an embedder or test harness that restarts nodes in-process accumulates 8 goroutines per restart with no way to reclaim them.\n\n## Root cause\n\n- `net/host.go:166-171` — `SetupDiscovery` creates the service in a local and returns only the `Start()` error:\n  ```go\n  svc := mdns.NewMdnsService(h, mdnsServiceTag, n)\n  return svc.Start()\n  ```\n  The handle is discarded at the call site (`node/node.go:207`); `MdnsService` has exactly **one** mention in non-test code in the whole repo, and **no `Close()` call site exists anywhere**.\n- `node/node.go:1301-1308` — `Stop()` cancels the context, waits on the WaitGroup, closes the vote manager, the host and the store — but never touches the mDNS service, and the service's goroutines do not observe the node context or the host lifecycle.\n\n## Reproduction (our own Go test, `node/zz_r56_m2_mdns_b56_test.go`)\n\nThree full `New()` + `Stop()` cycles with mDNS on (production default) and fresh data dirs. Measured at HEAD, 2026-11-14 (two runs, identical counts):\n\n| Cycle | Goroutines after `Stop()` | Delta vs baseline |\n|---|---|---|\n| baseline | 2 | — |\n| 1 | 10 | +8 |\n| 2 | 18 | +16 |\n| 3 | 26 | **+24 (+8.0/cycle, monotonic)** |\n\nAttribution from the full goroutine dump after cycle 3 (26 goroutines: 2 test harness + 24 leaked): **all 24 leaked stacks belong to the mDNS service tree** — 18 pure `libp2p/zeroconf` stacks, 3 pure `discovery/mdns` stacks, 3 stacks spanning both. Representative leaked stack: `zeroconf.(*client).periodicQuery` ← `(*client).run` ← `zeroconf.Browse` ← `mdnsService.startResolver`. No other package accounts for any of the leaked goroutines — the growth is exactly the undisposed mDNS service, 8 goroutines per `Stop()`.\n\n## Impact / worst case\n\n- **Honestly bounded:** a single-run production `xe node` exits cleanly and the OS reclaims the goroutines. The impact is on embedders and test harnesses that create and stop nodes repeatedly in one process: 8 immortal goroutines per restart (plus the multicast sockets they hold), growing without bound across restarts, with no API to stop them.\n- No remote trigger, no crash, no state corruption, no funds: a lifecycle leak on the shutdown path. Hence Minor.\n\n## Duplicate check (re-verified at filing time, 2026-11-14 ~12:52–13:0x UTC)\n\n- Board search `q=mDNS` → 15 hits, none a lifecycle/`Stop()` leak. Closest are XE-7925 (new Medium — `--no-discovery` does not isolate a node, DHT bootstrap dials unconfigured public peers; a flag-semantics/isolation defect, read in full) and XE-3098 (new Minor — docs say there is no ambient peer discovery). The other 13 are sync, CLI, docs, or SSH tickets that merely mention discovery.\n- `q=zeroconf` → **0 hits**. `q=SetupDiscovery` → **0 hits**. A corpus title scan for mDNS/zeroconf titles finds no lifecycle-leak ticket.\n- `q=aurorabelle788` → 1 hit, XE-12805 (our own earlier filing) — the #559 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\nKeep the `MdnsService` handle (return it from `SetupDiscovery` or store it on the `Node`) and call `Close()` on it in `Node.Stop()` before closing the host. Alternatively, tie the service's lifetime to the node context so `cancel()` in `Stop()` retires its goroutines.","patch_refs":[],"thread":[{"id":15366,"source":"desk","source_ref":"reply:13257","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-13397, 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-12833 and says what differs; it is read like any other.","created_at":"2026-11-20T19:45:56.663320+00:00"}]}