Skip to content

Commit a4e6ea1

Browse files
branchseerclaude
andcommitted
fix(test): eliminate race in exit-on-ctrlc output ordering
Move "ctrl-c received" print from the signal handler to the main thread (after OnceLock::wait), so it always executes after the milestone flush println!(). Previously, std::process::exit(0) in the handler could terminate before output reached the PTY, causing flaky snapshot diffs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c5aa545 commit a4e6ea1

File tree

5 files changed

+12
-40
lines changed

5 files changed

+12
-40
lines changed

crates/vite_task_bin/src/vtt/exit_on_ctrlc.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
/// exit-on-ctrlc
24
///
35
/// Sets up a Ctrl+C handler, emits a "ready" milestone, then waits.
@@ -25,19 +27,18 @@ pub fn run() -> Result<(), Box<dyn std::error::Error>> {
2527
}
2628
}
2729

28-
ctrlc::set_handler(move || {
29-
use std::io::Write;
30-
let _ = write!(std::io::stdout(), "ctrl-c received");
31-
let _ = std::io::stdout().flush();
32-
std::process::exit(0);
30+
let ctrlc_once_lock = Arc::new(std::sync::OnceLock::<()>::new());
31+
32+
ctrlc::set_handler({
33+
let ctrlc_once_lock = Arc::clone(&ctrlc_once_lock);
34+
move || {
35+
let _ = ctrlc_once_lock.set(());
36+
}
3337
})?;
3438

3539
pty_terminal_test_client::mark_milestone("ready");
36-
// Print a newline so the milestone bytes get flushed through line-buffered
37-
// writers (labeled/grouped log modes).
38-
println!();
3940

40-
loop {
41-
std::thread::park();
42-
}
41+
ctrlc_once_lock.wait();
42+
println!("ctrl-c received");
43+
Ok(())
4344
}

crates/vite_task_bin/tests/e2e_snapshots/fixtures/ctrl-c/snapshots.toml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ steps = [
1313
] },
1414
]
1515

16-
[[e2e]]
17-
name = "ctrl-c terminates running tasks (labeled)"
18-
steps = [
19-
{ argv = [
20-
"vt",
21-
"run",
22-
"--log=labeled",
23-
"dev",
24-
], interactions = [
25-
{ "expect-milestone" = "ready" },
26-
{ "write-key" = "ctrl-c" },
27-
] },
28-
]
29-
3016
[[e2e]]
3117
name = "ctrl-c terminates running tasks (cached)"
3218
steps = [

crates/vite_task_bin/tests/e2e_snapshots/fixtures/ctrl-c/snapshots/ctrl-c terminates running tasks (cached).snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ expression: e2e_outputs
77
$ vtt exit-on-ctrlc
88
@ write-key: ctrl-c
99
$ vtt exit-on-ctrlc
10-
1110
ctrl-c received

crates/vite_task_bin/tests/e2e_snapshots/fixtures/ctrl-c/snapshots/ctrl-c terminates running tasks (labeled).snap

Lines changed: 0 additions & 13 deletions
This file was deleted.

crates/vite_task_bin/tests/e2e_snapshots/fixtures/ctrl-c/snapshots/ctrl-c terminates running tasks.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ expression: e2e_outputs
77
$ vtt exit-on-ctrlccache disabled
88
@ write-key: ctrl-c
99
$ vtt exit-on-ctrlccache disabled
10-
1110
ctrl-c received

0 commit comments

Comments
 (0)