[Bug Report] cross-host envd requests intermittently 502 (connect refused) from a stale eBPF host-port session
This documents the root cause behind the CubeProxy retry in PR #1839; that PR mitigates the symptom at the proxy, this issue is the underlying CubeNet bug.
Summary
In a multi-node cluster, envd requests to a sandbox on another node are proxied
to that node's mapped host port. A fraction of the connect attempts get
connect() failed (111: Connection refused) even though the sandbox is healthy,
because the node-side eBPF host-port forwarding resets the SYN when a stale
session from a since-recycled sandbox still matches.
Reproduction
Two-node cluster, enable_exposed_port: true, exposed_port_list: ["80","49983"].
Drive ~100 sequential envd requests (one at a time) at a sandbox placed on the
other node. ~20% return 502; cube-proxy/error.log:
connect() failed (111: Connection refused) while connecting to upstream,
upstream: "http://<nodeIP>:<hostPort>/files?...", host: "49983-<id>.cube.app"The failures are per-source-port and interleave with successes within ~1.5s.
Root cause (evidence)
Host-port forwarding is eBPF NAT in CubeVS (no iptables DNAT, no userspace proxy):
- Allocation:
Cubelet/network/runtime/resource_allocator.go(host range 20000–29999), installed viaCubelet/network/runtime/tap_lifecycle.goapplyPortMappings→CubeNet/cubevs/port.go(remote_port_mapping/local_port_mappingBPF hash maps). - Inbound TC program
CubeNet/src/nodenic.bpf.c(from_world): before DNAT it looks up a session keyed by{mvm_inner_ip, listen_port, peer_ip, peer_port}(CubeNet/src/session.h). If the session'sgen!=current_gen(vm_ifindex)it deletes the entry andtcp_send_reset(...)— the peer sees RST → 502. mvm_inner_ipis a single node-wide constant andlisten_port= 49983 is exposed by every sandbox, so the session key is shared across all sandboxes on the node; it is unique only per (nginx source IP, ephemeral port).genis bumped on every TAP register / recovery / rollback (CubeNet/.../cubevs_adapter.goatomic.AddUint32; TAP deletion clears the meta socurrent_genreturns 0). TAPs are pooled and reused, so an ifindex quickly carries a new generation.- Port-mapping sessions are only touched for
access_time; they are not retired on FIN/RST and linger (SYN_RECV ~60s, ESTABLISHED ~3h). So whenever nginx's ephemeral source port collides with a lingering session created for a different (now recycled / re-generationed) sandbox on the same tuple, that one SYN is RST'd; a retry from another source port succeeds.
This was introduced with the stale-session reset on rollback
(commit around 2026-08-25). Single-node deployments never hit it: CubeProxy
short-circuits same-host calls straight to SandboxIP:container_port.
Suggested direction (not implemented here)
Scope the port-mapping session key so it cannot collide across sandboxes/TAP
generations on the shared (inner_ip, envd_port) tuple — e.g. include the
sandbox/vm identity or per-mapping generation in the key, or retire the session
on the RST path rather than only resetting. Needs on-hardware validation of the
datapath, so it is filed as an issue rather than a PR.
Workaround / mitigation already sent
PR #1839 adds a
single set_more_tries(1) in balancer_phase.lua, so nginx retries the
connect on a fresh source port and the 502 disappears in practice.
Source: TencentCloud/CubeSandbox