Repository navigation
Conversation
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.
| 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(), || { |
There was a problem hiding this comment.
Warning · Captured-output test misses a broken Windows CLI detach gate
The fixture calls prepare_check with its own may_detach() result, bypassing on_invocation.
The test still passes if the CLI always allows discovery, leaving redirected Windows callers vulnerable to waiting for the checker.
Run the actual CLI with captured output and an empty update cache, then assert that no discovery attempt is recorded.
There was a problem hiding this comment.
Accepted. Added captured_windows_invocation_leaves_discovery_to_a_console_run in tests/update.rs: it runs the real binary with captured output and an empty cache and asserts no update.json is written. I checked it: with on_invocation changed on purpose to always allow the helper, the test fails (and that run took 2.1 s, the bug itself); restored, it passes. It is Windows-only because on other platforms the real binary would start a network check.
There was a problem hiding this comment.
Still present at ac50182: Proofline found this issue again after it was marked fixed. It is now at src/update/tests.rs:129-136.
There was a problem hiding this comment.
Keeping this as is. This unit test calls prepare_check directly on purpose: it checks one rule ("only detach decides") and has to inject a fake launcher, which on_invocation cannot do. The gap this finding was about (nothing exercised the real on_invocation path) is covered by captured_windows_invocation_leaves_discovery_to_a_console_run in tests/update.rs, which runs the real binary. I checked it: with on_invocation changed on purpose to always allow the helper, that test fails (and the run takes 2.1 s, the bug itself).
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.
| 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(), || { |
There was a problem hiding this comment.
Warning · The Windows fixture misses single-stream redirection regressions
The fixture pipes both streams, so its Windows assertions still pass if may_detach checks only one stream or replaces AND with OR.
Discovery could launch with one Windows output stream redirected without failing the test.
Test both mixed terminal states and assert that neither permits discovery.
There was a problem hiding this comment.
Accepted, good catch: the common real case is mixed. $o = proofline ... captures only stdout while stderr stays on the console. The rule now lives in a pure detachable(windows, stdout_is_console, stderr_is_console) function, and the new test windows_detaches_only_when_both_output_streams_are_consoles checks all four combinations. A pure function is used because a test cannot reliably give a child process a real console. I checked it: with OR, stdout-only and stderr-only versions put in on purpose, all old tests still passed and the new test failed each time. Fixed in ac50182.
| .output() | ||
| .unwrap(); | ||
| assert!(version.status.success()); | ||
| assert!(!directory.path().join("proofline/update.json").exists()); |
There was a problem hiding this comment.
Warning · Windows capture test passes even if version skips the updater
The Windows test checks only that --version succeeds and leaves no update.json; both checks pass if on_invocation moves after clap’s early exit.
The test would miss a regression that bypasses the detach decision entirely.
Also assert that proofline/update.lock exists in the fresh cache directory.
There was a problem hiding this comment.
Accepted. "No update.json" alone cannot tell "decided not to start the helper" from "never decided", so the test now also asserts that update.lock exists (the lock is created before the decision). I checked it: with on_invocation made to skip --version on purpose, the old test passed and the new assertion fails; restored, it passes. Fixed in ac50182.
There was a problem hiding this comment.
Still present at ac50182: Proofline found this issue again after it was marked fixed.
There was a problem hiding this comment.
Keeping this as is: it is already covered. The test now asserts that update.lock exists, and main.rs calls update::on_invocation() before Cli::try_parse() (src/main.rs:13), so before clap handles --version and exits. I checked it: with on_invocation made to skip --version on purpose, the test fails on the update.lock assertion; restored, it passes.
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.
| 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) { |
There was a problem hiding this comment.
Warning · Update docs omit Windows deferral when either output stream is redirected
The on_invocation comment says discovery continues in a child, but this change defers even due checks on Windows when either output stream is not a terminal.
Readers may expect a redirected invocation to start a check that it actually skips.
Document that due checks run in a child only when detachment is allowed, and that Windows requires both output streams to be terminals.
There was a problem hiding this comment.
Accepted. The on_invocation comment now says a due check runs in a child only when may_detach allows it, and that on Windows both stdout and stderr must be consoles, so a redirected run leaves the check to a later one. Fixed in a301ee2.
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.
| #[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"); |
There was a problem hiding this comment.
Warning · Deferral test misses due checks with a stale cache
The deferral test passes detach = false only with a missing cache, so it still passes if deferral works only when the cache is missing.
A redirected run could launch discovery and refresh checked on a stale cache without failing the test.
Seed a valid cache older than a day and assert that a non-detachable call neither launches discovery nor changes checked.
Details
The subprocess fixtures also start with empty caches; the integration test with a populated cache initializes it with the current timestamp, so the freshness check still prevents discovery under this regression.
Also at:
src/update/tests.rslines 338 to 344tests/update.rslines 5 to 22
| #[cfg(windows)] | ||
| #[test] | ||
| fn captured_windows_invocation_leaves_discovery_to_a_console_run() { | ||
| let directory = tempfile::tempdir().unwrap(); |
There was a problem hiding this comment.
Warning · CLI test misses discovery being disabled for every invocation
The captured Windows test still passes if on_invocation supplies false instead of may_detach(): the scheduler creates the lock before returning without recording an attempt.
Background discovery could stop on all CLI invocations without this test failing.
Add a real CLI test that starts with no cache, allows detachment, and asserts that an attempt is recorded.
Details
The lock assertion catches skipping the scheduler, not passing the wrong policy. The helper tests and subprocess fixture bypass the production call site, and the other CLI test seeds a fresh cache.
A person dismissed this finding earlier. It is posted again because it cites new evidence: tests/update.rs lines 7–21 (not cited before).
| 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) |
There was a problem hiding this comment.
Warning · The Windows fixture can miss a discovery launch
The fixture checks for HTTP requests after the invocation exits, not whether its launcher ran.
An unintended detached worker can pass the test if it records no attempt and hasn't reached the server when the counter is sampled.
Record a synchronous marker in the fixture's launch callback and assert it was never called.
| assert!(directory.path().join("proofline/update.lock").exists()); | ||
| assert!(!directory.path().join("proofline/update.json").exists()); |
There was a problem hiding this comment.
Warning · Captured Windows test misses discovery being disabled for console runs
The Windows CLI test only exercises piped output. If on_invocation passes false instead of may_detach(), it still creates update.lock and skips writing update.json, satisfying both assertions.
Due update checks could stop running for eligible console invocations without failing this test.
Add a Windows console-backed CLI case that starts without a cache and asserts that update.json records an attempt.
A person dismissed this finding earlier. It is posted again because it cites new evidence: tests/update.rs lines 10–21 (changed since).
cf07cef to
a301ee2
Compare
The problem in one sentence
On Windows, when a script or CI job reads
proofline's output, every command can take 1–3 secondslonger than it should, because a hidden helper process that checks for updates keeps the output
"open" after
prooflineitself has already finished.See it yourself (PowerShell, Windows)
$o = …captures the output, the way a script or CI step does. Printing--versiontakes about10 ms; the rest is waiting.
Why it happens, step by step
Once a day, the CLI checks for a new release in the background.
update::on_invocationstarts a second copy of itself (
proofline __update-check). That helper asksproofline.shforthe latest version and writes the answer to a small cache file. The main command does not wait
for it; the idea is that the helper finishes quietly on its own.
Capturing output uses a pipe, and the reader waits until the pipe is closed. When PowerShell
runs
$o = proofline --version, it connectsproofline's output to a pipe and keeps reading untilthe pipe is closed. A pipe only closes when every process holding its writing end has let go.
On Windows, a new process can receive copies of its parent's handles. A handle is the
operating system's "ticket" that lets a process use a file or a pipe. Rust's
Command::spawnstarts processes on Windows with
bInheritHandles = TRUE, which means: give the new process acopy of every ticket the parent has marked as inheritable. The parent's stdout/stderr pipe
tickets are inheritable (they were inherited from PowerShell in the first place). The helper is
started with
Stdio::null(), which only decides where the helper's own output goes; it doesnot stop the extra copies from being handed over. This is a known Rust issue:
windows: when creating process with redirected stdio, make parent's std handles not inheritable rust-lang/rust#54760.
So the helper is still holding the pipe after
prooflineexits.proofline --versionfinishes in about 10 ms, but the pipe stays open until the helper's network request is done (up
to its 3-second timeout, longer if DNS is slow). PowerShell, a CI runner, or an editor reading
the output waits all that time.
Linux and macOS don't have this problem. There, Rust marks its own file descriptors
close-on-exec, so the helper doesn't keep the pipe. That's also why CI didn't notice: CI runs
cargo testonly on Ubuntu.The repository's own test already catches this. On Windows,
update::tests::detached_discovery_is_serialized_and_notices_arrive_on_the_next_invocationfailsevery time (it times out at
src/update/tests.rs:291). Its comment even says"Inherited output pipes would fail this too."
Who is affected
windows-*runners). Each job starts with an emptycache, so every
prooflinecommand in the job pays the extra seconds.$(...), pipes,editors and agents that talk to
proofline mcp. There it happens once a day (the cacheremembers the last check for 24 hours).
Why not the "obvious" fix
The direct fix is to tell Windows not to hand those pipe tickets to the helper
(
SetHandleInformation(..., HANDLE_FLAG_INHERIT, 0)on the three standard handles). I tried it:the failing test then passes in 0.09 s. But calling that Windows function needs
unsafecode, andthis crate sets
unsafe_code = "forbid"(Cargo.toml:67), so that's not an option. I alsochecked the alternatives:
CommandExt::inherit_handles), buton the pinned toolchain (1.100) it is still unstable (
windows_process_extensions_inherit_handles,#146407), so stable Rust can't use it.
What this PR changes
Start the background check on Windows only when nobody can be left waiting, which is when the
command's output goes straight to a console window that a person is looking at. A console isn't a
pipe, so nothing waits for it to "close".
src/update.rs, newmay_detach(): returnstrueon Linux/macOS (no change there). OnWindows it returns
trueonly if both stdout and stderr are consoles (is_terminal()).src/update.rs,prepare_check(..., detach, ...): whendetachisfalse, return beforewriting anything to the cache. The order matters. The cache records "I checked today". If a
script run wrote that note without actually checking, the next run in a console would think the
check was done and skip it, and the person wouldn't hear about a new release that day.
Why nothing is lost
The "a new version is available" message is only ever shown when output goes to a console
(
suggestion()returnsNoneotherwise). So a piped run on Windows never showed it anyway. Nowit just doesn't start the check either, and the next console run checks as before.
How I tested it (Windows 11, Rust 1.100.0)
Tests, failing first, then passing:
may_detach(), withtheir output piped to the test, as in a real script. Before: times out after 5 s. After:
passes. On Windows it checks the new behaviour: the processes exit quickly, no update request is
made, and nothing is written to the cache. On Linux/macOS the original scenario runs unchanged.
an_invocation_that_cannot_detach_leaves_discovery_to_the_next_one(all platforms):when the check can't be started, the launcher is never called and the cache stays empty. The
next run that can start it does start it and records it, even when its output isn't a console
(only
detachdecides). Before the change it failed because the attempt was recorded.windows_detaches_only_when_both_output_streams_are_consoles(all platforms): the ruleis a small function,
detachable(), so the test can check all four console/pipe combinations.On Windows, any redirected stream blocks the helper. That includes the common
$o = proofline …case, where only stdout is captured and stderr stays on the console. On otherplatforms, every combination allows it.
captured_windows_invocation_leaves_discovery_to_a_console_run(tests/update.rs,Windows only): runs the real
proofline --versionbinary with its output captured and an emptycache. It checks that
update.lockexists (so the update logic really ran and made its decision)and that no
update.jsonis written (so the decision was "don't start the helper"). This coversthe wiring inside
on_invocation, not justprepare_check.The real binary:
$o = proofline --version, empty cachePROOFLINE_NO_UPDATE_CHECK=1(baseline)After the change, no cache file was written in the piped runs, as intended. (The very first
launch of a freshly built
.exesometimes took ~1 s before and after; that's Windows scanning anew file, not the checker.)
A console run still checks for updates:
Start-Process proofline --version(which opens itsown console window) with an empty cache wrote the cache file within 5 s
(
{"checked":…,"latest":"0.4.7"}).Checks:
cargo fmt --checkpasses.cargo clippy --locked --all-targets -- -D warningsreports nothing in the changed code. Itdoes report two lints that already fail on
mainin Windows-only test code(
src/agent/tests.rs:526,:745). I left those alone to keep this PR focused.cargo test --locked --no-fail-fast: 147 passed, 7 failed. The 7 also fail onmainandare Windows test-setup problems unrelated to this change: 5 in
tests/mcp.rs(env_clear()removes
SYSTEMROOT),tests/agent_install.rs:77(short vs. long temp path) andtests/local_patch.rs:275(a file name Windows doesn't allow).mainhas 8 failures: these 7plus the update test this PR fixes.
Not tested locally: Linux. On Linux/macOS
may_detach()is alwaystrue, so the code path isthe same as before; the Ubuntu CI job checks it.
Follow-up (separate issue, not this PR)
Run
cargo clippyandcargo teston the Windows CI job. Both would have caught this.