Skip to content
Merged
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
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions crates/karva/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ argfile = { workspace = true }
camino = { workspace = true }
clap = { workspace = true, features = ["wrap_help", "string", "env"] }
colored = { workspace = true }
crossbeam-channel = { workspace = true }
ctrlc = { workspace = true }
tracing = { workspace = true, features = ["release_max_level_debug"] }
wild = { workspace = true }

Expand Down
10 changes: 2 additions & 8 deletions crates/karva/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,12 @@ pub(crate) fn test(args: TestCommand) -> Result<ExitStatus> {
let config = karva_runner::ParallelTestConfig {
num_workers,
no_cache,
create_ctrlc_handler: true,
};

let (shutdown_tx, shutdown_rx) = crossbeam_channel::unbounded();

ctrlc::set_handler(move || {
let _ = shutdown_tx.send(());
})
.expect("Error setting Ctrl+C handler");

let start_time = Instant::now();

let result = karva_runner::run_parallel_tests(&db, &config, &sub_command, Some(&shutdown_rx))?;
let result = karva_runner::run_parallel_tests(&db, &config, &sub_command)?;

print_test_output(
printer,
Expand Down
3 changes: 2 additions & 1 deletion crates/karva_benchmark/src/walltime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn test_project(project: &ProjectDatabase) {
let config = karva_runner::ParallelTestConfig {
num_workers,
no_cache: false,
create_ctrlc_handler: false,
};

let args = SubTestCommand {
Expand All @@ -71,7 +72,7 @@ fn test_project(project: &ProjectDatabase) {
..SubTestCommand::default()
};

karva_runner::run_parallel_tests(project, &config, &args, None).unwrap();
karva_runner::run_parallel_tests(project, &config, &args).unwrap();
}

pub fn bench_project(bencher: Bencher, benchmark: &ProjectBenchmark) {
Expand Down
1 change: 1 addition & 0 deletions crates/karva_runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ karva_system = { workspace = true }
anyhow = { workspace = true }
camino = { workspace = true }
crossbeam-channel = { workspace = true }
ctrlc = { workspace = true }
ignore = { workspace = true }
tracing = { workspace = true }
which = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/karva_runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod collection;
mod orchestration;
mod partition;
mod shutdown;

pub use orchestration::{ParallelTestConfig, run_parallel_tests};
15 changes: 14 additions & 1 deletion crates/karva_runner/src/orchestration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use camino::Utf8PathBuf;
use crossbeam_channel::{Receiver, TryRecvError};

use crate::shutdown::shutdown_receiver;
use karva_cache::{
AggregatedResults, CACHE_DIR, CacheReader, RunHash, reader::read_recent_durations,
};
Expand Down Expand Up @@ -118,6 +120,12 @@ impl WorkerManager {
pub struct ParallelTestConfig {
pub num_workers: usize,
pub no_cache: bool,
/// Whether to create a Ctrl+C handler for graceful shutdown.
///
/// When `true`, a signal handler is installed (idempotently) to handle
/// Ctrl+C and gracefully stop workers. Set to `false` in contexts where
/// the handler should not be installed (e.g., benchmarks).
pub create_ctrlc_handler: bool,
}

/// Spawn worker processes for each partition
Expand Down Expand Up @@ -179,7 +187,6 @@ pub fn run_parallel_tests(
db: &ProjectDatabase,
config: &ParallelTestConfig,
args: &SubTestCommand,
shutdown_rx: Option<&Receiver<()>>,
) -> Result<AggregatedResults> {
let mut test_paths = Vec::new();

Expand Down Expand Up @@ -238,6 +245,12 @@ pub fn run_parallel_tests(

let mut worker_manager = spawn_workers(db, &partitions, &cache_dir, &run_hash, args)?;

let shutdown_rx = if config.create_ctrlc_handler {
Some(shutdown_receiver())
} else {
None
};

let num_workers = worker_manager.wait_all(shutdown_rx);

for worker in &mut worker_manager.workers {
Expand Down
40 changes: 40 additions & 0 deletions crates/karva_runner/src/shutdown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::sync::OnceLock;

use crossbeam_channel::Receiver;

static SHUTDOWN_CHANNEL: OnceLock<(crossbeam_channel::Sender<()>, Receiver<()>)> = OnceLock::new();

/// Returns a reference to the global shutdown receiver.
///
/// The first call initializes the channel and sets up the Ctrl+C handler.
/// Subsequent calls return the same receiver. This is safe to call multiple
/// times (idempotent).
pub fn shutdown_receiver() -> &'static Receiver<()> {
let (_, rx) = SHUTDOWN_CHANNEL.get_or_init(|| {
let (tx, rx) = crossbeam_channel::unbounded();

let handler_tx = tx.clone();
if let Err(err) = ctrlc::set_handler(move || {
let _ = handler_tx.send(());
}) {
tracing::warn!("Failed to set Ctrl+C handler: {err}");
}

(tx, rx)
});
rx
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_shutdown_receiver_is_idempotent() {
let rx1 = shutdown_receiver();
let rx2 = shutdown_receiver();

// Both calls should return the same receiver
assert!(std::ptr::eq(rx1, rx2));
}
}
Loading