TokenPLS:报告 mieru UDP 会话建立阶段的互等问题
TokenPLS 向 mieru 报告,在指定提交的 race 下载测试中,服务器数据可能先于开会话响应编号,客户端等待响应才 ACK,服务器又因初始窗口用尽等待 ACK,造成下载停住;TCP 不受影响。客户端修复提案允许收到服务端数据时建立会话,服务端需先安排响应序号的修复尚未编写。原帖给出测试次数与复现范围,不代表所有部署都会发生。
作者原文@TokenPLSSummary
On the UDP transport a download sometimes stops after the server's initial window and never resumes until a read times out. Ours stopped at exactly 20,992 bytes, 16 segments of 1,312 bytes. It happened in 4-6 of 300 runs of an 8 MiB download through a mieru node, with
-race. The TCP transport is unaffected.Root cause
At 1367cf2:
- A client packet session that is still opening sends no ACK until the open session response arrives — https://github.com/enfein/mieru/blob/1367cf2d2dd81c5f27d455be7cea691d7948c420/pkg/protocol/session.go#L946
- The server hands the session to
Acceptas soon as the open session request is delivered to it, before its input loop has handled the request — https://github.com/enfein/mieru/blob/1367cf2d2dd81c5f27d455be7cea691d7948c420/pkg/protocol/underlay_packet.go#L287-L292- When the input loop handles the request,
inputDatafirst moves the request's data to the receive queue, where the application can read it, and only then gives the open session response a sequence number and queues it — https://github.com/enfein/mieru/blob/1367cf2d2dd81c5f27d455be7cea691d7948c420/pkg/protocol/session.go#L1187-L1224If the server's application reads the request and writes back in that window, its data is numbered before the response. The server sends the 16 segments its initial window allows and waits for an ACK. The client waits for the response, which is queued behind data it does not acknowledge.
A server that dials a target before writing usually misses the window. One that answers at once (a fast local target, a cached answer) hits it.
Fix
- Client: a server only sends data in a session it has established, so a client packet session that is still opening can take the server's data as opening the session, as the response does. Only data, not an ACK. This works with servers as deployed. PR #330.
- Server: queue the open session response, or reserve its sequence number, before the request's data reaches the application. This removes the cause; we have not written it.
How this was found
We ship mieru, through mihomo, in Clash, a client for iPhone, iPad, Mac and Apple TV. The stalls showed up in a test that downloads repeatedly through a mieru node on the UDP transport.