Skip to content

Commit

Permalink
Fix vmess mux leak
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jan 1, 2025
1 parent 5ec5311 commit 8cd5f17
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

func HandleMuxConnection(ctx context.Context, conn net.Conn, handler Handler) error {
ctx, cancel := context.WithCancelCause(ctx)
session := &serverSession{
ctx: ctx,
conn: conn,
Expand All @@ -28,13 +29,11 @@ func HandleMuxConnection(ctx context.Context, conn net.Conn, handler Handler) er
streams: make(map[uint16]*serverStream),
writer: std_bufio.NewWriter(conn),
}
if ctx.Done() != nil {
go func() {
<-ctx.Done()
session.cleanup(ctx.Err())
}()
}
return session.recvLoop()
go func() {
<-ctx.Done()
session.cleanup(ctx.Err())
}()
return session.recvLoop(cancel)
}

type serverSession struct {
Expand All @@ -55,11 +54,11 @@ type serverStream struct {
pipe *io.PipeWriter
}

func (c *serverSession) recvLoop() error {
func (c *serverSession) recvLoop(cancel context.CancelCauseFunc) error {
for {
err := c.recv()
if err != nil {
c.cleanup(err)
cancel(err)
return E.Cause(err, "mux connection closed")
}
}
Expand Down

0 comments on commit 8cd5f17

Please sign in to comment.