{"record":{"id":"ef3b711b74f38662","repo":"wavetermdev/waveterm","slug":"failed-to-write-data-v","errorCode":null,"errorMessage":"failed to write data: %v","messagePattern":"failed to write data: (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/waveapp/waveapp.go","lineNumber":421,"sourceCode":"\t\t\tif inm == etag {\n\t\t\t\t// Resource not modified\n\t\t\t\tw.WriteHeader(http.StatusNotModified)\n\t\t\t\treturn nil\n\t\t\t}\n\t\t}\n\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}","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/waveapp/waveapp.go#L403-L439","documentation":"ServeFileOption chose the option.Data branch (in-memory bytes) and set Content-Length to len(option.Data), but http.ResponseWriter.Write returned an error. Since Content-Length was already declared, a short/failed write means the response is corrupt; the handler returns this error. Typical cause: the client disconnected or canceled the request mid-response.","triggerScenarios":"Serving a vdom file option with Data set when the HTTP client (Wave frontend fetch) disconnects before the body completes, the request context is canceled, or the connection is reset.","commonSituations":"User closes/navigates the block while a large data payload is being served; frontend request timeouts aborting the download; network interruption between Wave frontend and the local HTTP handler.","solutions":["Verify the option.Data length matches the declared Content-Length and the data is fully loaded before serving.","Check Wave logs/frontend console for request cancellation; this error is often benign client-disconnect noise.","Reduce payload size or switch to option.File streaming for large content so writes happen incrementally.","Ensure the handler isn't writing after the request context was canceled (check r.Context().Err()).","Retry the frontend fetch; a transient disconnect will not recur."],"exampleFix":"// before\nif _, err := w.Write(option.Data); err != nil {\n    return fmt.Errorf(\"failed to write data: %v\", err)\n}\n// after: ignore benign client-disconnect errors\nif _, err := w.Write(option.Data); err != nil && r.Context().Err() == nil {\n    return fmt.Errorf(\"failed to write data: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"if r.Context().Err() != nil {\n    return nil // client already gone; nothing to write\n}\nif len(option.Data) == 0 {\n    return fmt.Errorf(\"refusing to serve empty data option\")\n}","typeGuard":null,"tryCatchPattern":"if _, err := w.Write(option.Data); err != nil {\n    if r.Context().Err() != nil {\n        return nil // client disconnected; benign\n    }\n    return fmt.Errorf(\"failed to write data: %v\", err)\n}","preventionTips":["Check r.Context().Err() before treating write errors as fatal.","Keep Data payloads modest; stream large content via option.File.","Set Content-Length only when the payload is fully materialized.","Expect disconnect errors when users close blocks mid-fetch; log at debug level."],"tags":["http","io","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"}