{"record":{"id":"26a762dc9aedac09","repo":"grafana/k6","slug":"could-flush-the-data-to-the-output-w","errorCode":null,"errorMessage":"could flush the data to the output: %w","messagePattern":"could flush the data to the output: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/ui.go","lineNumber":404,"sourceCode":"\t\t\t\t\ttermWidth = tw\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trenderProgressBars(true)\n\t\tgs.OutMutex.Lock()\n\t\tprintProgressBars()\n\t\tgs.OutMutex.Unlock()\n\t}\n}\n\nfunc yamlPrint(w io.Writer, v any) error {\n\tdata, err := yaml.Marshal(v)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"could not marshal YAML: %w\", err)\n\t}\n\t_, err = fmt.Fprint(w, string(data))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"could flush the data to the output: %w\", err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":386,"sourceCodeEnd":408,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/ui.go#L386-L408","documentation":"After marshaling succeeds, yamlPrint writes the bytes with fmt.Fprint to the writer (typically stdout). If the write fails — most commonly a broken pipe when the consumer closed early (e.g. piping to head) — the error is wrapped as \"could flush the data to the output\".","triggerScenarios":"Piping the command's output to a short-reading consumer that exits before k6 finishes writing, e.g. 'k6 status | head -1'; stdout redirected to a closed or unlinked file descriptor.","commonSituations":"Shell pipelines with head/less quitting early; CI capturing output into a closed fd; terminal window closed mid-output.","solutions":["Capture to a file first, then post-process: k6 status > status.yaml && head -5 status.yaml","Use a full-stream reader (less) instead of head","In wrapper programs, read stdout to EOF before exiting the reader process"],"exampleFix":"# before\nk6 status | head -5   # reader closes the pipe early\n# after\nk6 status > status.yaml && head -5 status.yaml","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"# Write to a file so short-lived readers cannot break the pipe\nk6 status > status.yaml || { echo 'stdout write failed' >&2; exit 2; }\nhead -5 status.yaml","preventionTips":["Redirect output to a file instead of piping to short-lived readers","Use less (reads the whole stream) rather than head","In programs consuming k6 output via pipes, read until EOF before exiting"],"tags":["io","stdout","broken-pipe"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}