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 Dec 30, 2024
1 parent 8b806fd commit 71c1424
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, source M.Socksaddr, handler Handler) error {
ctx, cancel := context.WithCancelCause(ctx)
session := &serverSession{
ctx: ctx,
source: source,
Expand All @@ -29,13 +30,11 @@ func HandleMuxConnection(ctx context.Context, conn net.Conn, source M.Socksaddr,
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 @@ -57,11 +56,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 71c1424

Please sign in to comment.