{"record":{"id":"e0bd06ae4efe58cf","repo":"nikivdev/code","slug":"pbcopy-exited-with-status","errorCode":null,"errorMessage":"pbcopy exited with status {}","messagePattern":"pbcopy exited with status (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ai.rs","lineNumber":14167,"sourceCode":"/// Copy text to system clipboard.\nfn copy_to_clipboard(text: &str) -> Result<()> {\n    if std::env::var(\"FLOW_NO_CLIPBOARD\").is_ok() {\n        return Ok(());\n    }\n    #[cfg(target_os = \"macos\")]\n    {\n        let mut child = Command::new(\"pbcopy\")\n            .stdin(Stdio::piped())\n            .spawn()\n            .context(\"failed to spawn pbcopy\")?;\n\n        if let Some(stdin) = child.stdin.as_mut() {\n            stdin.write_all(text.as_bytes())?;\n        }\n\n        let status = child.wait()?;\n        if !status.success() {\n            bail!(\"pbcopy exited with status {}\", status);\n        }\n    }\n\n    #[cfg(target_os = \"linux\")]\n    {\n        // Try xclip first, then xsel\n        let result = Command::new(\"xclip\")\n            .arg(\"-selection\")\n            .arg(\"clipboard\")\n            .stdin(Stdio::piped())\n            .spawn();\n\n        let mut child = match result {\n            Ok(c) => c,\n            Err(_) => Command::new(\"xsel\")\n                .arg(\"--clipboard\")\n                .arg(\"--input\")\n                .stdin(Stdio::piped())","sourceCodeStart":14149,"sourceCodeEnd":14185,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/ai.rs#L14149-L14185","documentation":"The clipboard helper on macOS spawns `pbcopy` (via the platform-specific cfg branch), writes the text to its stdin, and checks the child's exit status. If pbcopy terminates with a nonzero status, the code bails with the exit status. This indicates the copy-to-clipboard operation genuinely failed even though pbcopy was found and launched.","triggerScenarios":"pbcopy fails to access the macOS pasteboard — e.g. running over SSH without a GUI session, in a headless context, or in sandboxes that deny pasteboard access; pbcopy killed by a signal (status reported as signal exit).","commonSituations":"SSH into a Mac and run the copy command — no Aqua session owns the pasteboard; running inside restrictive CI or container contexts; system clipboard daemon issues after long uptimes.","solutions":["Run the command from a local GUI session (Terminal.app/iTerm) rather than SSH, or use `pbcopy` with reattach-to-user-namespace workarounds where applicable","Test manually: `echo test | pbcopy && pbpaste` to confirm the pasteboard works outside the tool","Check the printed status for signal-exit codes (e.g. 9/137) pointing at OOM or kill policies, and free resources/adjust limits","As a workaround, print the text to stdout and copy it manually"],"exampleFix":"// before (SSH session)\n$ myapp ai copy last\nError: pbcopy exited with status 1\n// after (local GUI shell)\n$ echo test | pbcopy && pbpaste\ntest\n$ myapp ai copy last   # succeeds","handlingStrategy":"fallback","validationCode":"use std::process::Command;\nfn pbcopy_works() -> bool {\n    Command::new(\"pbcopy\").stdin(std::process::Stdio::piped()).spawn().is_ok()\n}\nif !pbcopy_works() { eprintln!(\"clipboard unavailable; printing to stdout instead\"); }","typeGuard":null,"tryCatchPattern":"match copy_to_clipboard(text) {\n    Err(e) if e.to_string().starts_with(\"pbcopy exited\") => {\n        eprintln!(\"clipboard failed ({e}); falling back to stdout\");\n        println!(\"{text}\");\n    }\n    Err(e) => return Err(e),\n    Ok(()) => {}\n}","preventionTips":["Run copy commands from a local GUI session, not over SSH without a pasteboard owner","Sanity-check the clipboard with `echo t | pbcopy && pbpaste` when setting up a new Mac workflow","Avoid running interactive clipboard flows inside CI/headless jobs; capture output to a file instead","Treat nonzero exit statuses (9/137) as resource/kill issues and check system logs if they recur"],"tags":["clipboard","macos","process-exit","subprocess"],"backgroundTag":"clipboard-command-failed","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}