Skip to content

Commit dee5898

Browse files
authored
fix: memory leak due to unclosed session (#1908)
1 parent 1e22f4d commit dee5898

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

transport/gost-plugin/websocket.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ type Option struct {
2323
Mux bool
2424
}
2525

26+
// muxConn is a wrapper around smux.Stream that also closes the session when closed
27+
type muxConn struct {
28+
net.Conn
29+
session *smux.Session
30+
}
31+
32+
func (m *muxConn) Close() error {
33+
streamErr := m.Conn.Close()
34+
sessionErr := m.session.Close()
35+
36+
// Return stream error if there is one, otherwise return session error
37+
if streamErr != nil {
38+
return streamErr
39+
}
40+
return sessionErr
41+
}
42+
2643
// NewGostWebsocket return a gost websocket
2744
func NewGostWebsocket(ctx context.Context, conn net.Conn, option *Option) (net.Conn, error) {
2845
header := http.Header{}
@@ -72,10 +89,14 @@ func NewGostWebsocket(ctx context.Context, conn net.Conn, option *Option) (net.C
7289

7390
stream, err := session.OpenStream()
7491
if err != nil {
92+
session.Close()
7593
return nil, err
7694
}
7795

78-
conn = stream
96+
return &muxConn{
97+
Conn: stream,
98+
session: session,
99+
}, nil
79100
}
80101
return conn, nil
81102
}

0 commit comments

Comments
 (0)