{"record":{"id":"cdc00921cc789ffd","repo":"uutils/coreutils","slug":"head-error-writing-stdout","errorCode":null,"errorMessage":"head-error-writing-stdout","messagePattern":"head-error-writing-stdout","errorType":"error_code","errorClass":"io::Error","httpStatus":null,"severity":"warning","filePath":"src/uu/head/src/head.rs","lineNumber":209,"sourceCode":"        options.verbose = matches.get_flag(options::VERBOSE);\n        options.line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));\n        options.presume_input_pipe = matches.get_flag(options::PRESUME_INPUT_PIPE);\n\n        options.mode = Mode::from(matches)?;\n        // #[allow(clippy::unwrap_used, reason = \"clap provides '-' by default\")] <https://github.com/rust-lang/rust/issues/15701>\n        options.files = matches\n            .get_many::<OsString>(options::FILES)\n            .unwrap()\n            .cloned()\n            .collect();\n\n        Ok(options)\n    }\n}\n\n#[inline]\nfn wrap_in_stdout_error(err: io::Error) -> io::Error {\n    io::Error::new(\n        err.kind(),\n        translate!(\"head-error-writing-stdout\", \"err\" => uucore::error::strip_errno(&err)),\n    )\n}\n\n// zero-copy fast-path\n#[cfg(any(target_os = \"linux\", target_os = \"android\"))]\nfn print_n_bytes(input: impl AsFd, n: u64) -> io::Result<u64> {\n    let out = io::stdout();\n    uucore::pipes::send_n_bytes(input, &out, n).map_err(wrap_in_stdout_error)\n}\n\n#[cfg(not(any(target_os = \"linux\", target_os = \"android\")))]\nfn print_n_bytes(input: impl Read, n: u64) -> io::Result<u64> {\n    // Read the first `n` bytes from the `input` reader.\n    let mut reader = input.take(n);\n\n    // Write those bytes to `stdout`.","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/uutils/coreutils/blob/325183372aaf86d8ae6feaf4f9548f74fe815102/src/uu/head/src/head.rs#L191-L227","documentation":"head wraps any I/O error that occurs while writing output to stdout with this 'error writing stdout' message, preserving the original error kind and appending the stripped errno text. It distinguishes failures on the output side (broken pipe, full disk) from failures reading the input files.","triggerScenarios":"head writes lines/bytes to stdout and the write fails — typically EPIPE when stdout is piped into a consumer that exits early (e.g., `head big | head -1` chains), or ENOSPC when stdout is redirected to a full disk/device.","commonSituations":"Piping head into commands like `less` then quitting, `| head` in scripts where downstream commands close the pipe, redirecting output to a full filesystem or a closed socket.","solutions":["Ignore it if caused by a downstream `head`/`q` closing the pipe — this is expected SIGPIPE/EPIPE behavior; ensure your script tolerates non-zero exit","Check available space with `df` if stdout is redirected to a file and free space on the target filesystem","Verify the consuming process in the pipeline is still alive and reads the data","Handle SIGPIPE in the surrounding shell script (set -o pipefail considerations)"],"exampleFix":"// before\nhead -n 1000 huge.log > /mnt/full-disk/out.txt\n// after\ndf -h /mnt/full-disk   # ensure space first\nhead -n 1000 huge.log > /mnt/full-disk/out.txt || echo \"stdout write failed: $?\"","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"// treat EPIPE on stdout as benign in pipelines\nhead -n 100 huge.log | head -n 5\nif [ ${PIPESTATUS[0]} -ne 0 ]; then\n  case $(head -n 100 huge.log 2>&1 >/dev/null) in\n    *\"error writing stdout\"*) echo \"benign broken pipe\" ;;\n  esac\nfi","preventionTips":["Expect nonzero exits when head's output is consumed by something that exits early","Check `df` before redirecting large outputs to disk","Use `set +o pipefail` (or handle PIPESTATUS) for pipelines where early exit is intentional"],"tags":["head","stdout","broken-pipe","io"],"backgroundTag":"broken-pipe","analyzedSha":"325183372aaf86d8ae6feaf4f9548f74fe815102","analyzedAt":"2026-08-31T11:11:36.175Z","contentChangedAt":"2026-08-31T11:11:36.175Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}