{"record":{"id":"a81a4afae18d8ded","repo":"VictoriaMetrics/VictoriaMetrics","slug":"cannot-flush-data-to-client-w","errorCode":null,"errorMessage":"cannot flush data to client: %w","messagePattern":"cannot flush data to client: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"lib/bufferedwriter/bufferedwriter.go","lineNumber":87,"sourceCode":"\t}\n\treturn n, bw.err\n}\n\n// Flush flushes bw to the underlying writer.\n//\n// Connection close errors are ignored to not trigger on them and to not write to logs, but Write method doesn't ignore\n// them since it may lead to an unexpected behaviour (see https://github.com/VictoriaMetrics/VictoriaMetrics/pull/8157)\nfunc (bw *Writer) Flush() error {\n\tbw.lock.Lock()\n\tdefer bw.lock.Unlock()\n\tif bw.err != nil {\n\t\tif netutil.IsTrivialNetworkError(bw.err) {\n\t\t\treturn nil\n\t\t}\n\t\treturn bw.err\n\t}\n\tif err := bw.bw.Flush(); err != nil {\n\t\tbw.err = fmt.Errorf(\"cannot flush data to client: %w\", err)\n\t\tif netutil.IsTrivialNetworkError(err) {\n\t\t\treturn nil\n\t\t}\n\t}\n\treturn bw.err\n}\n\n// Error returns the first occurred error in bw.\nfunc (bw *Writer) Error() error {\n\tbw.lock.Lock()\n\tdefer bw.lock.Unlock()\n\treturn bw.err\n}\n","sourceCodeStart":69,"sourceCodeEnd":101,"githubUrl":"https://github.com/VictoriaMetrics/VictoriaMetrics/blob/5079fb58f1e8e62113f90c945ad71586c797d770/lib/bufferedwriter/bufferedwriter.go#L69-L101","documentation":"BufferedWriter.Flush propagates an error from the underlying bufio.Writer.Flush when flushing buffered data to the client fails. If the error is a trivial network error (client disconnected), it is swallowed and nil is returned; otherwise bw.err is set and returned to the caller.","triggerScenarios":"Calling bw.Flush() when the underlying writer (network conn) returns a non-trivial error: broken pipe, connection reset by peer, or disk/socket write failure.","commonSituations":"HTTP client closed the connection before the buffered response finished; TLS renegotiation failures; container/network interruptions mid-stream.","solutions":["Read the wrapped cause; trivial network errors are already handled (nil returned), so only real failures reach you","Confirm client behavior — reconnects/timeouts on the consumer side are the usual root cause","Check network stability, proxies, and keep-alive settings between server and client","Add retry at the application layer if the flushed payload can be regenerated"],"exampleFix":"// before\nif err := bw.Flush(); err != nil { log.Fatal(err) }\n// after\nif err := bw.Flush(); err != nil {\n    if netutil.IsTrivialNetworkError(err) {\n        return nil // client disconnected — not a server fault\n    }\n    return err\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := bw.Flush(); err != nil && !netutil.IsTrivialNetworkError(err) {\n    log.Printf(\"flush failed: %v\", err)\n}","preventionTips":["Treat trivial network errors (disconnects) as non-fatal, as the library already does","Monitor client disconnect rates rather than treating every flush error as a server bug","Keep the underlying connection alive with keep-alives/proxy timeouts aligned to buffer flush cadence"],"tags":["network","io","flush","buffered-writer"],"backgroundTag":"broken-pipe","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"}