From 74f3182787d5dcba8593d098418409fc75198e94 Mon Sep 17 00:00:00 2001 From: Maxence Rossignol Date: Tue, 20 Oct 2026 14:48:29 +0200 Subject: [PATCH] Probe: time each state write on Windows (not for merge) Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 78 +++++++++++----------------------------- src/server.rs | 32 ++++++++++++++--- src/state.rs | 14 ++++++++ 3 files changed, 62 insertions(+), 62 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ce6cb0..a610e72 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,81 +1,45 @@ name: ci on: pull_request: - push: - branches: [main] -env: - CARGO_TERM_COLOR: always permissions: contents: read -# A new push to a pull request cancels its older run. -concurrency: - group: ci-${{ github.ref }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - check: - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - runs-on: ${{ matrix.os }} + probe: + runs-on: windows-latest timeout-minutes: 60 env: CARGO_INCREMENTAL: '1' CARGO_PROFILE_DEV_DEBUG: line-tables-only CARGO_PROFILE_TEST_DEBUG: line-tables-only - CARGO_PROFILE_RELEASE_CODEGEN_UNITS: '16' - CARGO_PROFILE_RELEASE_LTO: 'off' defaults: run: shell: bash steps: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 - run: rustup show - - if: runner.os == 'Linux' - run: cargo fmt --all -- --check - # Keep compiler reuse data, without caching acceptance outputs or pruning crates. - uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5 - id: cargo-cache with: - path: &cargo-cache-paths | + path: | ~/.cargo/registry target/debug - target/release - target/x86_64-unknown-linux-musl - key: cargo-ci-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('rust-toolchain.toml') }}-${{ hashFiles('Cargo.lock', 'Cargo.toml', '.github/workflows/ci.yml') }} + key: cargo-ci-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('rust-toolchain.toml') }}-probe restore-keys: cargo-ci-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('rust-toolchain.toml') }}- - - if: runner.os == 'Linux' + - name: Defender state + shell: pwsh run: | - sudo apt-get update && sudo apt-get install -y musl-tools - rustup target add x86_64-unknown-linux-musl - - name: Compile unit and acceptance tests - run: cargo test --locked --bins --test acceptance --no-run - - run: cargo clippy --locked --all-targets -- -D warnings - - run: cargo build --locked --release - # Host fault injection needs dynamic linking; container tests use static musl. - - name: Linux binary for container tests - if: runner.os == 'Linux' - run: cargo build --locked --release --target x86_64-unknown-linux-musl - env: - CC_x86_64_unknown_linux_musl: musl-gcc - # A failed scenario must not force the next run to rebuild everything. - - uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25 # v5 - if: steps.cargo-cache.outputs.cache-hit != 'true' - with: - path: *cargo-cache-paths - key: ${{ steps.cargo-cache.outputs.cache-primary-key }} - - run: cargo test --locked --bins - - name: acceptance tests (against the optimized CI binary) + Get-MpComputerStatus | Select-Object AntivirusEnabled, RealTimeProtectionEnabled, OnAccessProtectionEnabled | Format-List + (Get-MpPreference).ExclusionPath + $env:TEMP + - run: cargo test --locked --bins --no-run + - name: The test alone, 30 times run: | - if [[ "$RUNNER_OS" == Linux ]]; then - export JAYNSHARE_LINUX_BIN=target/x86_64-unknown-linux-musl/release/jaynshare - fi - cargo test --locked --test acceptance -- --test-threads=8 - env: - JAYNSHARE_BIN: target/release/jaynshare${{ runner.os == 'Windows' && '.exe' || '' }} - deny: - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 - - uses: EmbarkStudios/cargo-deny-action@b66acf5e9fe20f8aba065be86778a8a4c846f902 # v2 + for i in $(seq 1 30); do + echo "== alone $i" + cargo test --locked --bins -q -- --nocapture --exact server::tests::concurrent_state_writes_all_succeed_and_the_last_one_is_current 2>&1 | grep -E 'slow phase|took|panicked|deadlocked|test result' || true + done + - name: The full unit suite, 10 times + run: | + for i in $(seq 1 10); do + echo "== suite $i" + cargo test --locked --bins -q -- --nocapture 2>&1 | grep -E 'slow phase|concurrent writes took|writer [0-9] round|panicked|deadlocked|test result' || true + done diff --git a/src/server.rs b/src/server.rs index 8c0f6e6..482f2c6 100644 --- a/src/server.rs +++ b/src/server.rs @@ -375,10 +375,26 @@ mod tests { type Write = fn(&Server, usize) -> Result<(), String>; /// Runs `write` ROUNDS times on its own thread and sends the first failure. - fn writer(server: &Arc, results: &mpsc::Sender>, write: Write) { + static PROGRESS: [std::sync::atomic::AtomicUsize; 3] = + [const { std::sync::atomic::AtomicUsize::new(0) }; 3]; + + fn writer( + server: &Arc, + results: &mpsc::Sender>, + write: Write, + n: usize, + ) { let (server, results) = (Arc::clone(server), results.clone()); thread::spawn(move || { - let _ = results.send((0..ROUNDS).try_for_each(|i| write(&server, i))); + let _ = results.send((0..ROUNDS).try_for_each(|i| { + let started = Instant::now(); + let out = write(&server, i); + if started.elapsed() > Duration::from_millis(500) { + eprintln!("writer {n} round {i} took {:?}", started.elapsed()); + } + PROGRESS[n].store(i + 1, Ordering::SeqCst); + out + })); }); } @@ -425,16 +441,22 @@ mod tests { }, ]; let (results, finished) = mpsc::channel(); - for write in writes { - writer(&server, &results, write); + let started = Instant::now(); + for (n, write) in writes.into_iter().enumerate() { + writer(&server, &results, write, n); } for _ in writes { let result = finished .recv_timeout(Duration::from_secs(60)) - .expect("a writer deadlocked"); + .unwrap_or_else(|e| { + let rounds: Vec = + PROGRESS.iter().map(|p| p.load(Ordering::SeqCst)).collect(); + panic!("a writer deadlocked: {e:?}; rounds done per writer: {rounds:?}") + }); result.expect("every concurrent write succeeds"); } + eprintln!("concurrent writes took {:?}", started.elapsed()); let on_disk: serde_json::Value = serde_json::from_slice(&std::fs::read(&server.state_path).expect("state file")) .expect("state JSON"); diff --git a/src/state.rs b/src/state.rs index a79ebbd..1d6fbae 100644 --- a/src/state.rs +++ b/src/state.rs @@ -124,6 +124,14 @@ pub fn write(path: &Path, state: &State) -> io::Result<()> { /// Temporary file in the same directory, mode 0600, fsync, rename, directory fsync. pub fn write_private_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> { + let mut last = std::time::Instant::now(); + let mut lap = |phase: &str| { + let now = std::time::Instant::now(); + if now - last > std::time::Duration::from_millis(250) { + eprintln!("slow phase {phase}: {:?}", now - last); + } + last = now; + }; let dir = path.parent().unwrap_or(Path::new(".")); if !dir.exists() { ensure_private_dir(dir)?; @@ -137,13 +145,18 @@ pub fn write_private_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> { let tmp = dir.join(format!(".{file_name}.{}.tmp", std::process::id())); { let mut f = open_private(&tmp)?; + lap("open"); f.write_all(bytes)?; + lap("write"); f.sync_all()?; + lap("sync_all"); } + lap("close"); if let Err(e) = fs::rename(&tmp, path) { let _ = fs::remove_file(&tmp); return Err(e); } + lap("rename"); #[cfg(unix)] { File::open(dir)?.sync_all()?; @@ -152,6 +165,7 @@ pub fn write_private_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> { // renamed file inherits its parent's grant otherwise). #[cfg(windows)] crate::state::protect_windows(path).map_err(io::Error::other)?; + lap("protect_windows"); Ok(()) }