diff --git a/crates/pet-core/src/cache.rs b/crates/pet-core/src/cache.rs index 8477e0d7..25261e7d 100644 --- a/crates/pet-core/src/cache.rs +++ b/crates/pet-core/src/cache.rs @@ -28,6 +28,8 @@ pub struct LocatorCache { struct InFlightEntry { result: Mutex>>, changed: Condvar, + #[cfg(test)] + waiting: std::sync::atomic::AtomicBool, } #[derive(Clone)] @@ -52,6 +54,8 @@ impl InFlightEntry { Self { result: Mutex::new(None), changed: Condvar::new(), + #[cfg(test)] + waiting: std::sync::atomic::AtomicBool::new(false), } } } @@ -233,6 +237,10 @@ impl LocatorCache { .lock() .unwrap_or_else(|poisoned| poisoned.into_inner()); while result.is_none() { + #[cfg(test)] + entry + .waiting + .store(true, std::sync::atomic::Ordering::Release); result = entry .changed .wait(result) @@ -313,7 +321,15 @@ mod tests { mpsc, Arc, Barrier, Mutex, }; use std::thread; - use std::time::Duration; + use std::time::{Duration, Instant}; + + fn wait_for_pending(entry: &InFlightEntry) { + let deadline = Instant::now() + Duration::from_secs(5); + while !entry.waiting.load(Ordering::Acquire) { + assert!(Instant::now() < deadline, "cache waiter did not block"); + thread::yield_now(); + } + } #[test] fn test_cache_get_and_insert() { @@ -379,6 +395,8 @@ mod tests { barrier.wait(); started_rx.recv_timeout(Duration::from_secs(5)).unwrap(); + let entry = cache.in_flight.lock().unwrap().get("key").unwrap().clone(); + wait_for_pending(&entry); assert_eq!(calls.load(Ordering::SeqCst), 1); assert!(started_rx.try_recv().is_err()); @@ -397,21 +415,20 @@ mod tests { #[test] fn test_cache_get_or_insert_with_shares_concurrent_none_result() { - let entry = Arc::new(InFlightEntry::new()); + let cache: LocatorCache = LocatorCache::new(); + let InFlightClaim::Owner(owner) = cache.claim_in_flight(&"key".to_string()) else { + panic!("new cache unexpectedly has an in-flight owner"); + }; + let entry = owner.entry.clone(); let waiter_entry = entry.clone(); let waiter = thread::spawn(move || LocatorCache::::wait_for_in_flight(waiter_entry)); - *entry - .result - .lock() - .expect("locator cache in-flight result lock poisoned") = - Some(InFlightResult::Value(None)); - entry.changed.notify_all(); + wait_for_pending(&entry); + owner.complete(None); assert_eq!(waiter.join().unwrap(), None); - let cache: LocatorCache = LocatorCache::new(); assert_eq!(cache.get_or_insert_with("key".to_string(), || None), None); assert!(!cache.contains_key(&"key".to_string())); @@ -452,12 +469,13 @@ mod tests { in_flight: &in_flight, }; + let waiter_entry = entry.clone(); + let waiter = + thread::spawn(move || LocatorCache::::wait_for_in_flight(waiter_entry)); + wait_for_pending(&entry); drop(owner); - let waiter_result = - std::panic::catch_unwind(|| LocatorCache::::wait_for_in_flight(entry)); - - assert!(waiter_result.is_err()); + assert!(waiter.join().is_err()); assert!(in_flight.lock().unwrap().is_empty()); } diff --git a/crates/pet-jsonrpc/src/output.rs b/crates/pet-jsonrpc/src/output.rs index 8dbd157d..b10514c6 100644 --- a/crates/pet-jsonrpc/src/output.rs +++ b/crates/pet-jsonrpc/src/output.rs @@ -101,9 +101,22 @@ impl Output { ready: Condvar::new(), }); let writer_shared = Arc::clone(&shared); + #[cfg(test)] + let (waiting_tx, waiting_rx) = std::sync::mpsc::sync_channel(1); thread::Builder::new() .name("pet-jsonrpc-output".to_string()) - .spawn(move || writer_loop(writer, writer_shared))?; + .spawn(move || { + writer_loop( + writer, + writer_shared, + #[cfg(test)] + waiting_tx, + ) + })?; + #[cfg(test)] + waiting_rx + .recv_timeout(std::time::Duration::from_secs(5)) + .expect("test writer did not reach its empty-queue wait"); Ok(Self { shared, @@ -209,7 +222,13 @@ impl Output { } } -fn writer_loop(mut writer: impl Write, shared: Arc) { +fn writer_loop( + mut writer: impl Write, + shared: Arc, + #[cfg(test)] waiting: std::sync::mpsc::SyncSender<()>, +) { + #[cfg(test)] + let mut waiting = Some(waiting); loop { let frame = { let mut state = shared @@ -224,6 +243,10 @@ fn writer_loop(mut writer: impl Write, shared: Arc) { if !state.accepting { return; } + #[cfg(test)] + if let Some(waiting) = waiting.take() { + waiting.send(()).expect("test writer observer dropped"); + } state = shared .ready .wait(state) @@ -562,11 +585,11 @@ mod tests { impl Write for BlockingWriter { fn write(&mut self, bytes: &[u8]) -> io::Result { + let (released, ready) = &*self.release; + let mut released = released.lock().expect("release lock poisoned"); if let Some(entered) = self.entered.take() { entered.send(()).expect("blocked writer observer dropped"); } - let (released, ready) = &*self.release; - let mut released = released.lock().expect("release lock poisoned"); while !*released { released = ready.wait(released).expect("release lock poisoned"); } @@ -616,13 +639,13 @@ mod tests { impl Write for DelayedFailingWriter { fn write(&mut self, _bytes: &[u8]) -> io::Result { + let (released, ready) = &*self.release; + let mut released = released.lock().expect("release lock poisoned"); self.entered .take() .expect("writer entered more than once") .send(()) .expect("writer observer dropped"); - let (released, ready) = &*self.release; - let mut released = released.lock().expect("release lock poisoned"); while !*released { released = ready.wait(released).expect("release lock poisoned"); } @@ -1088,11 +1111,11 @@ mod tests { where S: Serializer, { + let (released, ready) = &*self.release; + let mut released = released.lock().expect("release lock poisoned"); self.entered .send(()) .expect("serialization observer dropped"); - let (released, ready) = &*self.release; - let mut released = released.lock().expect("release lock poisoned"); while !*released { released = ready.wait(released).expect("release lock poisoned"); } diff --git a/crates/pet-python-utils/src/process.rs b/crates/pet-python-utils/src/process.rs index 3244da24..3dec93b3 100644 --- a/crates/pet-python-utils/src/process.rs +++ b/crates/pet-python-utils/src/process.rs @@ -670,7 +670,7 @@ mod tests { struct ReleaseDescendant(tempfile::TempDir); impl ReleaseDescendant { - fn assert_terminated(&self) { + fn assert_terminated(&self, mut waiting: Option>) { assert!( self.0.path().join("ready").exists(), "descendant never started" @@ -681,6 +681,9 @@ mod tests { match lease.try_lock() { Ok(()) => break, Err(fs::TryLockError::WouldBlock) => { + if let Some(waiting) = waiting.take() { + waiting.send(()).expect("lease wait observer dropped"); + } assert!( started.elapsed() < CLEANUP_TIMEOUT, "descendant still holds its lease" @@ -697,6 +700,26 @@ mod tests { } } + #[test] + fn termination_check_waits_for_the_descendant_lease() { + use std::sync::{mpsc, Arc}; + + let directory = Arc::new(ReleaseDescendant(tempfile::tempdir().unwrap())); + let lease = fs::File::create(directory.0.path().join("lease")).unwrap(); + lease.lock().unwrap(); + fs::write(directory.0.path().join("ready"), b"ready").unwrap(); + let (waiting_tx, waiting_rx) = mpsc::sync_channel(1); + let waiting_directory = directory.clone(); + let waiter = thread::spawn(move || waiting_directory.assert_terminated(Some(waiting_tx))); + + waiting_rx + .recv_timeout(CLEANUP_TIMEOUT) + .expect("termination check did not observe the held lease"); + assert!(!waiter.is_finished()); + lease.unlock().unwrap(); + waiter.join().unwrap(); + } + impl ReleaseDescendant { fn release(&self) -> io::Result<()> { fs::write(self.0.path().join("release"), b"release")?; @@ -748,7 +771,7 @@ mod tests { Some(if mode == "tree-nonzero" { 23 } else { 0 }) ); assert!(started.elapsed() < Duration::from_secs(5)); - directory.assert_terminated(); + directory.assert_terminated(None); assert_child_reaped(fs::read_to_string(pid).unwrap().parse().unwrap()); } } @@ -768,7 +791,7 @@ mod tests { assert!(matches!(error, ProcessError::OutputLimit(4096)), "{error}"); } assert!(started.elapsed() < Duration::from_secs(5)); - directory.assert_terminated(); + directory.assert_terminated(None); } } @@ -802,7 +825,7 @@ mod tests { .unwrap() .status .success()); - directory.assert_terminated(); + directory.assert_terminated(None); } #[cfg(windows)] @@ -906,7 +929,7 @@ mod tests { .unwrap() .status .success()); - owned.assert_terminated(); + owned.assert_terminated(None); assert!(unrelated.0.try_wait().unwrap().is_none()); let lease = fs::File::open(directory.path().join("lease")).unwrap(); assert!(matches!( @@ -1187,7 +1210,7 @@ mod tests { assert!(matches!(error, ProcessError::Cancelled), "{error}"); }); - owned.assert_terminated(); + owned.assert_terminated(None); assert_child_reaped(fs::read_to_string(root_pid).unwrap().parse().unwrap()); assert!(unrelated.0.try_wait().unwrap().is_none()); let lease = fs::File::open(unrelated_directory.path().join("lease")).unwrap(); diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index 187df616..fbccc355 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -1,7 +1,10 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::{path::PathBuf, sync::Once}; +use std::{ + path::{Path, PathBuf}, + sync::Once, +}; use common::{does_version_match, resolve_test_path}; use lazy_static::lazy_static; @@ -489,6 +492,59 @@ fn verify_we_can_get_same_env_info_using_find_with_exe( ); } +fn comparable_symlinks(environment: &PythonEnvironment, toolcache: &Path) -> Vec { + environment + .symlinks + .iter() + .flatten() + .filter(|path| { + if environment.kind != Some(PythonEnvironmentKind::MacPythonOrg) + || !is_ci_toolcache_alias(path, toolcache) + { + return true; + } + + // PATH discovery knows this shortcut, but direct framework identification need not. + let executable = environment + .executable + .as_ref() + .expect("CI toolcache alias has no environment executable"); + let target = std::fs::canonicalize(path).unwrap_or_else(|error| { + panic!("Cannot resolve CI toolcache alias {path:?}: {error}") + }); + let expected = std::fs::canonicalize(executable).unwrap_or_else(|error| { + panic!("Cannot resolve environment executable {executable:?}: {error}") + }); + assert_eq!( + target, expected, + "CI toolcache alias {path:?} points to a different interpreter than {executable:?}" + ); + false + }) + .cloned() + .collect() +} + +fn is_ci_toolcache_alias(path: &Path, toolcache: &Path) -> bool { + let Ok(relative) = path.strip_prefix(toolcache) else { + return false; + }; + let mut parts = relative.iter(); + let Some(version) = parts.next().and_then(|part| part.to_str()) else { + return false; + }; + version.split('.').count() == 3 + && version + .split('.') + .all(|part| !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit())) + && parts + .next() + .is_some_and(|arch| arch == "x64" || arch == "arm64") + && parts.next().is_some_and(|part| part == "bin") + && parts.next().is_some_and(|part| part == "python") + && parts.next().is_none() +} + fn compare_environments(actual: PythonEnvironment, expected: PythonEnvironment, method: &str) { let mut actual = actual.clone(); let mut expected = expected.clone(); @@ -572,35 +628,9 @@ fn compare_environments(actual: PythonEnvironment, expected: PythonEnvironment, actual.prefix = expected.clone().prefix; } } - // known issue - actual.symlinks = Some( - actual - .clone() - .symlinks - .unwrap_or_default() - .iter() - .filter(|p| { - // This is in the path, but not easy to figure out, unless we add support for codespaces or CI. - !(p.starts_with("/Users/runner/hostedtoolcache/Python") - && p.to_string_lossy().contains("arm64")) - }) - .map(|p| p.to_path_buf()) - .collect::>(), - ); - expected.symlinks = Some( - expected - .clone() - .symlinks - .unwrap_or_default() - .iter() - .filter(|p| { - // This is in the path, but not easy to figure out, unless we add support for codespaces or CI. - !(p.starts_with("/Users/runner/hostedtoolcache/Python") - && p.to_string_lossy().contains("arm64")) - }) - .map(|p| p.to_path_buf()) - .collect::>(), - ); + let toolcache = Path::new("/Users/runner/hostedtoolcache/Python"); + actual.symlinks = Some(comparable_symlinks(&actual, toolcache)); + expected.symlinks = Some(comparable_symlinks(&expected, toolcache)); // if we know the arch, then verify it if expected.arch.as_ref().is_some() @@ -843,3 +873,125 @@ fn get_python_interpreter_info(cli: &[String]) -> Option { None } } + +#[test] +fn ci_toolcache_comparison_preserves_unrelated_and_malformed_aliases() { + let toolcache = Path::new("/Users/runner/hostedtoolcache/Python"); + let symlinks = [ + "/usr/local/bin/python3.12", + "/Users/runner/hostedtoolcache/Python/3.12.10/notarm64/bin/python", + "/Users/runner/hostedtoolcache/Python/3.12.10/arm64/lib/python", + "/Users/runner/hostedtoolcache/Python/3.12.10/arm64/bin/python-config", + "/Users/runner/hostedtoolcache/Python/3.12.\u{00e9}/arm64/bin/python", + "/Users/runner/hostedtoolcache/Python/3.12.10/arm64/bin/python/extra", + "/Users/runner/hostedtoolcache/Python-extra/3.12.10/arm64/bin/python", + ] + .map(PathBuf::from) + .to_vec(); + let environment = PythonEnvironment { + kind: Some(PythonEnvironmentKind::MacPythonOrg), + symlinks: Some(symlinks.clone()), + ..Default::default() + }; + + assert_eq!(comparable_symlinks(&environment, toolcache), symlinks); +} + +#[test] +fn ci_toolcache_aliases_require_an_exact_version_and_architecture() { + let toolcache = Path::new("/Users/runner/hostedtoolcache/Python"); + for arch in ["x64", "arm64"] { + assert!(is_ci_toolcache_alias( + &toolcache.join("3.12.10").join(arch).join("bin/python"), + toolcache + )); + } + for relative in [ + "3.12/x64/bin/python", + "3..10/arm64/bin/python", + "3.12.10.1/arm64/bin/python", + "3.12.10/x86_64/bin/python", + "3.12.10/arm64/../bin/python", + "3.12.10/arm64/bin/python3", + "3.12.10/arm64-\u{00e9}/bin/python", + ] { + assert!( + !is_ci_toolcache_alias(&toolcache.join(relative), toolcache), + "{relative}" + ); + } + + let alias = toolcache.join("3.12.10/arm64/bin/python"); + let environment = PythonEnvironment { + kind: Some(PythonEnvironmentKind::Conda), + symlinks: Some(vec![alias.clone()]), + ..Default::default() + }; + assert_eq!(comparable_symlinks(&environment, toolcache), vec![alias]); +} + +#[cfg(unix)] +#[test] +fn ci_toolcache_comparison_verifies_both_architectures_and_keeps_ordinary_aliases() { + use std::{fs, os::unix::fs::symlink}; + + let directory = tempfile::tempdir().unwrap(); + let toolcache = directory.path().join("toolcache-\u{00e9}/Python"); + let target = directory.path().join("python3.12"); + fs::write(&target, b"interpreter").unwrap(); + let executable = directory.path().join("executable"); + symlink(&target, &executable).unwrap(); + let ordinary = directory.path().join("ordinary-python"); + symlink(&target, &ordinary).unwrap(); + let mut symlinks = vec![ordinary.clone()]; + for arch in ["x64", "arm64"] { + let alias = toolcache.join("3.12.10").join(arch).join("bin/python"); + fs::create_dir_all(alias.parent().unwrap()).unwrap(); + symlink(&executable, &alias).unwrap(); + symlinks.push(alias); + } + let environment = PythonEnvironment { + kind: Some(PythonEnvironmentKind::MacPythonOrg), + executable: Some(executable), + symlinks: Some(symlinks.clone()), + ..Default::default() + }; + + assert_eq!( + comparable_symlinks(&environment, &toolcache), + vec![ordinary] + ); + assert_eq!(environment.symlinks, Some(symlinks)); +} + +#[cfg(unix)] +#[test] +fn ci_toolcache_comparison_rejects_different_or_unresolvable_interpreters() { + use std::{fs, os::unix::fs::symlink}; + + let directory = tempfile::tempdir().unwrap(); + let toolcache = directory.path().join("Python"); + let executable = directory.path().join("python3.12"); + let other = directory.path().join("other-python"); + fs::write(&executable, b"interpreter").unwrap(); + fs::write(&other, b"other interpreter").unwrap(); + let alias = toolcache.join("3.12.10/x64/bin/python"); + fs::create_dir_all(alias.parent().unwrap()).unwrap(); + let environment = PythonEnvironment { + kind: Some(PythonEnvironmentKind::MacPythonOrg), + executable: Some(executable), + symlinks: Some(vec![alias.clone()]), + ..Default::default() + }; + + symlink(&other, &alias).unwrap(); + assert!(std::panic::catch_unwind(|| comparable_symlinks(&environment, &toolcache)).is_err()); + fs::remove_file(&other).unwrap(); + assert!(std::panic::catch_unwind(|| comparable_symlinks(&environment, &toolcache)).is_err()); + + use std::{ffi::OsStr, os::unix::ffi::OsStrExt}; + let invalid = toolcache + .join(OsStr::from_bytes(b"3.12.\xff")) + .join("arm64/bin/python"); + assert!(!is_ci_toolcache_alias(&invalid, &toolcache)); +} diff --git a/docs/QUALITY_SNAPSHOTS.md b/docs/QUALITY_SNAPSHOTS.md index 86f6c435..a1a8c116 100644 --- a/docs/QUALITY_SNAPSHOTS.md +++ b/docs/QUALITY_SNAPSHOTS.md @@ -215,6 +215,20 @@ the unchanged line/function comparator gates those comparable artifacts. This wo PR without silently accepting an absent macOS baseline. Main/manual runs publish the native measurement and proof for inspection. Existing functional macOS installed-manager jobs remain. +Concurrency tests observe a pending wait before releasing an owner or worker. Cache wait +observations and JSONRPC writer-start observations compile only with `#[cfg(test)]`; they do +not change production scheduling or shutdown. The descendant termination check is also tested +with a file lease held until its pending poll is observed. These handshakes prevent scheduling +from deciding whether intended waiter paths execute, including individual writer instantiations +that a positive union of LCOV `DA` lines can obscure. Raw counters are not normalized and the +raw gate remains unchanged. + +The functional macOS comparison permits only the extra `MacPythonOrg` PATH shortcut at +`/Users/runner/hostedtoolcache/Python//{x64,arm64}/bin/python`. +Before excluding it from either alias list, the test requires it to resolve to that environment's +executable. Missing or different targets fail; unrelated paths and ordinary aliases remain +strictly compared, and the normalization does not modify interpreter metadata. + Stable Rust line instrumentation does not provide condition/branch outcomes. Reports show LCOV `BRDA` totals when supplied, otherwise explicitly report branch data as unavailable (not 100%). Native malformed-frame/envelope, EOF, broken-output, saturation, and descendant tests provide