api/tunnel: goroutine and file descriptor leak on TCP tunnel disconnect (uncoordinated wg.Wait deadlocks on half-close)
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()
- When the remote VM session ends (e.g. SSH session closes or peer stream resets), Goroutine 2 finishes reading from
tunneland callswg.Done(). However,clientConnis never closed, so Goroutine 1 remains blocked reading fromclientConn.wg.Wait()never unblocks. - Conversely, when a client closes their TCP connection, Goroutine 1 finishes reading from
clientConnand callswg.Done(). However,tunnelis never closed, so Goroutine 2 remains blocked reading fromtunnel.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
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.
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.