Fix ss2022 gouroutine leak

ss2022-leak
风扇滑翔翼 2025-09-18 19:13:49 +00:00 committed by GitHub
parent fe57507fd9
commit 8bec31a035
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 1 deletions

View File

@ -4,8 +4,10 @@ import (
"context"
"io"
"net"
"time"
"github.com/sagernet/sing/common/bufio"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/transport"
)
@ -33,8 +35,26 @@ func (w *PipeConnWrapper) Close() error {
return nil
}
// This Read implemented a timeout to avoid goroutine leak.
// as a temporarily solution
func (w *PipeConnWrapper) Read(b []byte) (n int, err error) {
return w.R.Read(b)
type readResult struct {
n int
err error
}
c := make(chan readResult, 1)
go func() {
n, err := w.R.Read(b)
c <- readResult{n: n, err: err}
}()
select {
case result := <-c:
return result.n, result.err
case <-time.After(300 * time.Second):
common.Interrupt(w.R)
common.Close(w.R)
return 0, io.EOF
}
}
func (w *PipeConnWrapper) Write(p []byte) (n int, err error) {