Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix potential deadlock due to inconsistent locking order #67

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,21 +376,14 @@ func (s *Stream) cleanup() {
// processFlags is used to update the state of the stream
// 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
defer func() {
if closeStream {
s.cleanup()
}
}()
var establishStream, closeStream, notify bool

s.stateLock.Lock()
defer s.stateLock.Unlock()
if flags&flagACK == flagACK {
if s.state == streamSYNSent {
s.state = streamEstablished
}
s.session.establishStream(s.id)
establishStream = true
}
if flags&flagFIN == flagFIN {
if s.readState == halfOpen {
Expand All @@ -400,6 +393,7 @@ func (s *Stream) processFlags(flags uint16) {
closeStream = true
s.state = streamFinished
}
notify = true
s.notifyWaiting()
}
}
Expand All @@ -412,8 +406,20 @@ func (s *Stream) processFlags(flags uint16) {
}
s.state = streamFinished
closeStream = true
notify = true
s.notifyWaiting()
}
s.stateLock.Unlock()

if establishStream {
s.session.establishStream(s.id)
}
if notify {
s.notifyWaiting()
}
if closeStream {
s.cleanup()
}
}

// notifyWaiting notifies all the waiting channels
Expand Down