Skip to content
Closed
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
78 changes: 21 additions & 57 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 27 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Server>, results: &mpsc::Sender<Result<(), String>>, write: Write) {
static PROGRESS: [std::sync::atomic::AtomicUsize; 3] =
[const { std::sync::atomic::AtomicUsize::new(0) }; 3];

fn writer(
server: &Arc<Server>,
results: &mpsc::Sender<Result<(), String>>,
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
}));
});
}

Expand Down Expand Up @@ -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<usize> =
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");
Expand Down
14 changes: 14 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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()?;
Expand 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(())
}

Expand Down