From 1443801416bca25016f36e94da45cf9abc21821a Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Sun, 22 Nov 2026 21:59:59 +0500 Subject: [PATCH 1/4] Windows: don't make scripts wait for the background update check Once a day the CLI starts a hidden helper (proofline __update-check) to see whether a newer release exists, and doesn't wait for it. On Windows, Rust starts that helper with bInheritHandles=TRUE, so it also gets a copy of the pipe the parent's output goes into (rust-lang/rust#54760). When a script, CI step or editor reads proofline's output, it keeps waiting until the helper's network request finishes, which adds 1-3 s to the command. Linux and macOS are not affected. The direct fix (clearing the inherit flag) needs unsafe code, which this crate forbids, and std's safe option (inherit_handles) is still unstable. So on Windows the check now starts only when stdout and stderr are both consoles, where nothing waits for the pipe to close. The update notice is only shown in a console anyway, so nothing visible is lost. When the check can't start, prepare_check returns before writing the cache. Otherwise a script run would record "checked today" without checking, and the next console run would skip the check. --- src/update.rs | 16 ++++++++++++++-- src/update/tests.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/update.rs b/src/update.rs index d001d62..5a1e99a 100644 --- a/src/update.rs +++ b/src/update.rs @@ -99,7 +99,7 @@ pub fn on_invocation() { return; }; let interactive = std::io::stderr().is_terminal() && std::io::stdout().is_terminal(); - if let Ok(Some(notice)) = prepare_check(&path, &args, interactive, start_check) { + if let Ok(Some(notice)) = prepare_check(&path, &args, interactive, may_detach(), start_check) { crate::say(¬ice); } else { // Missing or failed advisory discovery never changes the command result. @@ -150,6 +150,7 @@ fn prepare_check( path: &Path, args: &[std::ffi::OsString], interactive: bool, + detach: bool, launch: impl FnOnce() -> anyhow::Result<()>, ) -> anyhow::Result> { let directory = path.parent().context("update cache has no directory")?; @@ -171,7 +172,8 @@ fn prepare_check( latest: None, }); let notice = suggestion(&cache, args, interactive); - if cache.checked <= now() && now().saturating_sub(cache.checked) < DAY { + // Without a detachable child, leave the attempt to a later invocation. + if !detach || (cache.checked <= now() && now().saturating_sub(cache.checked) < DAY) { return Ok(notice); } // Record the attempt first, so an offline machine does not fork on every command. @@ -188,6 +190,16 @@ fn prepare_check( Ok(notice) } +/// Whether a discovery child can outlive this invocation unnoticed. A Windows +/// child inherits every inheritable handle (rust-lang/rust#54760), including +/// this process's redirected stdout and stderr, so whoever reads the output +/// (`$(...)`, a pipe, a CI runner, an MCP host) would wait for the checker to +/// exit. Std limits inheritance only through unstable or unsafe APIs, so on +/// Windows discovery waits for an invocation whose output is a console. +fn may_detach() -> bool { + !cfg!(windows) || (std::io::stdout().is_terminal() && std::io::stderr().is_terminal()) +} + fn checker_command(executable: &Path) -> Command { let mut command = Command::new(executable); command.env_clear(); diff --git a/src/update/tests.rs b/src/update/tests.rs index d7387c5..976d77f 100644 --- a/src/update/tests.rs +++ b/src/update/tests.rs @@ -92,7 +92,7 @@ fn installer_directory_and_bounded_cache() { ) .unwrap(); // A fresh cached result schedules no child, and survives the next invocation. - prepare_check(&cache, &["--json".into()], true, || { + prepare_check(&cache, &["--json".into()], true, true, || { anyhow::bail!("a fresh cache must not launch discovery") }) .unwrap(); @@ -101,6 +101,28 @@ fn installer_directory_and_bounded_cache() { assert!(read_cache(&cache).is_err()); } +#[test] +fn an_invocation_that_cannot_detach_leaves_discovery_to_the_next_one() { + let directory = tempfile::tempdir().unwrap(); + let cache = directory.path().join("update.json"); + // `prepare_check` ignores launch errors, so only a panic proves no launch. + let notice = prepare_check(&cache, &["status".into()], false, false, || { + unreachable!("an invocation that cannot detach must not launch discovery") + }) + .unwrap(); + assert!(notice.is_none()); + // No attempt is recorded, so the next invocation that can detach still checks. + assert!(read_cache(&cache).is_err()); + let mut launched = false; + prepare_check(&cache, &["status".into()], true, true, || { + launched = true; + Ok(()) + }) + .unwrap(); + assert!(launched); + assert!(read_cache(&cache).is_ok()); +} + #[tokio::test] async fn discovery_rejects_bad_bounded_and_offline_responses() { use axum::{Router, routing::get}; @@ -229,7 +251,10 @@ async fn detached_discovery_is_serialized_and_notices_arrive_on_the_next_invocat .env(CACHE, &path) .env(ADDRESS, address); let notice = tokio::task::spawn_blocking(move || { - prepare_check(&path, &["status".into()], true, || spawn_check(worker)) + // Output is piped to the test, as under `$(...)` or a CI runner. + prepare_check(&path, &["status".into()], true, may_detach(), || { + spawn_check(worker) + }) }) .await .unwrap() @@ -297,6 +322,14 @@ async fn detached_discovery_is_serialized_and_notices_arrive_on_the_next_invocat .contains("run 'proofline update'") ); } + if cfg!(windows) { + // A Windows child would hold the piped output open, so these + // invocations neither start discovery nor record an attempt. + assert_eq!(received.load(Ordering::SeqCst), 0); + assert!(read_cache(&cache).is_err()); + server.abort(); + return; + } tokio::time::timeout(Duration::from_secs(5), async { while received.load(Ordering::SeqCst) == 0 { tokio::time::sleep(Duration::from_millis(10)).await; From 836ab701862013c068e3aec26e6d4af30061e130 Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Sun, 22 Nov 2026 22:53:27 +0500 Subject: [PATCH 2/4] Cover the update-check gate where review found test gaps Proofline's review pointed out two things the tests didn't check: 1. The unit test only started the helper with interactive=true. If a later change skipped the update check whenever output is redirected, on every platform, the test would still pass. The call that should start the helper now uses interactive=false, so the test proves that only `detach` decides. 2. The subprocess test passes may_detach() to prepare_check itself, so it never checked that on_invocation does. A new Windows-only test runs the real proofline binary with its output captured (like a script) and an empty cache, and checks that no update.json is written. I checked that each test catches its mistake: with the mistake put in on purpose, the test failed; with it removed, it passed. --- src/update/tests.rs | 3 ++- tests/update.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/update/tests.rs b/src/update/tests.rs index 976d77f..1232dee 100644 --- a/src/update/tests.rs +++ b/src/update/tests.rs @@ -114,7 +114,8 @@ fn an_invocation_that_cannot_detach_leaves_discovery_to_the_next_one() { // No attempt is recorded, so the next invocation that can detach still checks. assert!(read_cache(&cache).is_err()); let mut launched = false; - prepare_check(&cache, &["status".into()], true, true, || { + // Only `detach` decides: a redirected (non-interactive) run still checks. + prepare_check(&cache, &["status".into()], false, true, || { launched = true; Ok(()) }) diff --git a/tests/update.rs b/tests/update.rs index 0a165b0..cbe5a33 100644 --- a/tests/update.rs +++ b/tests/update.rs @@ -2,6 +2,23 @@ use std::process::Command; use std::time::{SystemTime, UNIX_EPOCH}; +/// A Windows discovery child would inherit the captured output pipe and keep +/// the reader waiting, so a captured run with no cache records no attempt. +#[cfg(windows)] +#[test] +fn captured_windows_invocation_leaves_discovery_to_a_console_run() { + let directory = tempfile::tempdir().unwrap(); + // `output()` pipes stdout and stderr, as `$(...)` or a CI step does. + let version = Command::new(env!("CARGO_BIN_EXE_proofline")) + .arg("--version") + .env_remove("PROOFLINE_NO_UPDATE_CHECK") + .env("LOCALAPPDATA", directory.path()) + .output() + .unwrap(); + assert!(version.status.success()); + assert!(!directory.path().join("proofline/update.json").exists()); +} + #[test] fn cached_update_preserves_schema_help_and_source_installation() { let directory = tempfile::tempdir().unwrap(); From ac50182bbd964c8fef0f72a45de2d97680d06147 Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Sun, 22 Nov 2026 23:26:13 +0500 Subject: [PATCH 3/4] Test each output stream and that version runs the gate Proofline's second review found two more things the tests didn't check: 1. The Windows rule is "start the helper only if stdout AND stderr are both consoles". The subprocess test pipes both streams, so it would still pass if the rule checked only one stream or used OR. That mixed case is the common one: `$o = proofline ...` captures only stdout, while stderr stays on the console. The rule now lives in a small function, detachable(), and a new test checks all four console/pipe combinations on Windows (and that other platforms always allow the helper). 2. The Windows binary test would also pass if `--version` stopped running the update logic at all, since then no update.json is written either. It now also checks that update.lock exists: the lock is created before the decision, so it proves the decision ran. Each new check was confirmed by putting the mistake in on purpose (OR instead of AND, one stream only, skipping --version): the test failed, and passed again once the mistake was removed. --- src/update.rs | 12 +++++++++++- src/update/tests.rs | 12 ++++++++++++ tests/update.rs | 2 ++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/update.rs b/src/update.rs index 5a1e99a..be89805 100644 --- a/src/update.rs +++ b/src/update.rs @@ -197,7 +197,17 @@ fn prepare_check( /// exit. Std limits inheritance only through unstable or unsafe APIs, so on /// Windows discovery waits for an invocation whose output is a console. fn may_detach() -> bool { - !cfg!(windows) || (std::io::stdout().is_terminal() && std::io::stderr().is_terminal()) + detachable( + cfg!(windows), + std::io::stdout().is_terminal(), + std::io::stderr().is_terminal(), + ) +} + +/// One redirected stream is enough to hold a reader: `$o = proofline ...` +/// captures only stdout while stderr stays on the console. +const fn detachable(windows: bool, stdout_is_console: bool, stderr_is_console: bool) -> bool { + !windows || (stdout_is_console && stderr_is_console) } fn checker_command(executable: &Path) -> Command { diff --git a/src/update/tests.rs b/src/update/tests.rs index 1232dee..9fcdce9 100644 --- a/src/update/tests.rs +++ b/src/update/tests.rs @@ -101,6 +101,18 @@ fn installer_directory_and_bounded_cache() { assert!(read_cache(&cache).is_err()); } +#[test] +fn windows_detaches_only_when_both_output_streams_are_consoles() { + // `$o = proofline ...` pipes stdout while stderr stays on the console. + assert!(!detachable(true, false, true)); + assert!(!detachable(true, true, false)); + assert!(!detachable(true, false, false)); + assert!(detachable(true, true, true)); + for (stdout, stderr) in [(false, false), (false, true), (true, false), (true, true)] { + assert!(detachable(false, stdout, stderr)); + } +} + #[test] fn an_invocation_that_cannot_detach_leaves_discovery_to_the_next_one() { let directory = tempfile::tempdir().unwrap(); diff --git a/tests/update.rs b/tests/update.rs index cbe5a33..e3150f9 100644 --- a/tests/update.rs +++ b/tests/update.rs @@ -16,6 +16,8 @@ fn captured_windows_invocation_leaves_discovery_to_a_console_run() { .output() .unwrap(); assert!(version.status.success()); + // The lock proves the decision ran; the missing cache proves it said no. + assert!(directory.path().join("proofline/update.lock").exists()); assert!(!directory.path().join("proofline/update.json").exists()); } From a301ee21d1984a586f59d1df69a166795c649ad1 Mon Sep 17 00:00:00 2001 From: Ahmar Ali Date: Sun, 22 Nov 2026 23:30:13 +0500 Subject: [PATCH 4/4] Say when on_invocation defers the update check The comment above on_invocation said the check always continues in a background child. Since this change, on Windows it only does when both stdout and stderr are consoles; a redirected run leaves the check to a later run. The comment now says so. --- src/update.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/update.rs b/src/update.rs index be89805..5d230af 100644 --- a/src/update.rs +++ b/src/update.rs @@ -81,7 +81,9 @@ fn write_cache(path: &Path, cache: &Cache) -> anyhow::Result<()> { Ok(()) } -/// Reads only local state; discovery continues in a child with closed streams. +/// Reads only local state. A due check continues in a child with closed +/// streams only when `may_detach` allows it: on Windows both stdout and stderr +/// must be consoles, so a redirected run leaves the check to a later one. /// Call before starting the runtime, including for help and version invocations. pub fn on_invocation() { if std::env::var_os("PROOFLINE_NO_UPDATE_CHECK").is_some_and(|value| value == "1") {