Skip to content

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

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

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

Conversation

@Ahmar004

@Ahmar004 Ahmar004 commented Nov 22, 2026 •

Copy link
Copy Markdown

Fixes #78

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.

Linux: the repository's CI passed on my fork (formatting, clippy, cargo test and
cargo deny on Ubuntu, plus the Windows build):
https://github.com/Ahmar004/cli/actions/runs/45060272853. On Linux/macOS may_detach() is always
true, so the code path there is the same as before.

Follow-up (separate issue, not this PR)

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

Proofline review

I ran Proofline on this branch in my fork (Ahmar004#1). It made 7 comments over three review rounds; I accepted 5 and disagreed with 2:

# Proofline comment Decision Why, and what changed
1 Warning, src/update/tests.rs:104: the launch assertions only use interactive = true, so a guard that skipped discovery for redirected output would still pass. Accepted Correct: my change deliberately separates "may a helper start" (detach) from "may a notice be shown" (interactive), but no test pinned that separation. The launching call now uses interactive = false. Checked: with an !interactive guard added on purpose, the old test passed and the new one fails (assert!(launched)); without it, it passes.
2 Warning, src/update/tests.rs subprocess test: it passes may_detach() to prepare_check itself, bypassing on_invocation, so a CLI that always allowed discovery would still pass. Accepted Correct: the wiring in on_invocation was only checked by hand. Added captured_windows_invocation_leaves_discovery_to_a_console_run (tests/update.rs), which runs the real binary with captured output and an empty cache and asserts that no update.json is written, as suggested. Checked: with on_invocation changed on purpose to pass true, it fails (and the run took 2.1 s instead of 1 s, the bug itself); restored, it passes. Windows-only, because on other platforms the binary would start a real network check (endpoint injection exists only in unit tests).
3 Warning, src/update/tests.rs:256 (second review): the fixture pipes both streams, so the Windows assertions still pass if may_detach checks only one stream or uses OR instead of AND. Accepted Correct, and it's the most important one. The common real case is mixed: $o = proofline … captures only stdout, while stderr stays on the console. Moved the rule into a pure detachable(windows, stdout_is_console, stderr_is_console) and added windows_detaches_only_when_both_output_streams_are_consoles, which checks all four combinations. A pure function is used because a test can't reliably give a child process a real console. Checked: with OR, stdout-only and stderr-only rules injected on purpose, all 7 old tests passed and the new test fails each time.
4 Warning, tests/update.rs:19 (second review): the binary test passes even if --version stops running the update logic, since that also leaves no update.json. Accepted Correct: "no cache file" alone can't tell "decided not to start the helper" from "never decided". Added assert!(update.lock exists); the lock is created before the decision. Checked: with on_invocation made to skip --version on purpose, the old test passed and the new assertion fails; restored, it passes.
5 Third review, reopened comment 2 ("still present", now pointing at the unit test's direct prepare_check(…, true, …) call). Disagreed (dismissed) Pattern re-match, not a remaining gap. That unit test calls prepare_check directly on purpose: it checks one rule ("only detach decides") and must inject a fake launcher, which on_invocation can't do. The real on_invocation path is covered by the binary test from row 2, which fails when on_invocation is broken on purpose.
6 Third review, reopened comment 4 ("still present" after the update.lock assertion was added). Disagreed (dismissed) Already covered: the test asserts update.lock exists, and src/main.rs:13 calls on_invocation() before Cli::try_parse(), so before clap handles --version. The injected "skip --version" mistake makes the test fail.
7 Third review, new: the comment above on_invocation still says discovery always continues in a child. Accepted Correct, the comment was out of date. It now says a due check runs in a child only when may_detach allows it, and that on Windows both streams must be consoles (commit a301ee2).

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

This branch has not been deployed

No deployments
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.

Windows: scripts and CI wait 1–3 s for each proofline command because of the background update check

1 participant