{"record":{"id":"e169f81c5682f8c5","repo":"wavetermdev/waveterm","slug":"failed-to-write-buffered-data-v","errorCode":null,"errorMessage":"failed to write buffered data: %v","messagePattern":"failed to write buffered data: (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/waveapp/waveapp.go","lineNumber":427,"sourceCode":"\t}\n\n\t// Handle the content based on the option type\n\tswitch {\n\tcase option.FilePath != \"\":\n\t\tfilePath := wavebase.ExpandHomeDirSafe(option.FilePath)\n\t\thttp.ServeFile(w, r, filePath)\n\n\tcase option.Data != nil:\n\t\tw.Header().Set(\"Content-Length\", fmt.Sprintf(\"%d\", len(option.Data)))\n\t\tw.WriteHeader(http.StatusOK)\n\t\tif _, err := w.Write(option.Data); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write data: %v\", err)\n\t\t}\n\n\tcase option.File != nil:\n\t\tif bufferedData != nil {\n\t\t\tif _, err := w.Write(bufferedData); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to write buffered data: %v\", err)\n\t\t\t}\n\t\t}\n\t\tif _, err := io.Copy(w, option.File); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to copy from file: %v\", err)\n\t\t}\n\n\tcase option.Reader != nil:\n\t\tif bufferedData != nil {\n\t\t\tif _, err := w.Write(bufferedData); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to write buffered data: %v\", err)\n\t\t\t}\n\t\t}\n\t\tif _, err := io.Copy(w, option.Reader); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to copy from reader: %v\", err)\n\t\t}\n\n\tdefault:\n\t\treturn fmt.Errorf(\"no content available\")","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/waveapp/waveapp.go#L409-L445","documentation":"ServeFileOption chose the option.File branch and had buffered bytes to flush first; writing that buffered prefix to the http.ResponseWriter failed. As with the data branch, this usually means the client went away or the connection broke mid-response, so the buffered head of the file could not be delivered and the handler aborts.","triggerScenarios":"Serving a file option where the first chunk was buffered (e.g. sniffing content type) and the client disconnects before the flush, the request is canceled, or the connection resets mid-stream.","commonSituations":"User closing the Wave block while a file preview/stream loads; frontend aborting a slow fetch; large files over an interrupted local connection.","solutions":["Treat client-disconnect errors as benign: check r.Context().Err() before propagating.","Retry the load on the frontend side; the file itself is fine.","For large files, ensure buffering is bounded so the flush happens promptly before the client times out.","Verify the file handle is valid and readable before serving (a read error on bufferedData's source can surface here).","Check Wave logs to confirm whether the write error correlates with a canceled request."],"exampleFix":"// before\nif _, err := w.Write(bufferedData); err != nil {\n    return fmt.Errorf(\"failed to write buffered data: %v\", err)\n}\n// after\nif _, err := w.Write(bufferedData); err != nil && r.Context().Err() == nil {\n    return fmt.Errorf(\"failed to write buffered data: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"if option.File == nil {\n    return fmt.Errorf(\"no file to serve\")\n}\nif r.Context().Err() != nil {\n    return nil // request already canceled\n}","typeGuard":null,"tryCatchPattern":"if _, err := w.Write(bufferedData); err != nil {\n    if r.Context().Err() != nil {\n        return nil // client disconnected mid-stream; benign\n    }\n    return fmt.Errorf(\"failed to write buffered data: %v\", err)\n}\nif _, err := io.Copy(w, option.File); err != nil {\n    return fmt.Errorf(\"failed to copy from file: %v\", err)\n}","preventionTips":["Verify the file opens and reads correctly before serving.","Bound the buffered prefix so flushes happen promptly.","Check r.Context().Err() to filter client-disconnect noise.","Log write failures at debug level; they are usually cancellations, not data corruption."],"tags":["http","io","streaming","response-write"],"backgroundTag":"http-response-write-failed","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}