{"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":"std::io::Error","httpStatus":null,"severity":"error","filePath":"src/uu/head/src/head.rs","lineNumber":209,"sourceCode":"            verbose: matches.get_flag(options::VERBOSE),\n            line_ending: LineEnding::from_zero_flag(matches.get_flag(options::ZERO)),\n            presume_input_pipe: matches.get_flag(options::PRESUME_INPUT_PIPE),\n            mode: Mode::from(matches)?,\n            // #[allow(clippy::unwrap_used, reason = \"clap provides '-' by default\")] <https://github.com/rust-lang/rust/issues/15701>\n            files: matches\n                .get_many::<OsString>(options::FILES)\n                .unwrap()\n                .cloned()\n                .collect(),\n        };\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/92995feca29e324a7c559ede91db81c32d9f5d59/src/uu/head/src/head.rs#L191-L227","documentation":"`head` wraps any `io::Error` raised while writing parsed content to stdout, replacing the message with a localized `head-error-writing-stdout` that embeds the stripped errno text, while preserving the original error kind. It tells the user the failure happened at the output stage, not while reading input.","triggerScenarios":"Writing the first N lines/bytes to stdout fails, e.g. `head big.txt | head -c 100 > /dev/full`, a closed pipe (`head f | true`), or a full disk / EPIPE on the terminal.","commonSituations":"Piping into `head`-terminated pipelines where the downstream reader exits early (SIGPIPE handling), redirecting to `/dev/full`, or writing to a read-only/closed descriptor.","solutions":["Check the shell pipeline: ensure the consumer of head's output is alive and reading","Redirect to a writable destination (`df -h` to confirm free space; test with `> /tmp/out`)","If SIGPIPE from early reader exit is expected, ignore it (exit 141 is normal)","Run without redirection to verify stdout itself works"],"exampleFix":"// before\nhead -n 100 log.txt > /dev/full\n// after\nhead -n 100 log.txt > output.txt","handlingStrategy":"try-catch","validationCode":"[ -w \"$(dirname \"$out\")\" ] && df -h --output=avail \"$out\" | awk 'NR==2 && $1+0>0' || echo 'stdout target not writable'","typeGuard":"fn is_stdout_writable() -> bool {\n    use std::io::Write;\n    std::io::stdout().flush().is_ok()\n}","tryCatchPattern":"match result {\n    Err(e) if e.kind() == ErrorKind::BrokenPipe => { /* consumer exited early: ignore */ }\n    Err(e) => { eprintln!(\"head: error writing stdout: {}\"); exit(1); }\n    Ok(()) => {}\n}","preventionTips":["Ensure pipeline consumers stay alive until head finishes","Avoid redirecting to /dev/full or full filesystems","Check downstream commands in CI pipelines for early exit","Remember exit code 141/EPIPE on pipes is often benign"],"tags":["stdout","pipe","head","io"],"backgroundTag":"broken-pipe","analyzedSha":"92995feca29e324a7c559ede91db81c32d9f5d59","analyzedAt":"2026-09-20T12:39:35.476Z","contentChangedAt":"2026-09-20T12:39:35.476Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}