{"record":{"id":"bad2ea4e8522b017","repo":"rtk-ai/rtk","slug":"failed-to-relay-stdin","errorCode":null,"errorMessage":"Failed to relay stdin: {}","messagePattern":"Failed to relay stdin: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmds/system/pipe_cmd.rs","lineNumber":259,"sourceCode":"}\n\nfn identity_filter(input: &str) -> String {\n    input.to_string()\n}\n\nfn apply_filter(filter_fn: fn(&str) -> String, input: &str) -> String {\n    std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| filter_fn(input))).unwrap_or_else(\n        |_| {\n            eprintln!(\"[rtk] warning: filter panicked — passing through raw output\");\n            input.to_string()\n        },\n    )\n}\n\npub fn run(filter_name: Option<&str>, passthrough: bool) -> Result<()> {\n    if passthrough {\n        std::io::copy(&mut std::io::stdin(), &mut std::io::stdout())\n            .map_err(|e| anyhow::anyhow!(\"Failed to relay stdin: {}\", e))?;\n        return Ok(());\n    }\n\n    let mut buf = String::new();\n    std::io::stdin()\n        .take((RAW_CAP + 1) as u64)\n        .read_to_string(&mut buf)\n        .map_err(|e| anyhow::anyhow!(\"Failed to read stdin: {}\", e))?;\n    if buf.len() > RAW_CAP {\n        anyhow::bail!(\"stdin exceeds {} byte limit\", RAW_CAP);\n    }\n\n    let filter_fn = match filter_name {\n        Some(name) => resolve_filter(name).ok_or_else(|| {\n            anyhow::anyhow!(\n                \"Unknown filter '{}'. Available: cargo-test, pytest, go-test, go-build, \\\n                 ctest, tsc, vitest, grep, rg, find, fd, git-log, git-diff, git-status, \\\n                 log, mypy, ruff-check, ruff-format, sqlfluff-lint, prettier, phpunit, pest, \\","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/rtk-ai/rtk/blob/0924356b4caba4989607227b7c8824d3d8098719/src/cmds/system/pipe_cmd.rs#L241-L277","documentation":"`rtk pipe` (with passthrough mode) relays everything from stdin to stdout using `std::io::copy`. If the copy from stdin fails — broken pipe on stdout, I/O error on the input descriptor — pipe_cmd.rs:259 wraps the io::Error into `anyhow!(\"Failed to relay stdin: {}\", e)`. It signals that raw stdin relaying was interrupted by an OS-level I/O failure.","triggerScenarios":"Calling `rtk pipe --passthrough` (passthrough=true) while `std::io::copy(&mut stdin, &mut stdout)` fails: stdout consumer has exited/closed the pipe (EPIPE), or reading stdin hits an I/O error. Raised from `pipe_cmd::run` at src/cmds/system/pipe_cmd.rs:259.","commonSituations":"Piping into `rtk pipe --passthrough | head -n1` so head closes the pipe early; terminal/SSH session disconnecting mid-stream; redirected stdin from a file or device that errors during read.","solutions":["Ensure the downstream consumer reads all input before exiting (avoid `| head` truncation), or tolerate the error as expected SIGPIPE behavior.","Check that stdin is a readable, valid source (not a closed/unusable descriptor) before invoking `rtk pipe --passthrough`.","Retry the pipeline; if it persists without a downstream early-exit, inspect disk/terminal health and report an rtk bug."],"exampleFix":"// before (shell)\ncat big.log | rtk pipe --passthrough | head -n 5\n\n// after (shell)\ncat big.log | rtk pipe --passthrough | sed -n '1,5p;$p'\n// or capture full output before truncating\nrtk pipe --passthrough < big.log > out.txt && head -n 5 out.txt","handlingStrategy":"try-catch","validationCode":"# ensure downstream consumer will keep reading\ncommand -v head >/dev/null && echo 'avoid early-exiting consumers like `| head`'","typeGuard":null,"tryCatchPattern":"if ! cat data | rtk pipe --passthrough > out.txt 2>err.txt; then\n  grep -q 'Failed to relay stdin' err.txt && echo 'downstream closed early; retry without truncation'\nfi","preventionTips":["Do not pipe into consumers that exit early (`head`, `timeout`) in passthrough mode.","Keep terminal/SSH sessions alive during long relays.","Redirect to a file when downstream truncation is likely."],"tags":["stdin","pipe","io-error","broken-pipe","passthrough"],"backgroundTag":"broken-pipe","analyzedSha":"0924356b4caba4989607227b7c8824d3d8098719","analyzedAt":"2026-09-19T19:25:29.612Z","contentChangedAt":"2026-09-19T19:25:29.612Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}