Skip to content

test(socks5): let the server's relay recv_start land before first send - #43

Merged
Antruly merged 2 commits into
masterfrom
fix/socks5-udp-relay-race
Oct 20, 2026
Merged

Antruly merged 2 commits into
masterfrom
fix/socks5-udp-relay-race

Conversation

@Antruly

@Antruly Antruly commented Oct 20, 2026

Copy link
Copy Markdown
Owner

症状

test_socks5_udp_func 的 IPv6 段偶发失败,报 ipv6 echo failed (rc=0, got=''),5 秒超时打空。最近在两个平台各命中一次,且都在重跑后通过:

Linux CI 从未命中。

根因

src/net/uvcpp_socks5_server.cpp 的 begin_udp() 里这两步是有窗口的:

send_reply(uvcpp_socks5_reply::SUCCEEDED, relay, false);   // 先回复 BND.ADDR
udp_->recv_start(...);                                     // 后开始收 UDP

客户端拿到 BND.ADDR 之后立刻装好 recv 回调并发出第一条数据报。Linux 上这个 recv_start 在微秒级就落地,回调已武装;macOS 和 MSVC 的线程调度窗口能到几毫秒,数据报抵达时服务端还没开始读,内核直接丢弃,于是永远收不到应答。

服务端回的是控制连接的本地地址(begin_udp 里的注释解释了为什么不能回 ::),这一点没有歧义——回包目标是对的,纯粹是接收端还没就绪。

改动

只改测试,不动库逻辑。在 test_ipv6() 的 recv_start 与第一条 send_to 之间插入一次循环泵加 2 ms 等待,让服务端的 recv_start 先落地。

验证

本地 Linux(Release,默认 net 配置):

  • 修复前连跑 2000 次:2000 通过 / 0 失败(该竞态在 Linux 上窗口太窄,压不出来,与 CI 观察一致)
  • 修复后在 master 库代码上连跑 30 次:30 通过

平台差异需要 macOS 与 MSVC 的 CI 来确认。

begin_udp() sends the BND.ADDR reply before it arms udp_->recv_start(), so a
client that sends as soon as the association completes can put its first
datagram into a socket nobody is reading yet. Linux finishes that arming in
microseconds; on macOS and MSVC the scheduler gap reaches a few ms, which is
enough to drop the datagram and burn the 5s echo timeout.

Pump once and sleep 2ms between recv_start() and the first send_to().
The 2ms wait was a guess and did not survive Windows MSVC: the same case still
failed with got=''. Retrying up to 5 times makes the transient loss harmless,
and printing the echo server's datagram count per attempt distinguishes a
client-to-target loss from a target-to-client loss when it does not recover.
@Antruly
Antruly merged commit 7264201 into master Oct 20, 2026
43 checks passed
@Antruly
Antruly deleted the fix/socks5-udp-relay-race branch October 20, 2026 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant