Skip to content

Windows: don't make scripts wait for the background update check - #1

Open
Ahmar004 wants to merge 4 commits into
mainfrom
fix/windows-update-check-pipe-hang
Open

Ahmar004 wants to merge 4 commits into
mainfrom
fix/windows-update-check-pipe-hang

Conversation

@Ahmar004

@Ahmar004 Ahmar004 commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

The problem in one sentence

On Windows, when a script or CI job reads proofline's output, every command can take 1–3 seconds
longer than it should, because a hidden helper process that checks for updates keeps the output
"open" after proofline itself has already finished.

See it yourself (PowerShell, Windows)

# Use an empty cache folder, so the CLI thinks it hasn't checked for updates today.
$env:LOCALAPPDATA = (New-Item -ItemType Directory (Join-Path $env:TEMP ([guid]::NewGuid()))).FullName
Measure-Command { $o = proofline --version }    # 1.1–2.6 s on my machine
$env:PROOFLINE_NO_UPDATE_CHECK = '1'
Measure-Command { $o = proofline --version }    # about 10 ms

$o = … captures the output, the way a script or CI step does. Printing --version takes about
10 ms; the rest is waiting.

Why it happens, step by step

  1. Once a day, the CLI checks for a new release in the background. update::on_invocation
    starts a second copy of itself (proofline __update-check). That helper asks proofline.sh for
    the 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.

  2. Capturing output uses a pipe, and the reader waits until the pipe is closed. When PowerShell
    runs $o = proofline --version, it connects proofline's output to a pipe and keeps reading until
    the pipe is closed. A pipe only closes when every process holding its writing end has let go.

  3. 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::spawn
    starts processes on Windows with bInheritHandles = TRUE, which means: give the new process a
    copy 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 does
    not 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.

  4. So the helper is still holding the pipe after proofline exits. proofline --version
    finishes 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.

  5. 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 test only on Ubuntu.

The repository's own test already catches this. On Windows,
update::tests::detached_discovery_is_serialized_and_notices_arrive_on_the_next_invocation fails
every 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 CI (for example GitHub Actions windows-* runners). Each job starts with an empty
    cache, so every proofline command in the job pays the extra seconds.
  • Scripts and tools that read the output on Windows developer machines: $(...), pipes,
    editors and agents that talk to proofline mcp. There it happens once a day (the cache
    remembers 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 unsafe code, and
this crate sets unsafe_code = "forbid" (Cargo.toml:67), so that's not an option. I also
checked the alternatives:

  • Rust's standard library has a safe switch for exactly this (CommandExt::inherit_handles), but
    on the pinned toolchain (1.100) it is still unstable (windows_process_extensions_inherit_handles,
    #146407), so stable Rust can't use it.
  • I couldn't find a small, maintained crate that wraps it safely.

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, new may_detach(): returns true on Linux/macOS (no change there). On
    Windows it returns true only if both stdout and stderr are consoles (is_terminal()).
  • src/update.rs, prepare_check(..., detach, ...): when detach is false, return before
    writing 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() returns None otherwise). So a piped run on Windows never showed it anyway. Now
it 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:

  • The existing test above now runs its "script" processes through the real may_detach(), with
    their 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.
  • New test 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 detach decides). Before the change it failed because the attempt was recorded.
  • New test windows_detaches_only_when_both_output_streams_are_consoles (all platforms): the rule
    is 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 other
    platforms, every combination allows it.
  • New test captured_windows_invocation_leaves_discovery_to_a_console_run (tests/update.rs,
    Windows only): runs the real proofline --version binary with its output captured and an empty
    cache. It checks that update.lock exists (so the update logic really ran and made its decision)
    and that no update.json is written (so the decision was "don't start the helper"). This covers
    the wiring inside on_invocation, not just prepare_check.

The real binary:

$o = proofline --version, empty cache run 1 run 2 run 3
before 2 552 ms 1 466 ms 1 119 ms
after 21 ms 10 ms 10 ms
with PROOFLINE_NO_UPDATE_CHECK=1 (baseline) 10 ms 11 ms 10 ms

After the change, no cache file was written in the piped runs, as intended. (The very first
launch of a freshly built .exe sometimes took ~1 s before and after; that's Windows scanning a
new file, not the checker.)

A console run still checks for updates: Start-Process proofline --version (which opens its
own console window) with an empty cache wrote the cache file within 5 s
({"checked":…,"latest":"0.4.7"}).

Checks:

  • cargo fmt --check passes.
  • cargo clippy --locked --all-targets -- -D warnings reports nothing in the changed code. It
    does report two lints that already fail on main in 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 on main and
    are 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) and
    tests/local_patch.rs:275 (a file name Windows doesn't allow). main has 8 failures: these 7
    plus the update test this PR fixes.

Not tested locally: Linux. On Linux/macOS may_detach() is always true, so the code path is
the same as before; the Ubuntu CI job checks it.

Follow-up (separate issue, not this PR)

Run cargo clippy and cargo test on the Windows CI job. Both would have caught this.

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.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Proofline has 2 new findings to review.

View full review in Proofline

Comment thread src/update/tests.rs Resolved
Comment thread src/update/tests.rs
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(), || {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

View finding in Proofline

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked fixed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Still present at ac50182: Proofline found this issue again after it was marked fixed. It is now at src/update/tests.rs:129-136.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

dismiss

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dismissed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dismissed.

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.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Proofline has 1 new finding to review.

View full review in Proofline

Comment thread src/update/tests.rs
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(), || {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

View finding in Proofline

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

fixed

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked fixed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked fixed.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Proofline has 1 more finding to review, 2 in this review run.

View full review in Proofline

Comment thread tests/update.rs
.output()
.unwrap();
assert!(version.status.success());
assert!(!directory.path().join("proofline/update.json").exists());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

View finding in Proofline

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

fixed

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked fixed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked fixed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Still present at ac50182: Proofline found this issue again after it was marked fixed.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

dismiss

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dismissed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dismissed.

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.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Proofline has 1 new finding to review. 3 open findings need attention in this pull request.

View full review in Proofline

Comment thread src/update.rs
Comment on lines 99 to +102
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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

View finding in Proofline

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

fixed

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked fixed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Marked fixed.

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.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Proofline has 2 new findings to review.

View full review in Proofline

Comment thread src/update/tests.rs
Comment on lines +116 to +119
#[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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

View finding in Proofline

Also at:

Comment thread tests/update.rs
Comment on lines +7 to +10
#[cfg(windows)]
#[test]
fn captured_windows_invocation_leaves_discovery_to_a_console_run() {
let directory = tempfile::tempdir().unwrap();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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).

View finding in Proofline

@Ahmar004 Ahmar004 closed this Nov 22, 2026
@Ahmar004 Ahmar004 reopened this Nov 22, 2026

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Proofline has 2 new findings to review.

View full review in Proofline

Comment thread src/update/tests.rs
Comment on lines 266 to +269
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

View finding in Proofline

Comment thread tests/update.rs
Comment on lines +20 to +21
assert!(directory.path().join("proofline/update.lock").exists());
assert!(!directory.path().join("proofline/update.json").exists());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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).

View finding in Proofline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant