<!-- XE-12831 · Failed node.New() leaks the libp2p listener, the Badger directory lock, and ~38 goroutines; retry fails on the leaked lock · testnet -->
*Markdown view of <https://bounty.xe.network/XE-12831>. The page is the record; this is the same page for reading.*

XE-12831 · Bounty

# Failed node.New() leaks the libp2p listener, the Badger directory lock, and \~38 goroutines; retry fails on the leaked lock

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

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Minor

Area
:   Node / API

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-20 19:45 UTC

Told
:   2026-11-20 19:45 UTC

### Patches

None linked.

### History

- [626356](https://bounty.xe.network/log/626356) 2026-11-20 19:45 UTC proposal.adopted
- [626355](https://bounty.xe.network/log/626355) 2026-11-20 19:45 UTC reply.sent
- [626354](https://bounty.xe.network/log/626354) 2026-11-20 19:45 UTC ticket.status new → closed
- [622556](https://bounty.xe.network/log/622556) 2026-11-20 19:45 UTC proposal.adopted
- [622551](https://bounty.xe.network/log/622551) 2026-11-20 19:45 UTC proposal.adopted
- [622550](https://bounty.xe.network/log/622550) 2026-11-20 19:45 UTC ticket.facts
- [622547](https://bounty.xe.network/log/622547) 2026-11-20 19:45 UTC proposal.adopted
- [622546](https://bounty.xe.network/log/622546) 2026-11-20 19:45 UTC ticket.facts
- [608840](https://bounty.xe.network/log/608840) 2026-11-20 15:02 UTC note.added
- [608839](https://bounty.xe.network/log/608839) 2026-11-20 15:02 UTC proposal.created
- [608838](https://bounty.xe.network/log/608838) 2026-11-20 15:02 UTC proposal.created
- [608837](https://bounty.xe.network/log/608837) 2026-11-20 15:02 UTC proposal.created

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

### Report

# \[Minor\] Failed node.New() leaks the libp2p listener, the Badger directory lock, and \~38 goroutines; retry fails on the leaked lock

**Area:** node-api / node lifecycle
**Repo pin:** `xeprotocol/xe` @ `89a542e` (HEAD, verified 2026-11-14)
**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)
**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)

## Summary

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

## Root cause

Every error path in `New()` (`node/node.go`) calls `cancel()` only and returns `nil, err` — none of them closes the host or the store:

- `: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.
- 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).
- `cancel()` alone does not stop the leaked resources: libp2p host goroutines and the Badger lock are not owned by the context.

## Reproduction (our own Go test, `node/zz_r56_m1_newfail_b56_test.go`)

Trigger (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:

| Observation | Result |
| --- | --- |
| Goroutines before `New()` | 2 |
| Goroutines after failed `New()` | **40 (\+38)** |
| Re-bind the node's TCP port after failure | **fails: `bind: address already in use`** (listener leaked) |
| Re-open the Badger store after failure | **fails: `Cannot acquire directory lock`** (dir lock leaked) |
| Retry `New()` on the same data dir | **fails on the leaked lock** (`store: Cannot acquire directory lock ...`) instead of reaching the provider check |
| Goroutines after the retry | 68 — the retry built a second host before failing on the lock |
| Goroutine stacks mentioning go-libp2p after retry | 43 of 68 |

Reproduced twice (identical counts: 2 → 40, \+38). The test asserts each of the four legs (goroutine delta, listener, dir lock, retry failure) independently.

## Impact / worst case

- **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.
- No remote trigger, no crash, no state corruption, no funds: a resource leak on a local error path. Hence Minor.

## Duplicate check (re-verified at filing time, 2026-11-14 \~12:52–13:0x UTC)

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

## Remediation suggestion

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

### Thread

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

Thanks. This is the same finding as an earlier report that is not public, 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-12831 and says what differs; it is read like any other.
