Repository navigation
fix(tunnel): 数据通道按 pong 回带的字节偏移确认,迟到的 pong 不再放开背压闸门或提前关闭 - #2297
Merged
Merged
Conversation
pong 只证明对端读到了那个 ping 之前发出的字节。闸门暂停期间每 20ms 补一个 ping, 平台慢时同时挂着一串 ping,原先任何 pong 都把未确认量清零:排在后面的旧 pong 让闸门 形同虚设,本地 FIN 后 finish() 也会在对端还没读完时就 close()。改为每个 ping 带上已发 字节数、按 pong 回带的偏移确认;并且每次超水位都停读,不再只在首次超水位时暂停。 大响应子进程用例改为等平台侧连接关闭再核对字节数,新增慢平台下闸门与收尾的确定性用例。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 24, 2026 20:57
平台关掉 autoPong、对每个 ping 回一个读不出偏移的 pong,模拟自答 pong 却不回带 payload 的中间一跳:闸门必须停在水位附近,不能把整份响应放出去。bun 1.4.3 的 ws 服务端不认 autoPong:false,所以与下面那条一样只在 Node 下跑。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
Author
|
已合并 PR #2297 → df74acf(squash,作者 deepcoldy),关闭 #2295(相关:#1196)。
|
This was referenced Oct 24, 2026
|
🚀 Released in v3.51.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2295(相关:#1196)
结论:不是 CI 负载下单纯慢,数据通道的背压确认有缺陷
不是慢:
systemd-run --user --scope -p CPUQuota=10%只限我自己这个测试进程的 CPU(没有起空转进程),在 CI 同版本的 Bun 1.4.3 下复现了一次:只收到 22,282,240 字节,同样是少了约 2.9MB、卡住不动。产品缺陷(
src/platform/tunnel-client.ts的bridgeDataChannel,确定、可稳定复现):unacked清零,排在后面的旧 pong 就把之后才发出的字节也算成已确认。插桩实测(平台限速 8MB/s,Bun 1.4.3/1.4.4 都一样):finish()在对端只读到 4.6MB 时就close()了,违反了代码注释里「排空后再 close」的约定。!probing为假,不会再暂停,这一轮探测剩下的时间里闸门一直敞着。第 1 条修好之后,这条就暴露出来了:Node 下同样会领先 16MB。复现那次失败的时间线:
unacked被清零、tcp 恢复;end→finish()看到unacked <= 0,同步close()。此时只交出了 22.28MB,剩下约 2.9MB 再也没有进 WebSocket。改成按偏移确认后,同样的时间点
unacked还是 3.8MB,finish()会继续 ping、等对端确认,而不是当场关闭。说明:剩下那 2.9MB 在 Bun 内部具体走的是哪条路,我没有钉死:可能是
end之后才交出、被readyState !== OPEN丢掉,也可能是根本没交出。用普通 socket 单独测 pause/resume/end 的顺序,两个 Bun 版本都正常。我也评估过额外加「等本地 socket 彻底 close 再关」这个兜底,变异下它没有区分力,所以没有加。改了什么
产品(
src/platform/tunnel-client.ts,只动bridgeDataChannel的「本地 → 平台」方向):unacked拆成sent/acked:每个 ping 带上发出时的sent(十进制字节数)作为 payload,pong 原样回带,按回带的偏移推进acked。读不出偏移的 pong(不是我们的 ping 换来的)不算确认。RFC 6455 要求 pong 的应用数据与 ping 相同;Nodews、Bun 1.4.3 / 1.4.4、平台侧的 Nodews服务端(ws 8,默认autoPong)都实测原样回带。测试(
test/platform-tunnel-data-bridge.test.ts):ws服务端 socket 在 Bun 下无效,这正是 test(tunnel): 背压用例在两个 runner 下都无区分力,且 bun 替身里 gate 从未生效(收紧上限救不了) #1196 说的那个缺口)。断言三件事:平台最终收齐;bridge 调close()时平台已经读完全部字节;bridge 领先平台的字节数始终低于响应的 1/3。autoPong,对每个 ping 都回一个读不出偏移的 pong,模拟「自己回 pong、却不回带 payload」的中间一跳。完成条件是平台回满 10 个 ping,或者看到连接关闭;断言平台收到的不到响应的 1/3,也就是闸门仍停在水位附近。它守的是上面「读不出偏移的 pong 不算确认」这条防线。只在 Node 下跑的原因:Bun 1.4.3 的ws服务端不认autoPong: false,仍会按原 payload 自动回 pong(实测),隔离不出这条路径。closed、dashboardDone、dashboardWritten,下次再红能直接看出是丢了还是没关。测试验证
本地只跑这一个文件(并发 1,
TMPDIR=/var/tmp/cf/...),全量交给 CI。没有循环压测,也没有起空转进程。插桩复现都是单次手动跑,CPU 限额只作用在测试进程自己身上。vitest run --project unit test/platform-tunnel-data-bridge.test.ts:12 passed。bun test同文件,Bun 1.4.3(CI 钉的版本,单独解压到私有目录,没有动全局 bun)和 Bun 1.4.4:都是 10 pass / 2 skip / 0 fail(skip 的是两条 Node-only 用例)。tsc --noEmit:通过。变异(在单独的 detached worktree 里做;跑新用例和大响应用例;每组共 5 次):
close()时平台只收到 4,606,976;领先 20.5MB)sent」改回去close()时平台只收到 19,811,328)修复后的实测:Bun 1.4.3 下领先量恒为 4.72MB、Node 下恒为 4.26MB(水位 4MB 加一批读取),
close()时平台都已收齐。影响面
ws8(按 RFC 回带 payload),数据通道经 TLB 落到平台 pod 的 ws 服务端,由它直接回 pong。tunnel-client的测试文件test/tunnel-client-ip-family.test.ts(控制通道拨号)本地没跑,交给 CI。🤖 Generated with Claude Code