{"record":{"id":"466afaa9d73ef9c4","repo":"kubernetes/kops","slug":"error-writing-to-output-v-466afa","errorCode":null,"errorMessage":"error writing to output: %v","messagePattern":"error writing to output: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/get_sshpublickeys.go","lineNumber":126,"sourceCode":"\tswitch options.Output {\n\n\tcase OutputTable:\n\t\tif len(items) == 0 {\n\t\t\treturn fmt.Errorf(\"no SSH public key found\")\n\t\t}\n\t\tt := &tables.Table{}\n\t\tt.AddColumn(\"ID\", func(i *SSHKeyItem) string {\n\t\t\treturn i.ID\n\t\t})\n\t\treturn t.Render(items, out, \"ID\")\n\n\tcase OutputYaml:\n\t\ty, err := yaml.Marshal(items)\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\tcase OutputJSON:\n\t\tj, err := json.Marshal(items)\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\tdefault:\n\t\treturn fmt.Errorf(\"unknown output format: %q\", options.Output)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":108,"sourceCodeEnd":142,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/get_sshpublickeys.go#L108-L142","documentation":"After successfully YAML-marshaling the key list, the command writes the bytes to the output writer. If that write fails (closed pipe, disk full, permission error on a redirect target), it returns \"error writing to output: %v\".","triggerScenarios":"`kops get sshpublickeys -o yaml` with stdout piped to a process that exited early (EPIPE), or output redirected to a file on a full filesystem or without write permission.","commonSituations":"Piping into `head` which closes early; CI logs directory full; redirecting to a read-only file.","solutions":["Re-run without the early-exiting pipe consumer, or pipe to `cat`","Check disk space and permissions on the redirect target","Inspect the wrapped error after 'error writing to output' for the OS-level cause","Handle EPIPE in wrappers/scripts rather than treating it as a kOps failure"],"exampleFix":"// before\nkops get sshpublickeys -o yaml | head -5\n// after\nkops get sshpublickeys -o yaml | head -5 || true   # tolerate SIGPIPE","handlingStrategy":"try-catch","validationCode":"out=/path/to/out.yaml; df -h \"$(dirname \"$out\")\" | awk 'NR==2 && $5+0 >= 95 {exit 1}'; [ -w \"$(dirname \"$out\")\" ] || exit 1","typeGuard":null,"tryCatchPattern":"if err := runGetSSHPublicKeys(opts); err != nil {\n\tif strings.HasPrefix(err.Error(), \"error writing to output:\") && strings.Contains(err.Error(), \"broken pipe\") {\n\t\treturn nil // consumer closed early; tolerate\n\t}\n\treturn err\n}","preventionTips":["Avoid head in pipelines; use awk 'NR<=N' which reads to EOF","Monitor disk space on CI runners","Write to a temp file then move, to fail fast on unwritable targets"],"tags":["cli","io","output"],"backgroundTag":"broken-pipe-write-failure","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"}