Hmbown/CodeWhale · error

Failed to write to

Error message

Failed to write to {label}: {e}

What it means

This error is thrown by write_text_with_stdin_command in crates/tui/src/tui/clipboard.rs when writing the clipboard text to the spawned clipboard helper's (e.g. pbcopy, set-clipboard) stdin pipe fails. The library spawns the helper and pipes the text in; any I/O error on that pipe (broken pipe, closed fd, EINTR exhaustion) is wrapped with this message. It means the text never reached the clipboard helper.

Solutions

  1. Retry the clipboard write; transient EPIPE/partial writes are often one-shot.
  2. Verify the helper binary (pbcopy / set-clipboard) actually runs standalone: echo hi | pbcopy.
  3. Check clipboard size — very large text increases the chance the child exits before reading; reduce or chunk the payload.
  4. Fall back to another clipboard backend (the library's other write_text_with_* strategies).
  5. Confirm no sandbox/seccomp profile is killing spawned children mid-run.

Example fix

// before
stdin.write_all(text.as_bytes()).map_err(|e| anyhow::anyhow!("Failed to write to {label}: {e}"))?;
// after
match stdin.write_all(text.as_bytes()) {
    Ok(()) => {}
    Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => {
        return write_text_fallback(text); // try next backend
    }
    Err(e) => return Err(anyhow::anyhow!("Failed to write to {label}: {e}")),
}
Defensive patterns

Strategy: fallback

Validate before calling

// before writing: ensure the helper can run and accept stdin
let ok = Command::new("pbcopy").arg("-help").stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).status().map(|s| s.success()).unwrap_or(false);
if !ok { eprintln!("clipboard helper unavailable; using fallback"); }

Try / catch

match write_text_with_pbcopy(text) {
    Err(e) if e.to_string().contains("Failed to write") => {
        // child died mid-write (broken pipe) — try next backend
        write_text_with_osc52(text).unwrap_or_else(|e2| eprintln!("clipboard write failed: {e2:#}"));
    }
    Err(e) => eprintln!("clipboard write failed: {e:#}"),
    Ok(()) => {}
}

Prevention

When it happens

Trigger: Calling write_text_with_pbcopy or write_text_with_set_clipboard when the child process exits before consuming the text (e.g. the helper crashed or was killed), the pipe fd was closed, or a transient EINTR/partial-write condition occurs during stdin.write_all.

Common situations: macOS pbcopy killed by the environment or sandbox; SSH session where the clipboard daemon died; helper binary present at spawn but crashing on startup while large text is still being written (classic EPIPE); resource limits closing descriptors.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/baca2d9b39c33267. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/tui/clipboard.rs:696

#[cfg(all(any(target_os = "macos", target_os = "windows"), not(test)))]
fn write_text_with_stdin_command(
    program: &str,
    args: &[&str],
    text: &str,
    label: &str,
) -> Result<()> {
    let mut child = Command::new(program)
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map_err(|e| anyhow::anyhow!("Failed to run {label}: {e}"))?;
    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(text.as_bytes())
            .map_err(|e| anyhow::anyhow!("Failed to write to {label}: {e}"))?;
    }
    let _ = std::thread::Builder::new()
        .name("clipboard-wait".to_string())
        .spawn(move || {
            let _ = child.wait();
        });
    Ok(())
}

#[cfg(all(target_os = "linux", not(target_env = "ohos"), not(test)))]
fn write_text_with_wlcopy(text: &str) -> Result<()> {
    write_text_with_wlcopy_using_argv("wl-copy", text)
}

#[cfg(all(target_os = "linux", not(target_env = "ohos"), not(test)))]
fn read_text_with_wlpaste() -> Result<String> {
    read_text_with_wlpaste_using_argv("wl-paste")
}

View on GitHub (pinned to 433685b202)