Skip to content

XHTTP: Release cached clients on outbound removal - #7118

Closed
yiguodev wants to merge 2 commits into
XTLS:mainfrom
YuanDevTeam:fix/xhttp-outbound-cleanup
Closed

yiguodev wants to merge 2 commits into
XTLS:mainfrom
YuanDevTeam:fix/xhttp-outbound-cleanup

Conversation

@yiguodev

@yiguodev yiguodev commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Repeatedly adding, dialing, and removing XHTTP outbounds retains one global dialer-cache entry per fresh stream configuration. Calling Handler.Close() also leaves those cached clients alive.

Close the removed handler and its owned transport state, including independent download settings and every cached destination. Mark configurations closed before cleanup so concurrent acquisition cannot recreate their cache entries. Run cleanup outside manager/cache locks.

RemoveHandler(tag)
  unregister the handler under the manager lock
  release the manager lock
+ close the removed handler exactly once
+   close its stream configuration
+     detach owned XHTTP cache entries
+     cancel requests and close current/retired clients and connections

Track raw TCP connections before TLS/REALITY wrapping, preserve the existing caller-cancellation semantics, and reserve XMUX usage before releasing the selection lock. H1 uploads consume their responses before returning connections to the pool, so ordinary XMUX retirement waits for outstanding uploads; concurrent uploads remain supported. Invalid responses and Connection: close connections are discarded. Partially successful stream-up dialing closes the already-open download on failure.

Evidence

  • Before: regression tests expose missing handler/config cleanup and active requests surviving client close. The H1 slow-response rotation test also fails when an upload lease ends at the write.
    After: 13 focused lifecycle tests cover removal and same-tag replacement outside the manager lock, handler cleanup, config isolation and separate downloads, concurrent acquisition/close, retired active clients, late dialing/pool returns, real TLS H1/H2 cancellation, slow-response H1/H2 rotation, HTTP1 connection-close handling, and partial-dial cleanup.
  • Before: 1000 fresh XHTTP nodes leave the cache at 1005 entries, including 5 warmup nodes, even after removal and explicit handler close.
    After: all 240 samples across 16 Linux/arm64 runs have zero retained cache entries; all 12,060 HEAD requests, including warmup, succeed. After idle, each run returns to its baseline FD/goroutine counts with zero socket FDs.
Scenario Before: idle heap increase After: idle heap increase
stream-one / Remove 2.826–2.828 MiB 0.053–0.058 MiB
stream-one / Remove + Close 2.866–2.897 MiB 0.050–0.053 MiB
packet-up / Remove 3.075–3.077 MiB 0.047–0.052 MiB
packet-up / Remove + Close 3.072–3.075 MiB 0.048–0.052 MiB

Each measured run adds 1000 nodes in 200 batches of at most 5, after a 5-node warmup; each scenario repeats twice. Measurements use the same loopback VLESS/XHTTP HTTP/1.1 backend, 2 CPU / 2 GiB Linux container, Go 1.27.2, and two GCs after 10 seconds idle. The matrix also includes direct and no-dial controls. RSS is recorded separately and is not treated as retained cache memory.

Passed:

go run ./infra/vformat/main.go -mode check -pwd ./
go test ./app/proxyman/outbound ./transport/internet ./transport/internet/splithttp ./core ./app/proxyman/command -count=1 -timeout=180s
go test ./... -run '^$' -count=1 -timeout=120s # compile every package
go test -race ./transport/internet/splithttp -count=1 -timeout=120s
go test -race ./app/proxyman/outbound -run '^(TestRemoveHandlerCloseMayReplaceTag|TestManagerCloseClearsHandlersBeforeCallbacks|TestHandlerCloseOwnsStreamConfigAndRetainsErrors|TestConcurrentSelectAndHandlerRemoval)$' -count=1 -timeout=90s

libXray's full test suite also passes with a temporary module replacement pointing to this Core. go vet still reports the same eight pre-existing protobuf XmuxConfig copy-lock warnings found in the unmodified baseline.

Merge Danger

Door: two-way; code-only revert, without configuration or data migration.

Blast Radius: networking

Removed handlers and stream configurations are terminal: callers must construct new handlers to register them again. H1 no longer pipelines requests on the same connection before the preceding response completes; high-latency WAN throughput was not measured. Active HTTP/3 teardown and stalled uTLS/REALITY handshakes were not exercised end to end. Browser Dialer's browser-side cancellation and device builds are outside this validation.

Copilot AI balanced review requested due to automatic review settings October 10, 2026 01:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@yiguodev

Copy link
Copy Markdown
Collaborator Author

修改说明(中文)

反复新增、拨号和删除 XHTTP outbound 时,每个新建的流配置都会在全局拨号缓存中留下一个条目。即使额外调用 Handler.Close(),关联的缓存客户端也没有被释放。

本次修改让节点删除同时关闭 handler 及其拥有的传输资源,包括独立下载配置和所有目标地址的缓存。清理前先将配置标记为已关闭,避免并发获取重新创建缓存;实际关闭操作在管理器锁和缓存锁外执行。

RemoveHandler(tag)
  在管理器锁内注销对应 handler
  释放管理器锁
+ 关闭该 handler,且只执行一次
+   关闭其流配置
+     摘除关联的 XHTTP 缓存
+     取消请求,关闭当前及已退休的客户端和连接

底层 TCP 连接在 TLS/REALITY 封装前登记,保留已有的调用方取消语义。XMUX 在释放选择锁前预留请求计数,普通轮换等待活动请求完成,删除节点则关闭全部关联客户端。迟完成的拨号和关闭后归还的连接不能重新入池。

