Skip to content

Commit

Permalink
netconn: Avoid returning 0, nil in NetConn.Read
Browse files Browse the repository at this point in the history
Closes #367
  • Loading branch information
nhooyr committed Oct 13, 2023
1 parent a02cbef commit c26a23d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions netconn.go
Expand Up @@ -141,6 +141,10 @@ func (nc *netConn) Read(p []byte) (int, error) {
nc.readMu.forceLock()
defer nc.readMu.unlock()

return nc.read(p)
}

func (nc *netConn) read(p []byte) (int, error) {
if atomic.LoadInt64(&nc.readExpired) == 1 {
return 0, fmt.Errorf("failed to read: %w", context.DeadlineExceeded)
}
Expand Down Expand Up @@ -171,6 +175,10 @@ func (nc *netConn) Read(p []byte) (int, error) {
if err == io.EOF {
nc.reader = nil
err = nil
if n == 0 {
// Avoid returning 0, nil. #367
n, err = nc.read(p)
}
}
return n, err
}
Expand Down

0 comments on commit c26a23d

Please sign in to comment.