Skip to content

Commit

Permalink
read returns EOF if closed gracefully by remote
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Jun 18, 2024
1 parent d7aacf9 commit 6441b29
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ncp
import (
"context"
"errors"
"io"
"log"
"net"
"strings"
Expand Down Expand Up @@ -49,6 +50,7 @@ type Session struct {
sync.RWMutex
isEstablished bool
isClosed bool
isClosedByRemote bool
sendBuffer []byte
sendWindowStartSeq uint32
sendWindowEndSeq uint32
Expand Down Expand Up @@ -122,6 +124,12 @@ func (session *Session) IsClosed() bool {
return session.isClosed
}

func (session *Session) IsClosedByRemote() bool {
session.RLock()
defer session.RUnlock()
return session.isClosedByRemote
}

func (session *Session) GetBytesRead() uint64 {
session.RLock()
defer session.RUnlock()
Expand Down Expand Up @@ -621,7 +629,10 @@ func (session *Session) handleClosePacket() error {
session.cancel()

session.Lock()
session.isClosed = true
if !session.isClosed {
session.isClosed = true
session.isClosedByRemote = true
}
session.Unlock()

return nil
Expand Down Expand Up @@ -691,6 +702,9 @@ func (session *Session) Read(b []byte) (_ int, e error) {
if e == context.Canceled {
e = ErrSessionClosed
}
if e == ErrSessionClosed && session.IsClosedByRemote() {
e = io.EOF
}
}()

if session.IsClosed() {
Expand Down Expand Up @@ -957,7 +971,7 @@ func (session *Session) updateConnWindowSize() {
totalSize := 0.0
for _, conn := range session.connections {
conn.RLock()
totalSize += float64(conn.windowSize)
totalSize += conn.windowSize
conn.RUnlock()
}
if totalSize <= 0 {
Expand Down
File renamed without changes.

0 comments on commit 6441b29

Please sign in to comment.