Skip to content

VLESS Multiplex (mux.cool): MutexWriter passes Interrupt and Close through - #7355

Closed
Medium1992 wants to merge 1 commit into
XTLS:mainfrom
Medium1992:fix/mux-mutexwriter-interrupt
Closed

Medium1992 wants to merge 1 commit into
XTLS:mainfrom
Medium1992:fix/mux-mutexwriter-interrupt

Conversation

@Medium1992

Copy link
Copy Markdown
Contributor

Since 5745dea common.Interrupt(link.Writer) in the mux client and server workers hits a MutexWriter, which embeds only buf.Writer, so it does nothing: a closed worker leaves its link open, and a write stuck on a peer that stopped reading never returns.
MutexWriter now passes Interrupt and Close to the writer inside, without the lock; the tests fail on v26.11.9 and pass with this.

🤖 Generated with Claude Code

…rough

Since 5745dea the workers' common.Interrupt(link.Writer) reaches a
MutexWriter, which embeds only buf.Writer, and does nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member

想过会不会有这种问题,不过还好吧目前主要就 XDRIVE 在用,等收集更多反馈十天后再更一版

@RPRX

RPRX commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

不对,服务端的话 XUDP 也会触发这个,看看实际影响

Update:并不会触发

@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member

继续补充:VLESS Reverse 也在用

看了下代码,ServerWorker/ClientWorker 的 monitor() 会调用 common.Interrupt(m.link.Writer)

@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member

VLESS 服务端 DispatchLink() 并不会触发问题,monitor() 中 common.Interrupt(m.link.Writer) 由 <-w.done.Wait(): 触发且

func (s *Server) DispatchLink(ctx context.Context, dest net.Destination, link *transport.Link) error {
	if dest.Address != muxCoolAddress {
		return s.dispatcher.DispatchLink(ctx, dest, link)
	}
	worker, err := NewServerWorker(ctx, s.dispatcher, link)
	if err != nil {
		return err
	}
	select {
	case <-ctx.Done():
	case <-worker.done.Wait():
	}
	return nil
}

这里直接 return 回 VLESS 入站,结束,本来如果依赖 common.Interrupt(link.Writer) 来结束连接就很抽象,很多都没这接口

@RPRX

RPRX commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

客户端的是 Mux 生成了一对 pipe 给 VLESS 出站,若由 Mux 发起关闭,虽然关不掉 upLinkWriter 了但能关掉 downlinkReader

func (f *DialingWorkerFactory) Create() (*ClientWorker, error) {
	opts := []pipe.Option{pipe.WithSizeLimit(64 * 1024)}
	uplinkReader, upLinkWriter := pipe.New(opts...)
	downlinkReader, downlinkWriter := pipe.New(opts...)

	c, err := NewClientWorker(transport.Link{
		Reader: downlinkReader,
		Writer: upLinkWriter,
	}, f.Strategy)
	if err != nil {
		return nil, err
	}

	go func(p proxy.Outbound, d internet.Dialer, c common.Closable) {
		outbounds := []*session.Outbound{{
			Target: net.TCPDestination(muxCoolAddress, muxCoolPort),
		}}
		ctx := session.ContextWithOutbounds(context.Background(), outbounds)
		ctx, cancel := context.WithCancel(ctx)

		if errP := p.Process(ctx, &transport.Link{Reader: uplinkReader, Writer: downlinkWriter}, d); errP != nil {
			errC := errors.Cause(errP)
			if !(goerrors.Is(errC, io.EOF) || goerrors.Is(errC, io.ErrClosedPipe) || goerrors.Is(errC, context.Canceled)) {
				errors.LogInfoInner(ctx, errP, "failed to handler mux client connection")
			}
		}
		common.Must(c.Close())
		cancel()
	}(f.Proxy, f.Dialer, c.done)

	return c, nil
}

这里确实依赖 Interrupt 了,不过 pipe 都有,关掉任一应该都会导致 VLESS 出站的双向 copy 结束,所以同样并不会触发问题

Update:后半句可能不对

@Medium1992

Copy link
Copy Markdown
Contributor Author

Server side: agreed, closing the inbound connection unblocks it.

Client side does leak, though. If the connection drops during an upload, the session goroutines are blocked writing into the full 64 KiB uplink pipe. Nobody reads uplinkReader after Process returns, and common.Interrupt(upLinkWriter) no longer reaches the pipe, so they stay blocked for good (the rest wait on the MutexWriter lock behind them). This test fails on v26.11.9 and passes on v26.10.31 and with this PR:

test
type dropOutbound chan struct{}

func (d dropOutbound) Process(context.Context, *transport.Link, internet.Dialer) error {
	<-d // never reads the uplink, then the connection "drops"
	return io.ErrUnexpectedEOF
}

