Skip to content

Commit

Permalink
fix inconsistent locking of mutexes by holding the stateLock for shor…
Browse files Browse the repository at this point in the history
…ter (#98)
  • Loading branch information
marten-seemann authored Sep 6, 2022
1 parent 781eed8 commit e0dd63f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,40 +384,48 @@ func (s *Stream) cleanup() {
// based on set flags, if any. Lock must be held
func (s *Stream) processFlags(flags uint16) {
// Close the stream without holding the state lock
closeStream := false
var closeStream bool
defer func() {
if closeStream {
s.cleanup()
}
}()

s.stateLock.Lock()
defer s.stateLock.Unlock()
if flags&flagACK == flagACK {
s.stateLock.Lock()
if s.state == streamSYNSent {
s.state = streamEstablished
}
s.stateLock.Unlock()
s.session.establishStream(s.id)
}
if flags&flagFIN == flagFIN {
var notify bool
s.stateLock.Lock()
if s.readState == halfOpen {
s.readState = halfClosed
if s.writeState != halfOpen {
// We're now fully closed.
closeStream = true
s.state = streamFinished
}
notify = true
}
s.stateLock.Unlock()
if notify {
s.notifyWaiting()
}
}
if flags&flagRST == flagRST {
s.stateLock.Lock()
if s.readState == halfOpen {
s.readState = halfReset
}
if s.writeState == halfOpen {
s.writeState = halfReset
}
s.state = streamFinished
s.stateLock.Unlock()
closeStream = true
s.notifyWaiting()
}
Expand Down

0 comments on commit e0dd63f

Please sign in to comment.