Skip to content

Mux: Write frames to the connection from one goroutine - #7345

Closed
StarProxima wants to merge 2 commits into
XTLS:mainfrom
StarProxima:mux-single-writer
Closed

StarProxima wants to merge 2 commits into
XTLS:mainfrom
StarProxima:mux-single-writer

Conversation

@StarProxima

@StarProxima StarProxima commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Follow up to #7337 (comment)

Why

The mux writes to its link from one goroutine per session. Before #5076 a pipe sat under it and only the pipe reader wrote the connection. Now the VLESS inbound hands the mux its connection writer directly, so VisionWriter gets entered from several goroutines at once. Same for the VLESS reverse portal through the inbound

What changed

ServerWorker and ClientWorker put one pipe and one copy goroutine in front of any writer that is not a pipe already, once per connection and not per session. Writers below the mux see one call at a time again, so the VisionWriter mutex from #7337 is removed

Testing and limits

TestServerWorkerWritesOneAtATime and TestClientWorkerWritesOneAtATime count calls that enter the writer while another one is inside, 31 on main and 0 with this change. TestVisionUnderMux runs a real VisionWriter under ServerWorker, without the change the client mux can't parse the stream and drops it
go test -race ./common/mux/... ./proxy/... and testing/scenarios pass

@RPRX

RPRX commented Nov 7, 2026

Copy link
Copy Markdown
Member

#7337 (comment) 就是这个意思,core 中接口的设计都不接受 concurrent write,给 XTLS Vision 这个被调用方加锁治标不治本

不过加个 pipe 感觉有点重了,虽然它基于 chan、可能能保证顺序而不像锁 #6372 (comment) ,不过这里本来就是不同 session 要鸡毛严格顺序,且也不该哪个 session 写得多就长时间占着,所以改成带锁的结构体、实现 WriteMultiBuffer 并在内部加锁吧

代码中 NewServerWorker() 不是直接改 link.Writer?虽然这样更严谨但我追踪了下代码直接改应该也没事,算了还是更严谨吧


截至这个 PR,TUN 和 Mux 的代码都有了挺大的变动,#5845 可能已经被修复,也没人回复了,所以我会先把它标记为已解决

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

@StarProxima 现在有空改吗

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

你没开允许我修改,那我来改吧,会给你 co-author

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.

2 participants