Skip to content

TUN inbound: Half-close the app connection when the outbound is done, instead of waiting for uplinkOnly - #7347

Open
StarProxima wants to merge 2 commits into
XTLS:mainfrom
StarProxima:half-close-on-close
Open

StarProxima wants to merge 2 commits into
XTLS:mainfrom
StarProxima:half-close-on-close

Conversation

@StarProxima

@StarProxima StarProxima commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Why

With Xray's TUN as the system VPN, an app that reads a response until the server closes waits about 1 s more than it should on every such connection. The outbound already knows the response is over, but TUN keeps the app's connection open until the whole dispatch ends, and that happens only after uplinkOnly. Examples are HTTP/1.0 without Content-Length, RTSP (Moonlight in #5845) and simple request and reply protocols. With this change the app gets the end of the response right away

What changed

  • TUN sends FIN to the app as soon as the outbound is done with the response. The app can still send, and the connection is closed as before when the dispatch is over
  • the lightweight TCP stack gets CloseWrite for that
  • the rest of the core still closes links in pairs, as agreed in the thread

Testing and limits

  • TestTCPCloseWrite checks that FIN goes out, that data from the app is still read after it and that Close sends nothing more. go test -race ./proxy/tun passes
  • this helps only when the outbound itself sees the end of the response, for example freedom or VMess. Over VLESS the outbound ends only after uplinkOnly on the server, so the wait there stays

Related #5845 #7345

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

这个 PR 修复了 #5845 吗

@StarProxima

StarProxima commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

这个 PR 修复了 #5845 吗

@RPRX Partly. It removes the 2 s wait for raw, TLS and REALITY without Vision and on the TUN side

the report itself is on XHTTP, which needs its own change, I can do it in a follow-up

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

先全部修完看看代码吧

@StarProxima

Copy link
Copy Markdown
Contributor Author

先全部修完看看代码吧

@RPRX Done, XHTTP, Vision and REALITY are in now and the description lists what each one does

stream-one over HTTP/1.1 and HTTP/2 still waits since net/http can't end the response and keep reading the request, I have a commit for that if you want it

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

有点多了,等 @Fangliding 看一下

@Fangliding

Fangliding commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

我是不赞成这么弄 conn接口硬整这个 之前我已经关掉一个了 那个moonlight远古玩意硬依赖半关闭特性才出问题 现代协议早就不这样了

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

我也觉得弄半关闭有点那啥了,所以这个 PR 里有没有能留下来的部分

@Fangliding

Copy link
Copy Markdown
Member

downlink要是结束了往客户端发半关闭只在tun内部是可以接受的 我不知道之前的有没有

@RPRX

RPRX commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

另一个比较奇怪的事情是 #5845 中有些协议组合是没问题的

@StarProxima 先把这个 PR 改为只留 TUN 支持半关闭吧,正好发版测测

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

@Fangliding 不对等下,“往客户端发半关闭”,然而 core 那些出站只能把“完全关闭”传给 TUN,它咋知道要不要半关闭

@Fangliding

Copy link
Copy Markdown
Member

downlink/uplink两条路一边关了不就是关了 虽然不知道有谁会写(

@Fangliding

Copy link
Copy Markdown
Member

嘛 其实不改也行 直接RST 99%的情况下都没啥问题 现代软件大都不把半关闭特性视作多可靠

@Fangliding

Copy link
Copy Markdown
Member

半关闭特性处理起来贼拉头疼的 go http库最底层甚至都有一些这个workaround

@RPRX

RPRX commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

说错了,不是“只能”而是没人这么写,core 中 link 都是成对关闭的,且

https://github.com/search?q=repo%3AXTLS%2FXray-core+closewrite&type=code

印象深的也就 REALITY 库搞了个到 target 的 closewrite

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

@StarProxima 要不你看看有什么 workaround 能修 #5845 ,毕竟那个 issue 中说有些协议比如 VMess 是正常的,很神奇

@StarProxima StarProxima changed the title Common & TUN inbound: Half-close the client connection when the target closes, instead of waiting for uplinkOnly TUN inbound: Half-close the app connection when the outbound is done, instead of waiting for uplinkOnly Nov 8, 2026
@StarProxima

Copy link
Copy Markdown
Contributor Author

先把这个 PR 改为只留 TUN 支持半关闭吧

@RPRX Done, only the TUN part is left now

as for VMess working in #5845, it sends an empty chunk at the end of the stream so the client knows the reply is over, VLESS has nothing like that

@StarProxima

StarProxima commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Left the uplinkOnly: 0 idea for the reporter in #5845 #5845 (comment)

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

也就是说 VMess、Freedom 这俩 outbound 实现了只调用到 TUN 的 CloseWrite() 而非 Close()?VLESS 的话 Mux 有潜力实现它

@StarProxima

Copy link
Copy Markdown
Contributor Author

也就是说 VMess、Freedom 这俩 outbound 实现了只调用到 TUN 的 CloseWrite() 而非 Close()?

@RPRX No, they all just call Close on the output like VLESS does. TUN turns it into FIN now

the difference is when they know the response is over. Freedom gets EOF from the target, VMess gets the end chunk, VLESS only sees the server close the whole connection after uplinkOnly

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

@StarProxima 给一下代码调用链,我咋记得 writer reader 都是同时关闭的

@StarProxima

Copy link
Copy Markdown
Contributor Author

给一下代码调用链

@RPRX the early one is this task.Close(output) in freedom, it runs right after responseDone and lands in TCPWriter.Close, which sends FIN now

the one you remember is in Handler.Dispatch, both get closed there but only after Process returns, so after uplinkOnly. VMess has the same task.Close

@RPRXX
RPRXX force-pushed the main branch 2 times, most recently from 9ade89d to 5d17b87 Compare November 14, 2026 15:58
@StarProxima

Copy link
Copy Markdown
Contributor Author

@RPRX only the TUN part is left now, as you asked. Anything you'd like changed before it goes in?

@svlavr

svlavr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I tested #7347 on main and with #7364 applied. For ordinary VMess and VLESS, the patch removes the extra client-side wait. With Mux, the application already receives FIN as soon as the logical stream ends.

When aborting a connection with unread incoming data, main sends RST. With #7347, Interrupt falls back to TCPWriter.Close(), so TUN sends FIN first and then RST when the connection is fully closed.

RFC 9293, §3.6.1 recommends sending RST when closing a connection discards unread data. I suggest keeping Close() for graceful FIN and handling Interrupt separately. For an abort with unread data, preserve the existing RST behavior without a preceding FIN and add a regression test for it.

@RPRX

RPRX commented Nov 24, 2026

Copy link
Copy Markdown
Member

说实话都快 2027 年了不是太想处理这种 Half-close 问题

@StarProxima

Copy link
Copy Markdown
Contributor Author

@svlavr good catch, thanks. TCPWriter now has an empty Interrupt, so an abort with unread data gets RST without a FIN first, as on main. Added TestTCPWriterInterrupt for it.

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