Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions crates/jcode-tui/src/tui/app/helpers/clipboard_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,70 @@ pub(crate) fn copy_via_clipboard_helper(program: &str, args: &[&str], text: &str
return false;
}

let deadline = std::time::Instant::now() + std::time::Duration::from_millis(150);
let deadline = std::time::Instant::now() + EARLY_FAILURE_WINDOW;
loop {
match child.try_wait() {
Ok(Some(status)) if status.success() => return true,
// Exited nonzero (e.g. xclip with no DISPLAY): let the next
// fallback try.
Ok(Some(_)) => return false,
Ok(Some(status)) => return status.success(),
Ok(None) => {
if std::time::Instant::now() >= deadline {
// Still running: the helper became the selection owner,
// which is the success case.
std::thread::spawn(move || {
let _ = child.wait();
});
return true;
break;
}
std::thread::sleep(std::time::Duration::from_millis(5));
std::thread::sleep(POLL_INTERVAL);
}
Err(_) => return false,
}
}

// The early window expired with the child still running, and "still
// running" is NOT proof that it took ownership. A helper that is going to
// fail has to actually be scheduled before it can exit, and on a loaded
// machine it may not have been yet: `false` does not need 150 ms to exit,
// but a runner at load 50 can take that long to run it at all. Treating
// that as ownership reports a successful copy that never happened, which is
// exactly how `helper_that_exits_nonzero_falls_through` failed on master CI
// (run 41547128038, job 125625340312):
//
// assertion failed: !copy_via_clipboard_helper("false", &[], "hello")
//
// So keep waiting for a bounded grace period and let the real exit status
// decide. Only a child that survives BOTH windows is treated as resident.
//
// The tradeoff is deliberate: a genuinely resident helper now costs up to
// RESIDENT_GRACE extra before the copy is reported, whereas before it was
// reported at the end of the early window. A silent false "copied" is worse
// than a bounded stall, and this only delays in the ambiguous case - a
// helper that fails still returns as soon as it actually exits.
let grace_deadline = std::time::Instant::now() + RESIDENT_GRACE;
loop {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: Duplicate poll loop — extract into a helper

The two polling blocks (early window at line 42, grace window at line 75) share identical structure. Factor them into a small helper such as poll_child_until(child, deadline) -> Option<ExitStatus> so a future change to one window (sleep cadence, deadline check, error handling) cannot silently diverge from the other.

match child.try_wait() {
Ok(Some(status)) => return status.success(),
Ok(None) => {
if std::time::Instant::now() >= grace_deadline {
break;
}
std::thread::sleep(POLL_INTERVAL);
}
Err(_) => return false,
}
}

// Alive across both windows: it became the selection owner.
std::thread::spawn(move || {
let _ = child.wait();
});
true
}

/// How long to poll for an early failure before the answer becomes ambiguous.
const EARLY_FAILURE_WINDOW: std::time::Duration = std::time::Duration::from_millis(150);

/// Extra grace once the poll expires, so a starved-but-doomed child still
/// reports its real status rather than being mistaken for a resident owner.
const RESIDENT_GRACE: std::time::Duration = std::time::Duration::from_millis(850);

/// Poll cadence while waiting.
const POLL_INTERVAL: std::time::Duration = std::time::Duration::from_millis(5);

/// Tests for the external clipboard-helper spawn path (issue #684). They use
/// ordinary coreutils instead of real clipboard tools so they pass on headless
/// CI: what matters is the contract (writes stdin, does not block on a
Expand Down
21 changes: 19 additions & 2 deletions crates/jcode-tui/src/tui/app/tests/scroll_copy_02/part_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,17 @@ fn test_click_on_inline_image_label_line_cycles_level() {
ImageExpandLevel::Large,
"first label click should expand Fit -> Large"
);
assert_eq!(app.status_notice(), Some("Image size: large".to_string()));
// The status line carries the new size and, when this click also attempts a
// clipboard copy, a result suffix ("· Image copied" / "· Could not copy
// image" - see navigation.rs). Clipboard availability is environmental: a
// headless CI runner has no clipboard at all, so the copy legitimately
// fails there. This test's subject is the size cycle, so assert the size it
// reports and let the copy outcome vary by host.
let notice = app.status_notice().unwrap_or_default();
assert!(
notice.starts_with("Image size: large"),
"expected the size cycle to be reported, got {notice:?}"
);

// Large and Full have identical geometry for this landscape image, so the
// redundant Full state is skipped and the next click returns to Fit.
Expand Down Expand Up @@ -1208,7 +1218,14 @@ fn test_real_draw_click_on_body_anchored_image_label_cycles_level() {
"clicking the rendered image label must cycle Fit -> Large \
(this is the exact path the user reported as broken)"
);
assert_eq!(app.status_notice(), Some("Image size: large".to_string()));
// Size first, copy result optional - same reasoning as the label-click test
// above: a headless runner has no clipboard, so the copy this click attempts
// fails there and the status carries "· Could not copy image".
let notice = app.status_notice().unwrap_or_default();
assert!(
notice.starts_with("Image size: large"),
"expected the size cycle to be reported, got {notice:?}"
);
}

/// The inline-image placeholder marker row must never reach the terminal as
Expand Down
Loading