{"record":{"id":"d4dbfd319cb67e80","repo":"VictoriaMetrics/VictoriaMetrics","slug":"cannot-flush-q-to-server-w","errorCode":null,"errorMessage":"cannot flush %q to server: %w","messagePattern":"cannot flush %q to server: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/handshake/handshake.go","lineNumber":292,"sourceCode":"\treturn writeMessage(c, string(buf[:]))\n}\n\nfunc readIsCompressed(c net.Conn) (bool, error) {\n\tbuf, err := readData(c, 1)\n\tif err != nil {\n\t\treturn false, err\n\t}\n\tisCompressed := buf[0] != 0\n\treturn isCompressed, nil\n}\n\nfunc writeMessage(c net.Conn, msg string) error {\n\tif _, err := io.WriteString(c, msg); err != nil {\n\t\treturn fmt.Errorf(\"cannot write %q to server: %w\", msg, err)\n\t}\n\tif fc, ok := c.(flusher); ok {\n\t\tif err := fc.Flush(); err != nil {\n\t\t\treturn fmt.Errorf(\"cannot flush %q to server: %w\", msg, err)\n\t\t}\n\t}\n\treturn nil\n}\n\ntype flusher interface {\n\tFlush() error\n}\n\nfunc readMessage(c net.Conn, msg string) error {\n\tbuf, err := readData(c, len(msg))\n\tif err != nil {\n\t\treturn err\n\t}\n\tif string(buf) != msg {\n\t\treturn fmt.Errorf(\"unexpected message obtained; got %q; want %q\", buf, msg)\n\t}\n\treturn nil","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/VictoriaMetrics/VictoriaMetrics/blob/5079fb58f1e8e62113f90c945ad71586c797d770/lib/handshake/handshake.go#L274-L310","documentation":"writeMessage in lib/handshake sends a handshake message over a net.Conn and, if the connection implements a Flush method (buffered/borrowed conn), flushes it. This error wraps the underlying Flush failure, meaning the buffered data could not be pushed to the peer.","triggerScenarios":"Calling writeMessage (via genericServer, genericClient, or writeIsCompressed) when the connection's Flush() returns an error, typically because the underlying TCP conn is broken or the peer closed it mid-handshake.","commonSituations":"Peer process crashed or restarted during handshake; network drop; connection deadline expired; load balancer terminated an idle connection just as the handshake started.","solutions":["Check connectivity/firewall between the two endpoints (the wrapped error names the root cause).","Add retries with backoff around the connection/handshake on the client side.","Verify the peer service is running and accepting connections (logs, liveness probes).","Ensure read/write deadlines on the conn are generous enough for slow networks."],"exampleFix":"// before\nif err := writeMessage(conn, msg); err != nil {\n    return err\n}\n// after\nif err := writeMessage(conn, msg); err != nil {\n    logger.Printf(\"handshake flush failed, retrying: %v\", err)\n    conn.Close()\n    conn, err = dialWithRetry(addr, 3, time.Second)\n    if err != nil { return err }\n    return writeMessage(conn, msg)\n}","handlingStrategy":"retry","validationCode":"// Before using the conn, verify it is alive:\nif tcp, ok := c.(*net.TCPConn); ok {\n    if err := tcp.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {\n        return fmt.Errorf(\"conn unusable: %w\", err)\n    }\n}\nif n, err := c.Write([]byte{}); err != nil || n < 0 {\n    return fmt.Errorf(\"conn already broken: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"err := writeMessage(conn, msg)\nif err != nil {\n    // wrapped Flush failure is unrecoverable on this conn\n    conn.Close()\n    conn, err = redial(addr) // retry on a fresh connection\n}","preventionTips":["Always redial on handshake flush errors; never reuse the conn","Set explicit read/write deadlines before the handshake","Enable TCP keepalives to detect dead peers early","Monitor peer liveness/health so restarts are known quickly"],"tags":["network","io","handshake","tcp"],"backgroundTag":"connection-reset-during-write","analyzedSha":"5079fb58f1e8e62113f90c945ad71586c797d770","analyzedAt":"2026-09-03T18:10:26.153Z","contentChangedAt":"2026-09-03T18:10:26.153Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}