Skip to content

Commit 56637bf

Browse files
0pcomcharmcrush
andcommitted
Fix data race in SessionCommon Close() and Ping() methods
Protect concurrent access to sm.yamux and sm.smux fields with sm.mutx lock to prevent data races when Close() or Ping() are called while session is being initialized. The race occurred when: - handleSession() writes to dSes.sm.yamux (server.go:242) while holding the lock - Close() reads sc.sm.yamux (session_common.go:187-188) without holding the lock - A goroutine can call Close() concurrently during session setup Changes: - Add sm.mutx.Lock() protection in Close() method - Add sm.mutx.RLock() protection in Ping() method for consistency 💘 Generated with Crush Co-Authored-By: Crush <[email protected]>
1 parent f2471ee commit 56637bf

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

pkg/dmsg/session_common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ func (sc *SessionCommon) RemoteTCPAddr() net.Addr { return sc.netConn.RemoteAddr
169169

170170
// Ping obtains the round trip latency of the session.
171171
func (sc *SessionCommon) Ping() (time.Duration, error) {
172+
sc.sm.mutx.RLock()
173+
defer sc.sm.mutx.RUnlock()
172174
if sc.sm.yamux != nil {
173175
return sc.sm.yamux.Ping()
174176
}
@@ -181,12 +183,14 @@ func (sc *SessionCommon) Close() error {
181183
return nil
182184
}
183185
var err error
186+
sc.sm.mutx.Lock()
184187
if sc.sm.smux != nil {
185188
err = sc.sm.smux.Close()
186189
}
187190
if sc.sm.yamux != nil {
188191
err = sc.sm.yamux.Close()
189192
}
193+
sc.sm.mutx.Unlock()
190194
sc.rMx.Lock()
191195
sc.nMap = nil
192196
sc.rMx.Unlock()

0 commit comments

Comments
 (0)