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

fixes a stream deadlock multiple ways #8

Merged
merged 3 commits into from
Oct 22, 2019
Merged
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
28 changes: 14 additions & 14 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ func (s *Stream) Read(b []byte) (n int, err error) {
defer asyncNotify(s.recvNotifyCh)
START:
s.stateLock.Lock()
switch s.state {
state := s.state
s.stateLock.Unlock()

switch state {
case streamRemoteClose:
fallthrough
case streamClosed:
s.recvLock.Lock()
if s.recvBuf.Len() == 0 {
s.recvLock.Unlock()
s.stateLock.Unlock()
empty := s.recvBuf.Len() == 0
s.recvLock.Unlock()
if empty {
return 0, io.EOF
}
s.recvLock.Unlock()
case streamReset:
s.stateLock.Unlock()
return 0, ErrConnectionReset
}
s.stateLock.Unlock()

// If there is no data available, block
s.recvLock.Lock()
Expand Down Expand Up @@ -143,17 +143,17 @@ func (s *Stream) write(b []byte) (n int, err error) {

START:
s.stateLock.Lock()
switch s.state {
state := s.state
s.stateLock.Unlock()

switch state {
case streamLocalClose:
fallthrough
case streamClosed:
s.stateLock.Unlock()
return 0, ErrStreamClosed
case streamReset:
s.stateLock.Unlock()
return 0, ErrConnectionReset
}
s.stateLock.Unlock()

// If there is no data available, block
window := atomic.LoadUint32(&s.sendWindow)
Expand Down Expand Up @@ -208,14 +208,14 @@ func (s *Stream) sendFlags() uint16 {
// sendWindowUpdate potentially sends a window update enabling
// further writes to take place. Must be invoked with the lock.
func (s *Stream) sendWindowUpdate() error {
// Determine the flags if any
flags := s.sendFlags()

// Determine the delta update
max := s.session.config.MaxStreamWindowSize
s.recvLock.Lock()
delta := (max - uint32(s.recvBuf.Len())) - s.recvWindow

// Determine the flags if any
flags := s.sendFlags()

// Check if we can omit the update
if delta < (max/2) && flags == 0 {
s.recvLock.Unlock()
Expand Down