Skip to content

Commit bb63890

Browse files
authored
Prevent concurrent lockfile conflicts using flock and add backup (payjoin#851)
## Summary This PR introduces a safe and stable mechanism to manage Cargo.lock during testing across multiple scripts. - File Locking with flock: Ensures only one script can manipulate Cargo.lock at a time using a .Cargo.lock.flock file. - Backup and Restore: test.sh creates a backup of the existing Cargo.lock before overwriting it and restores it afterward using an EXIT trap. This prevents loss of local Cargo.lock changes when running ./contrib/test.sh, and race conditions when running concurrently closes payjoin#773
2 parents 6d1be60 + 8c9bc73 commit bb63890

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

contrib/test.sh

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
DEPS="recent minimal"
5-
CRATES="payjoin payjoin-cli payjoin-directory payjoin-ffi"
6-
7-
for dep in $DEPS; do
8-
cargo --version
9-
rustc --version
10-
11-
# Some tests require certain toolchain types.
12-
export NIGHTLY=false
13-
export STABLE=true
14-
if cargo --version | grep nightly; then
15-
STABLE=false
16-
NIGHTLY=true
17-
fi
18-
if cargo --version | grep beta; then
19-
STABLE=false
4+
LOCKFILE="Cargo.lock"
5+
LOCKFILE_BAK="Cargo.lock.bak"
6+
LOCKFILE_LOCK=".Cargo.lock.flock"
7+
8+
# Acquire file lock to prevent concurrent modification
9+
# We can't use cargo test --lockfile-path since that doesn't exist in our MSRV
10+
(
11+
flock 9
12+
13+
# Backup original lockfile
14+
if [ -f "$LOCKFILE" ]; then
15+
mv "$LOCKFILE" "$LOCKFILE_BAK"
2016
fi
2117

22-
cp "Cargo-$dep.lock" Cargo.lock
18+
# Restore the original lockfile on exit
19+
trap '[ -f "$LOCKFILE_BAK" ] && mv "$LOCKFILE_BAK" "$LOCKFILE"' EXIT
20+
21+
DEPS="recent minimal"
22+
CRATES="payjoin payjoin-cli payjoin-directory payjoin-ffi"
2323

24-
for crate in $CRATES; do
25-
(
26-
cd "$crate"
27-
./contrib/test.sh
28-
)
24+
for dep in $DEPS; do
25+
cargo --version
26+
rustc --version
27+
28+
# Some tests require certain toolchain types.
29+
export NIGHTLY=false
30+
export STABLE=true
31+
if cargo --version | grep nightly; then
32+
STABLE=false
33+
NIGHTLY=true
34+
fi
35+
if cargo --version | grep beta; then
36+
STABLE=false
37+
fi
38+
39+
cp "Cargo-$dep.lock" Cargo.lock
40+
41+
for crate in $CRATES; do
42+
(
43+
cd "$crate"
44+
./contrib/test.sh
45+
)
46+
done
2947
done
30-
done
48+
49+
) 9>"$LOCKFILE_LOCK"

0 commit comments

Comments
 (0)