Skip to content
Open
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
30 changes: 27 additions & 3 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -99,7 +101,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) {
Comment on lines 101 to +104

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.

crate::say(&notice);
} else {
// Missing or failed advisory discovery never changes the command result.
Expand Down Expand Up @@ -150,6 +152,7 @@ fn prepare_check(
path: &Path,
args: &[std::ffi::OsString],
interactive: bool,
detach: bool,
launch: impl FnOnce() -> anyhow::Result<()>,
) -> anyhow::Result<Option<String>> {
let directory = path.parent().context("update cache has no directory")?;
Expand All @@ -171,7 +174,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.
Expand All @@ -188,6 +192,26 @@ 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 {
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 {
let mut command = Command::new(executable);
command.env_clear();
Expand Down
50 changes: 48 additions & 2 deletions src/update/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -101,6 +101,41 @@ 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();
let cache = directory.path().join("update.json");
Comment thread
prooflinedev[bot] marked this conversation as resolved.
Resolved
Comment on lines +116 to +119

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:

// `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;
// Only `detach` decides: a redirected (non-interactive) run still checks.
prepare_check(&cache, &["status".into()], false, 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};
Expand Down Expand Up @@ -229,7 +264,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(), || {

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.

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.

spawn_check(worker)
Comment on lines 266 to +269

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

})
})
.await
.unwrap()
Expand Down Expand Up @@ -297,6 +335,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;
Expand Down
19 changes: 19 additions & 0 deletions tests/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@
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();
Comment on lines +7 to +10

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

// `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());
// 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());

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.

Comment on lines +20 to +21

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

}

#[test]
fn cached_update_preserves_schema_help_and_source_installation() {
let directory = tempfile::tempdir().unwrap();
Expand Down