H1 上传会读取、排空并关闭响应后再归还连接,使请求计数覆盖完整响应过程。多个上传仍可并发;无效响应和 Connection: close 连接会被丢弃。stream-up 部分拨号成功后失败时,会关闭已经打开的下载端。

验证结果

  • 修复前: 回归测试暴露了 handler/配置未清理、关闭客户端后请求仍存活的问题;H1 慢响应轮换测试也能复现上传计数在写入完成时过早释放。
    修复后: 当前保留 13 项关键生命周期回归测试,覆盖锁外关闭和同名替换、handler 清理、配置隔离和独立下载、并发获取与关闭、已退休但仍活动的客户端、迟完成拨号及连接归还、实际 TLS/H1/H2 请求取消、H1/H2 慢响应轮换、HTTP1 Connection: close 处理及部分拨号失败清理。
  • 修复前: 新增 1000 个 XHTTP 节点后,删除节点并额外关闭 handler,缓存仍有 1005 项,其中 5 项来自预热。
    修复后: 16 组 Linux/arm64 实验的全部 240 个采样点缓存均为 0;包含预热的 12,060 次 HEAD 请求全部成功。空闲后 FD 和 goroutine 恢复到各次运行的基线,socket FD 为 0。
场景 修复前空闲后存活堆增量 修复后空闲后存活堆增量
stream-one / Remove 2.826–2.828 MiB 0.053–0.058 MiB
stream-one / Remove + Close 2.866–2.897 MiB 0.050–0.053 MiB
packet-up / Remove 3.075–3.077 MiB 0.047–0.052 MiB
packet-up / Remove + Close 3.072–3.075 MiB 0.048–0.052 MiB

每次测量先预热 5 个节点,再执行 200 批、每批最多 5 个节点,共新增 1000 个节点;各场景独立重复两次。修复前后使用相同的 loopback VLESS/XHTTP HTTP/1.1 后端、2 CPU / 2 GiB Linux 容器、Go 1.27.2,并在空闲 10 秒后执行两次 GC。实验还包含直连和仅新增/删除对照。RSS 单独记录,不作为缓存残留或存活 Go 堆的等价指标。

以下检查已通过:

go run ./infra/vformat/main.go -mode check -pwd ./
go test ./app/proxyman/outbound ./transport/internet ./transport/internet/splithttp ./core ./app/proxyman/command -count=1 -timeout=180s
go test ./... -run '^$' -count=1 -timeout=120s # 全部包编译检查
go test -race ./transport/internet/splithttp -count=1 -timeout=120s
go test -race ./app/proxyman/outbound -run '^(TestRemoveHandlerCloseMayReplaceTag|TestManagerCloseClearsHandlersBeforeCallbacks|TestHandlerCloseOwnsStreamConfigAndRetainsErrors|TestConcurrentSelectAndHandlerRemoval)$' -count=1 -timeout=90s

使用临时 module replacement 指向本次 Core 修改后,libXray 的完整测试也通过。go vet 仍报告原始基线中已有的 8 项 protobuf XmuxConfig 复制锁告警。

兼容性与验证边界

本次修改可通过代码回退撤销,无需配置或数据迁移;影响范围为网络连接生命周期。

被删除的 handler 和已关闭的流配置不能再次使用,重新注册时需要构造新对象。H1 不再在上一响应完成前通过同一 TCP 连接流水线发送下一请求;公网高延迟环境下的吞吐表现未测量。活动 HTTP/3 请求关闭、挂起的 uTLS/REALITY 握手没有端到端验证;Browser Dialer 浏览器端的请求取消和设备构建也不在本次验证范围内。

@RPRX

RPRX commented Oct 10, 2026

Copy link
Copy Markdown
Member

十个文件,感觉有点魔改,其实目前 core 的重点不包含 outbound 动态增减吧,如果是 inbound 还行

@yiguodev

Copy link
Copy Markdown
Collaborator Author

目前在给 libXray 增加运行时测速功能,这个不解决的话就只能放弃这个 feature 了。

@RPRX

RPRX commented Oct 10, 2026

Copy link
Copy Markdown
Member

只是感觉这样有点堆屎山了,不如先重构 core 的底层架构

@yiguodev

Copy link
Copy Markdown
Collaborator Author

来吧,vless-rs ,我早就想搞了。

@yiguodev yiguodev closed this Oct 10, 2026
@yiguodev
yiguodev deleted the fix/xhttp-outbound-cleanup branch October 10, 2026 02:55
@RPRX

RPRX commented Oct 10, 2026

Copy link
Copy Markdown
Member

我的意思先把 core 底层架构搞清晰些,然后 ctx 多层 with cancel,这样无论是 remove instance/outbound 还是什么的都方便

@yiguodev

Copy link
Copy Markdown
Collaborator Author

这个真搞不了,水平不行啊

@RPRX

RPRX commented Oct 10, 2026

Copy link
Copy Markdown
Member

@4RH1T3CT0R7 同样你有空可以 refactor Xray-core 的底层架构让逻辑更清晰些,包括底层传输那里有点绕,尤其是 UDP 部分

至于 /main/distro/main.go 就直接去掉吧,然后话说 app、features 这俩文件夹能不能合并的,core、main 感觉也能简并下

虽然以上基本不是热点代码,主要增强可读性,加上 #6795 (comment) ,完成后送一个 Project X NFT,@Fangliding 你也看看

@Fangliding

Copy link
Copy Markdown
Member

能跑就别改了吧 这些都不打紧
光是核几个ai发现bug就够麻烦了

@RPRX

RPRX commented Oct 10, 2026

Copy link
Copy Markdown
Member

至少架构更清晰些后要实现这个 PR 的目标,以及单连接统计/控制啥的都更简单些,尤其是删掉那些完全没人用的注册机制后

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.

4 participants