{"record":{"id":"f8f0fb617434d61e","repo":"kubernetes/kops","slug":"error-writing-to-output-v-f8f0fb","errorCode":null,"errorMessage":"error writing to output: %v","messagePattern":"error writing to output: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/get_instances.go","lineNumber":165,"sourceCode":"\t\treturn err\n\t}\n\n\tfor _, cg := range cloudGroups {\n\t\tcloudInstances = append(cloudInstances, cg.Ready...)\n\t\tcloudInstances = append(cloudInstances, cg.NeedUpdate...)\n\t\tcg.AdjustNeedUpdate()\n\t}\n\n\tswitch options.Output {\n\tcase OutputTable:\n\t\treturn instanceOutputTable(cloudInstances, out)\n\tcase OutputYaml:\n\t\ty, err := yaml.Marshal(asRenderable(cloudInstances))\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to marshal YAML: %v\", err)\n\t\t}\n\t\tif _, err := out.Write(y); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing to output: %v\", err)\n\t\t}\n\t\treturn nil\n\tcase OutputJSON:\n\t\tj, err := json.Marshal(asRenderable(cloudInstances))\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to marshal JSON: %v\", err)\n\t\t}\n\t\tif _, err := out.Write(j); err != nil {\n\t\t\treturn fmt.Errorf(\"error writing to output: %v\", err)\n\t\t}\n\t\treturn nil\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported output format: %q\", options.Output)\n\t}\n}\n\nfunc instanceOutputTable(instances []*cloudinstances.CloudInstance, out io.Writer) error {\n\tfmt.Println(\"\")","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/get_instances.go#L147-L183","documentation":"After successfully YAML-marshaling the instance list, RunGetInstances writes the bytes to the command's output writer (stdout, or a file via redirection). If out.Write fails, the command returns \"error writing to output: %v\". This is an I/O failure of the destination stream, not a data problem.","triggerScenarios":"`kops get instances -o yaml` where the io.Writer passed to RunGetInstances errors on Write at get_instances.go:164-165 — e.g. stdout closed (EPIPE from `| head`), disk full when redirecting to a file, or a closed custom writer.","commonSituations":"Piping into `head` or `less -F` that closes the pipe early (broken pipe); redirecting to a file on a full filesystem or read-only mount; writing to a closed pipe in scripted/CI contexts.","solutions":["Check the wrapped inner error: EPIPE means the downstream reader closed — use `cat` or drop the early-closing consumer","Verify disk space (df -h) and file permissions when redirecting output to a file","Retry in a terminal where stdout is a valid tty or an open regular file","In scripts, avoid commands that exit early (e.g. use `head -n 100 || true` semantics or full output capture)"],"exampleFix":"// before\nkops get instances -o yaml | head -5   # SIGPIPE kills the writer\n// after\nkops get instances -o yaml > /tmp/instances.yaml && head -5 /tmp/instances.yaml","handlingStrategy":"try-catch","validationCode":"// ensure output destination is writable before running\ndf -h . | awk 'NR==2 {exit ($5+0 > 95 ? 1 : 0)}' || { echo \"disk nearly full\"; exit 1; }","typeGuard":"if w, ok := out.(*os.File); ok {\n    if _, err := w.Stat(); err != nil {\n        return fmt.Errorf(\"output target unusable: %w\", err)\n    }\n}","tryCatchPattern":"if _, err := out.Write(y); err != nil {\n    if errors.Is(err, syscall.EPIPE) {\n        klog.V(2).Infof(\"downstream consumer closed the pipe\")\n        return nil\n    }\n    return fmt.Errorf(\"error writing to output: %w\", err)\n}","preventionTips":["Avoid piping into commands that exit early (head, less with quit-on-first-page); redirect to a file instead","Monitor disk space on CI runners before dumping large outputs","Check write permissions on the redirect target before running","Never close the process's stdout manually in wrapper scripts"],"tags":["io","stdout","broken-pipe","cli-output"],"backgroundTag":"broken-pipe","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}