<!-- XE-2446 · api/tunnel: goroutine and file descriptor leak on TCP tunnel disconnect (uncoordinated wg.Wait deadlocks on half-close) · testnet -->
*Markdown view of <https://bounty.xe.network/XE-2446>. The page is the record; this is the same page for reading.*

XE-2446 · Bounty

# api/tunnel: goroutine and file descriptor leak on TCP tunnel disconnect (uncoordinated wg.Wait deadlocks on half-close)

Closed · duplicate
—claimed Medium
[xe#321](https://github.com/xeprotocol/xe/issues/321)
filed 2026-11-09 16:50 UTC
by arturbarszcz78

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Medium

Area
:   Node / API

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-10 12:21 UTC

Told
:   2026-11-10 12:19 UTC

### Patches

- [xe#322](https://github.com/xeprotocol/xe/pull/322) open by arturbarszcz78
  fix(api): unblock TCP tunnel proxy and prevent connection leaks on disconnect (fixes #321)
- [xe#324](https://github.com/xeprotocol/xe/pull/324) open by tipprb
  fix(api): unblock TCP tunnel and close connections on disconnect (#321)

### History

- [80301](https://bounty.xe.network/log/80301) 2026-11-10 12:20 UTC github.state\_pushed
- [80300](https://bounty.xe.network/log/80300) 2026-11-10 12:20 UTC github.labels
- [80240](https://bounty.xe.network/log/80240) 2026-11-10 12:19 UTC reply.mirrored
- [80164](https://bounty.xe.network/log/80164) 2026-11-10 12:19 UTC proposal.adopted
- [80163](https://bounty.xe.network/log/80163) 2026-11-10 12:19 UTC reply.sent
- [80162](https://bounty.xe.network/log/80162) 2026-11-10 12:19 UTC ticket.status new → closed
- [79637](https://bounty.xe.network/log/79637) 2026-11-10 12:19 UTC proposal.adopted
- [79636](https://bounty.xe.network/log/79636) 2026-11-10 12:19 UTC ticket.facts area
- [79635](https://bounty.xe.network/log/79635) 2026-11-10 12:19 UTC proposal.adopted
- [79634](https://bounty.xe.network/log/79634) 2026-11-10 12:19 UTC ticket.facts
- [79633](https://bounty.xe.network/log/79633) 2026-11-10 12:19 UTC proposal.adopted
- [79632](https://bounty.xe.network/log/79632) 2026-11-10 12:19 UTC ticket.facts

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

### Report

### Summary

In `api/tunnel.go:handleTunnelTCP`, the bidirectional proxy between `clientConn` and the VM `tunnel` deadlocks on connection termination. Both `tunnel.Close()` and `clientConn.Close()` are executed after `wg.Wait()`. When either side closes its connection (client disconnects or remote VM exits), the other reading goroutine remains blocked indefinitely on `io.Copy`, so `wg.Wait()` never returns. This permanently leaks two goroutines and both network connections per tunnel session.

### Suggested Tier

Medium (resource exhaustion / denial of service on API node)

### Root Cause

In `api/tunnel.go` (lines 70-89):

```
	var wg sync.WaitGroup
	wg.Add(2)

	go func() {
		defer wg.Done()
		if buffered := buf.Reader.Buffered(); buffered > 0 {
			_, _ = io.CopyN(tunnel, buf, int64(buffered))
		}
		_, _ = io.Copy(tunnel, clientConn)
	}()

	go func() {
		defer wg.Done()
		_, _ = io.Copy(clientConn, tunnel)
	}()

	wg.Wait()
	_ = tunnel.Close()
	_ = clientConn.Close()
```

1. When the remote VM session ends (e.g. SSH session closes or peer stream resets), Goroutine 2 finishes reading from `tunnel` and calls `wg.Done()`. However, `clientConn` is never closed, so Goroutine 1 remains blocked reading from `clientConn`. `wg.Wait()` never unblocks.
2. Conversely, when a client closes their TCP connection, Goroutine 1 finishes reading from `clientConn` and calls `wg.Done()`. However, `tunnel` is never closed, so Goroutine 2 remains blocked reading from `tunnel`. `wg.Wait()` never unblocks.

Because neither goroutine closes the counterpart connection upon exit, every terminated tunnel leaves `handleTunnelTCP` hanging forever.

### Impact

Every TCP tunnel established via `POST /tunnel/{leaseHash}/tcp` leaks two goroutines and two socket descriptors whenever either party disconnects. Over time, active lease consumers opening and closing tunnels exhaust the operating system file descriptor table (`EMFILE: too many open files`) and hang node worker routines, degrading API responsiveness.

### Reproduction

Simulating bidirectional proxying with in-memory connections demonstrates that closing either side fails to unblock the proxy:

```
func proxyCurrent(clientConn, tunnel net.Conn) {
	var wg sync.WaitGroup
	wg.Add(2)
	go func() { defer wg.Done(); _, _ = io.Copy(tunnel, clientConn) }()
	go func() { defer wg.Done(); _, _ = io.Copy(clientConn, tunnel) }()
	wg.Wait()
	_ = tunnel.Close()
	_ = clientConn.Close()
}
// When remote tunnel is closed: proxyCurrent hangs on wg.Wait() because clientConn is never closed.
```

### Suggested Fix

Use `sync.Once` to ensure both `tunnel` and `clientConn` are closed as soon as either copying direction finishes, mirroring standard proxy patterns:

```
	var once sync.Once
	closeAll := func() {
		once.Do(func() {
			_ = tunnel.Close()
			_ = clientConn.Close()
		})
	}
	defer closeAll()

	var wg sync.WaitGroup
	wg.Add(2)

	go func() {
		defer wg.Done()
		defer closeAll()
		if buffered := buf.Reader.Buffered(); buffered > 0 {
			_, _ = io.CopyN(tunnel, buf, int64(buffered))
		}
		_, _ = io.Copy(tunnel, clientConn)
	}()

	go func() {
		defer wg.Done()
		defer closeAll()
		_, _ = io.Copy(clientConn, tunnel)
	}()

	wg.Wait()
```

### Payout Addresses

- Base / EVM USDC: `0x4dE44e40fa09A3ce7F386a292318dD4B609b281E`
- XE Protocol L1: `23bd7ef9c17e79ee76e9f656d618cf0f7c2cda4a700b58d065917d14e97a304a`

### Thread

**Joseph Denne**team2026-11-10 12:19 UTCgithub

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, reply with what differs and we will look again.

**Valaxeth**2026-11-11 15:14 UTCgithub

Hi, re-checking at HEAD 89a542e — the deadlock is still there, in a path the #330 fix doesn't cover: The deadlock reported here is still present at HEAD 89a542e, but in a path #330/#331 do not cover. #330 and fix #331 cover only node/ssh\_gateway.go (handleDirectTCPIP) and net/tunnel.go (SetupTunnelHandler); neither mentions api/tunnel.go. handleTunnelTCP (api/tunnel.go:80-100) is the same pattern: two io.Copy goroutines, wg.Wait(), and tunnel.Close()/clientConn.Close() only after wg.Wait() returns. When either side disconnects, the sibling copy blocks forever on Read() and wg.Wait() never returns — two leaked goroutines \+ two leaked sockets per terminated tunnel. Reproduced live on a scratch test (api/zz\_r7d\_scratch\_test.go, removed after run): client closes its hijacked TCP conn → tunnel end still open after 3 s; remote tunnel end closes → client conn still open after 3 s. go test ./api/ -run TestR7DHTTPTunnel -v PASS (both "DEADLOCK REPRODUCED" log lines). Fix PRs #322 and #324 are still open/unmerged and git log \-- api/tunnel.go shows no commit other than HEAD — the fix never landed. Request: please reopen / cover the api path, or confirm whether #322/#324 are intended to cover it.
