{"id":"XE-12831","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":827,"title":"Failed node.New() leaks the libp2p listener, the Badger directory lock, and ~38 goroutines; retry fails on the leaked lock","area":"node-api","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"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:11.022411+00:00","updated_at":"2026-11-20T19:45:57.213873+00:00","source_created_at":null,"told_at":"2026-11-20T19:45:57.227877+00:00","status_changed_at":"2026-11-20T19:45:57.218678+00:00","url":"https://bounty.xe.network/XE-12831","body":"# [Minor] Failed node.New() leaks the libp2p listener, the Badger directory lock, and ~38 goroutines; retry fails on the leaked lock\n\n**Area:** node-api / node lifecycle\n**Repo pin:** `xeprotocol/xe` @ `89a542e` (HEAD, verified 2026-11-14)\n**Suggested tier:** Minor (CVSS 2.9 — 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#558` (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\nWhen `node.New()` fails after the host and store exist, it leaks everything it built: the libp2p listener stays bound, the Badger directory lock stays held, and ~38 goroutines keep running — and the caller has no handle to clean any of it up, because `New()` returns `nil` and no `Node.Close` exists (only `Stop()` on a `*Node`). A retry of `New()` on the same data dir then fails on the leaked Badger lock instead of reaching its real error, and each retry leaks another host's worth of goroutines.\n\n## Root cause\n\nEvery error path in `New()` (`node/node.go`) calls `cancel()` only and returns `nil, err` — none of them closes the host or the store:\n\n- `:159` genesis load, `:170` host, `:176` pubsub, `:182` gossip, `:188` vote gossip, `:207` mDNS, `:230-233` store, `:519-522` provider timekeepers check, `:529-532` provider `/dev/kvm` check — all `cancel()` + return.\n- No `Host.Close()` / `store.Close()` appears on any error return in `New()`, and no `Node.Close` method exists anywhere in the repo (only `Stop()` at `:1301-1308`, which requires the `*Node` that a failed `New()` never returns).\n- `cancel()` alone does not stop the leaked resources: libp2p host goroutines and the Badger lock are not owned by the context.\n\n## Reproduction (our own Go test, `node/zz_r56_m1_newfail_b56_test.go`)\n\nTrigger (chosen from the code): provider mode on a fresh data dir. `New()` builds the host, pubsub, gossip, keypair, Badger store, ledger and statechain first, then fails in the provider-mode block — on this host, the `/dev/kvm` check (`provider mode requires /dev/kvm access`). Measured at HEAD, 2026-11-14:\n\n| Observation | Result |\n|---|---|\n| Goroutines before `New()` | 2 |\n| Goroutines after failed `New()` | **40 (+38)** |\n| Re-bind the node's TCP port after failure | **fails: `bind: address already in use`** (listener leaked) |\n| Re-open the Badger store after failure | **fails: `Cannot acquire directory lock`** (dir lock leaked) |\n| Retry `New()` on the same data dir | **fails on the leaked lock** (`store: Cannot acquire directory lock ...`) instead of reaching the provider check |\n| Goroutines after the retry | 68 — the retry built a second host before failing on the lock |\n| Goroutine stacks mentioning go-libp2p after retry | 43 of 68 |\n\nReproduced twice (identical counts: 2 → 40, +38). The test asserts each of the four legs (goroutine delta, listener, dir lock, retry failure) independently.\n\n## Impact / worst case\n\n- **Honestly bounded:** a single-run production `xe node` process that fails to start exits, and the OS reclaims the listener, the lock and the goroutines. The impact is on embedders, test harnesses, and any supervisor that constructs nodes in-process and retries after a failure: the leaked listener holds the port (a retry on the same port cannot bind), the leaked Badger lock makes the data dir unusable for the rest of the process lifetime, and each retry leaks ~38 more goroutines plus another listener.\n- No remote trigger, no crash, no state corruption, no funds: a resource leak on a local error 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=\"directory lock\"` → XE-0041, XE-1838 only. XE-0041 (accepted Medium — SIGTERM during sync does not cancel in-flight sync; shutdown hangs, keeps the Badger lock) is a **shutdown** hang on a running node whose sync context derives from `context.Background()` — a different trigger and a different code path from a **constructor** failure. XE-1838 (triaging Minor — concurrent double-start swaps the persisted identity) is an unlocked key-file check-then-act race — a different mechanism entirely. Both read in full.\n- `q=\"Node.Stop\"` → XE-0041, XE-0920 only. XE-0920 (triaging Medium — queued VM provision blocks shutdown, Lima `time.After` not ctx) is a shutdown stall on the provision path — a different function and phase. Read in full.\n- `q=\"node.New\"` → 14 hits; closest is XE-7771 (new Medium — startup-window nil-closure panic in `Node.New`, sync goes live before ledger closures are assigned), a **crash** in the constructor's startup window, not a resource leak on an error return. Read in full. No board ticket names a failed-`New()` resource leak.\n- `q=aurorabelle788` → 1 hit, XE-12805 (our own earlier filing, which cites the author's #557) — the #558 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\nClose what `New()` built on every error return — e.g. a deferred cleanup in `New()` that closes the host and the store (in reverse construction order) unless construction succeeded, or named error returns with a single cleanup site. A `Node.Close` for the failed-construction case (or returning the partial node alongside the error) would also let callers clean up, but the constructor-owned cleanup is the robust fix.","patch_refs":[],"thread":[{"id":15373,"source":"desk","source_ref":"reply:13264","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, 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-12831 and says what differs; it is read like any other.","created_at":"2026-11-20T19:45:57.213873+00:00"}]}