func TestClientWorkerDropDuringUpload(t *testing.T) {
	drop := make(dropOutbound)
	w, _ := (&mux.DialingWorkerFactory{Proxy: drop, Strategy: mux.ClientStrategy{MaxConcurrency: 8, MaxConnection: 8}}).Create()
	ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{Target: net.TCPDestination(net.DomainAddress("example.com"), 80)}})
	r, wr := pipe.New(pipe.WithoutSizeLimit())
	_, out := pipe.New(pipe.WithoutSizeLimit())
	for i := 0; i < 32; i++ { // 256 KiB, more than the 64 KiB uplink pipe
		b := buf.New()
		b.Extend(buf.Size)
		wr.WriteMultiBuffer(buf.MultiBuffer{b})
	}
	w.Dispatch(ctx, &transport.Link{Reader: r, Writer: out})
	time.Sleep(500 * time.Millisecond)
	close(drop)
	<-w.WaitClosed()
	time.Sleep(time.Second)
	stack := make([]byte, 1<<20)
	if strings.Contains(string(stack[:runtime.Stack(stack, true)]), "mux.fetchInput") {
		t.Fatal("session goroutine still blocked on the uplink after the worker closed")
	}
}

@RPRX

RPRX commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

客户端大概是这样:Mux 内发起关闭 downlinkReader 导致 VLESS 出站双向 copy 结束返回到 if errP := p.Process,c.Close()

Update:逻辑或许是 VLESS 出站无法往回写 downlinkWriter -> 底层连接关闭 -> VLESS 出站无法往外写 -> 双向 copy 结束

大概只有 Mux 内 idle timeout 是这条路,如果是 VLESS 客户端到服务端的连接断掉了的情况,这个 ClientWorker 也肯定没了

@Medium1992

Copy link
Copy Markdown
Contributor Author

The test is the dropped-connection path: Process returns, c.Close() runs and the worker leaves the picker, but the session goroutines already blocked in upLinkWriter.WriteMultiBuffer on the full pipe never return — a blocked goroutine isn't collected, and nothing closes that pipe since Interrupt stopped reaching it.

@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member
func (m *ClientWorker) Dispatch(ctx context.Context, link *transport.Link) bool {
	if m.IsFull() {
		return false
	}

	sm := m.sessionManager
	s := sm.Allocate(&m.strategy, link.Reader, link.Writer)
	if s == nil {
		return false
	}
	go fetchInput(ctx, s, m.link.Writer)
	if _, ok := link.Reader.(*pipe.Reader); !ok {
		select {
		case <-ctx.Done():
		case <-s.done.Wait():
		}
	}
	return true
}

go fetchInput(ctx, s, m.link.Writer) 内卡住吗?确实有可能 leak goroutine,我再看看,好在至少服务端没问题

@Medium1992

Copy link
Copy Markdown
Contributor Author

Yes, inside fetchInput: it waits in the pipe's select for writeSignal or done, and only Interrupt/Close on upLinkWriter sets done. Stack from the test on v26.11.9, a second after the worker closed:

goroutine 26 [select]:
transport/pipe.(*pipe).WriteMultiBuffer      impl.go:161
transport/pipe.(*Writer).WriteMultiBuffer    writer.go:14
common/mux.(*MutexWriter).WriteMultiBuffer   writer.go:22
common/mux.writeMetaWithFrame                writer.go:99
common/mux.(*Writer).WriteMultiBuffer        writer.go:126
common/mux.fetchInput                        client.go:277

@RPRX

RPRX commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

不过触发条件是它正好处于写满等位置、但 VLESS 出站不再从 Mux 客户端读取的状态,不一定触发,因为 VLESS 客户端到服务端的连接断掉后,应用程序到 VLESS 客户端的连接会关掉,Mux 内 buf.Copy() 本身从应用程序读取都会失败,就不会等待写

即使触发了,好在只是 Mux 客户端的问题,且每次只是一个 goroutine、64kB memory leak,不是 VLESS Reverse 的话就还好

VLESS 客户端默认的 Single XUDP 不走 Mux 客户端组件,所以也就客户端明确开 Mux 时有可能触发,对 XDRIVE 有轻微影响

无论如何,这个 PR 肯定会合并,好在只是客户端问题、即使触发也不影响对于“连接状态”的判定,不会造成代理逻辑上的 bug

@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member

总之 v2 这架构读起来还是太耗费脑细胞了,正好要重整 Mux #4231 改为由 VLESS 出站调用吧,其它出站不管了 @Fangliding

@Fangliding

Copy link
Copy Markdown
Member

哎呀 那个改动不该合的 我之前就关掉一个 这段时间忙一点没怎么看PR就炸了

@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member

那个改动本身的目的倒没问题,就是不该有对其它组件的 concurrent writes,这个 PR 修的问题也好修

他本来写的加层 pipe #7337 我给改成锁了 5745dea ,不确定他那种写法有没有这问题,有点烧脑

@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member

这个 PR 修的问题是很小的问题,只是这个 PR 正好提醒了要把 Mux 客户端模块移到 VLESS 出站内调用,又可以少些 pipe

@Fangliding

Copy link
Copy Markdown
Member

唉算了我来吧

@Fangliding Fangliding closed this Nov 9, 2026
@RPRX

RPRX commented Nov 9, 2026

Copy link
Copy Markdown
Member

我咋感觉这修复没问题,你的改法也开个 PR 看看

@RPRX RPRX reopened this Nov 9, 2026
@RPRX RPRX closed this in c43ee98 Nov 10, 2026
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.

3 participants