Skip to content

Commit 8f00115

Browse files
Replaces 'flock' with mkdir locking (#886)
This PR replaces the Linux-specific `flock` command with a portable directory-based locking in the `test.sh` script. The change ensures compatibility across different Unix-like systems, including macOS, while maintaining the same functionality. Testing The locking mechanism has been verified with the following test cases: 1. Running a single instance of the test script 2. Attempting to run multiple instances concurrently 3. Testing cleanup on normal script completion 4. Testing cleanup on interruption (SIGINT, SIGTERM) Closes [#878](payjoin/rust-payjoin#878)
2 parents c8f84d0 + fcbdfc7 commit 8f00115

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

contrib/test.sh

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,52 @@
22
set -e
33

44
LOCKFILE="Cargo.lock"
5-
LOCKFILE_BAK="Cargo.lock.bak"
6-
LOCKFILE_LOCK=".Cargo.lock.flock"
5+
LOCKDIR=".bak"
6+
LOCKFILE_BAK="${LOCKDIR}/${LOCKFILE}"
77

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
8+
cleanup() {
9+
if [ -f "$LOCKFILE_BAK" ]; then
10+
mv "$LOCKFILE_BAK" "$LOCKFILE"
11+
fi
12+
rmdir "$LOCKDIR"
13+
}
14+
15+
if ! mkdir "$LOCKDIR" 2>/dev/null; then
16+
echo "Another instance is running. If you're sure it's not, remove $LOCKDIR and try again." >&2
17+
exit 1
18+
fi
19+
20+
trap cleanup EXIT
21+
22+
if [ -f "$LOCKFILE" ]; then
23+
mv "$LOCKFILE" "$LOCKFILE_BAK"
24+
fi
1225

13-
# Backup original lockfile
14-
if [ -f "$LOCKFILE" ]; then
15-
mv "$LOCKFILE" "$LOCKFILE_BAK"
26+
DEPS="recent minimal"
27+
CRATES="payjoin payjoin-cli payjoin-directory payjoin-ffi"
28+
29+
for dep in $DEPS; do
30+
cargo --version
31+
rustc --version
32+
33+
# Some tests require certain toolchain types.
34+
export NIGHTLY=false
35+
export STABLE=true
36+
if cargo --version | grep nightly; then
37+
STABLE=false
38+
NIGHTLY=true
1639
fi
40+
if cargo --version | grep beta; then
41+
STABLE=false
42+
fi
43+
44+
cp "Cargo-$dep.lock" "$LOCKFILE"
1745

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"
23-
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
46+
for crate in $CRATES; do
47+
(
48+
cd "$crate"
49+
./contrib/test.sh
50+
)
4751
done
52+
done
4853

49-
) 9>"$LOCKFILE_LOCK"

0 commit comments

Comments
 (0)