Brilliant. Yes, that was my bug. Thank you Steven!

On Monday, November 18, 2024 at 11:30:18 AM UTC-6 Steven Hartland wrote:

I believe the problem you have is that your readFull can return a partial 
read if an error occurs e.g. timeout so when that happens you lose data by 
overwriting the partial result with the next read.

If you apply something like the following, which returns the bytes read and 
continues the read from that offset you should be good:
--- main.go.timeout-race        2024-11-18 14:07:03
+++ main.go     2024-11-18 17:24:09
@@ -25,7 +25,7 @@ func service(conn net.Conn) {
 func service(conn net.Conn) {
 
        cliGoroNumBytes := make([]byte, 8)
-       err := readFull(conn, cliGoroNumBytes, nil)
+       _, err := readFull(conn, cliGoroNumBytes, nil)
        if err != nil {
                panic(err)
        }
@@ -115,8 +115,10 @@ func client(id int) {
                // need to periodically check for other events, e.g. 
shutdown, pause, etc.
                //
                // When we do so, we observe data loss.
+               var offset int
                for {
-                       err := readFull(conn, buff, &timeout)
+                       n, err := readFull(conn, buff[offset:], &timeout)
+                       offset += n
                        if err != nil {
                                //fmt.Printf("err = '%v'; current i=%v; 
prev j=%v\n", err, i, j)
                                r := err.Error()
@@ -153,8 +155,7 @@ func readFull(conn net.Conn, buf []byte, timeout *time
 var zeroTime = time.Time{}
 
 // readFull reads exactly len(buf) bytes from conn
-func readFull(conn net.Conn, buf []byte, timeout *time.Duration) error {
-
+func readFull(conn net.Conn, buf []byte, timeout *time.Duration) (int, 
error) {
        if timeout != nil && *timeout > 0 {
                conn.SetReadDeadline(time.Now().Add(*timeout))
        } else {
@@ -172,13 +173,13 @@ func readFull(conn net.Conn, buf []byte, timeout *time
                        if err != nil {
                                panic(err)
                        }
-                       return nil
+                       return total, nil
                }
                if err != nil {
-                       return err
+                       return total, err
                }
        }
-       return nil
+       return total, nil
 }
 
 func startClients() {

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/dff6feb2-b561-4115-9e45-6403d34e6519n%40googlegroups.com.

Reply via email to