{"id":"XE-2446","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"321","source_url":"https://github.com/xeprotocol/xe/issues/321","reporter_id":571,"title":"api/tunnel: goroutine and file descriptor leak on TCP tunnel disconnect (uncoordinated wg.Wait deadlocks on half-close)","area":"node-api","claimed_tier":"medium","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"closed","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-10T00:12:00.830182+00:00","updated_at":"2026-11-10T12:21:13.271519+00:00","source_created_at":"2026-11-09T16:50:46+00:00","told_at":"2026-11-10T12:19:20+00:00","status_changed_at":"2026-11-10T12:19:16.605715+00:00","url":"https://bounty.xe.network/XE-2446","body":"### Summary\nIn `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.\n\n### Suggested Tier\nMedium (resource exhaustion / denial of service on API node)\n\n### Root Cause\nIn `api/tunnel.go` (lines 70-89):\n```go\n\tvar wg sync.WaitGroup\n\twg.Add(2)\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tif buffered := buf.Reader.Buffered(); buffered > 0 {\n\t\t\t_, _ = io.CopyN(tunnel, buf, int64(buffered))\n\t\t}\n\t\t_, _ = io.Copy(tunnel, clientConn)\n\t}()\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\t_, _ = io.Copy(clientConn, tunnel)\n\t}()\n\n\twg.Wait()\n\t_ = tunnel.Close()\n\t_ = clientConn.Close()\n```\n\n1. 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.\n2. 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.\n\nBecause neither goroutine closes the counterpart connection upon exit, every terminated tunnel leaves `handleTunnelTCP` hanging forever.\n\n### Impact\nEvery 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.\n\n### Reproduction\nSimulating bidirectional proxying with in-memory connections demonstrates that closing either side fails to unblock the proxy:\n```go\nfunc proxyCurrent(clientConn, tunnel net.Conn) {\n\tvar wg sync.WaitGroup\n\twg.Add(2)\n\tgo func() { defer wg.Done(); _, _ = io.Copy(tunnel, clientConn) }()\n\tgo func() { defer wg.Done(); _, _ = io.Copy(clientConn, tunnel) }()\n\twg.Wait()\n\t_ = tunnel.Close()\n\t_ = clientConn.Close()\n}\n// When remote tunnel is closed: proxyCurrent hangs on wg.Wait() because clientConn is never closed.\n```\n\n### Suggested Fix\nUse `sync.Once` to ensure both `tunnel` and `clientConn` are closed as soon as either copying direction finishes, mirroring standard proxy patterns:\n```go\n\tvar once sync.Once\n\tcloseAll := func() {\n\t\tonce.Do(func() {\n\t\t\t_ = tunnel.Close()\n\t\t\t_ = clientConn.Close()\n\t\t})\n\t}\n\tdefer closeAll()\n\n\tvar wg sync.WaitGroup\n\twg.Add(2)\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tdefer closeAll()\n\t\tif buffered := buf.Reader.Buffered(); buffered > 0 {\n\t\t\t_, _ = io.CopyN(tunnel, buf, int64(buffered))\n\t\t}\n\t\t_, _ = io.Copy(tunnel, clientConn)\n\t}()\n\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tdefer closeAll()\n\t\t_, _ = io.Copy(clientConn, tunnel)\n\t}()\n\n\twg.Wait()\n```\n\n### Payout Addresses\n- Base / EVM USDC: `0x4dE44e40fa09A3ce7F386a292318dD4B609b281E`\n- XE Protocol L1: `23bd7ef9c17e79ee76e9f656d618cf0f7c2cda4a700b58d065917d14e97a304a`","patch_refs":[{"id":485,"repo":"xeprotocol/xe","kind":"pr","ref":"322","title":"fix(api): unblock TCP tunnel proxy and prevent connection leaks on disconnect (fixes #321)","url":"https://github.com/xeprotocol/xe/pull/322","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-11-10T00:12:01.832547+00:00"},{"id":487,"repo":"xeprotocol/xe","kind":"pr","ref":"324","title":"fix(api): unblock TCP tunnel and close connections on disconnect (#321)","url":"https://github.com/xeprotocol/xe/pull/324","note":"","state":"open","author":"tipprb","added_by":"desk","created_at":"2026-11-10T00:12:01.832547+00:00"}],"thread":[{"id":2039,"source":"github","source_ref":"6767119516","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, reply with what differs and we will look again.","created_at":"2026-11-10T12:19:20+00:00"},{"id":3125,"source":"github","source_ref":"6799578076","author":"Valaxeth","team":false,"body":"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.","created_at":"2026-11-11T15:14:07+00:00"}]}