From c26a23d10605b744fc034152c915a92a84a9d322 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 13 Oct 2023 04:30:08 -0700 Subject: [PATCH] netconn: Avoid returning 0, nil in NetConn.Read Closes #367 --- netconn.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/netconn.go b/netconn.go index 74000c9e..9dd4a241 100644 --- a/netconn.go +++ b/netconn.go @@ -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) } @@ -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 }