From c446566f42fd8083119598beda574b6197352767 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Mon, 26 Oct 2026 12:21:09 -0400 Subject: [PATCH] fix: hold a setup lock instead of guessing from running processes --- .../usr/local/bin/droiddeck-flatpak-setup | 5 ++-- tools/tests/test_flatpak_setup.py | 26 ++++++++----------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-setup b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-setup index 9717da5ba..534a4f7d3 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-setup +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-setup @@ -25,6 +25,7 @@ NEEDS WORK=/var/cache/droiddeck-flatpak CONF=$WORK/pacman.conf mkdir -p "$WORK/pkg" "$WORK/db" || fail "cannot create $WORK" +exec 9>"$WORK/setup.lock" && flock -n 9 || fail "another Flatpak setup is still running" OVERSEAS="https://ca.us.mirror.archlinuxarm.org https://fl.us.mirror.archlinuxarm.org https://de3.mirror.archlinuxarm.org https://nl.mirror.archlinuxarm.org" MAINLAND="" @@ -49,9 +50,7 @@ if [ ! -f /etc/pacman.d/gnupg/pubring.gpg ] && [ ! -f /etc/pacman.d/gnupg/pubrin fi step "Reading the package lists" -if [ -e "$WORK/db/db.lck" ] && ! grep -qx pacman /proc/[0-9]*/comm 2>/dev/null; then - rm -f "$WORK/db/db.lck" -fi +rm -f "$WORK/db/db.lck" sync_log=$(pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy 2>&1) \ || sync_log=$(pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy 2>&1) if [ ! -f "$WORK/db/sync/extra.db" ]; then diff --git a/tools/tests/test_flatpak_setup.py b/tools/tests/test_flatpak_setup.py index 5ad712efe..7de2d0f7a 100644 --- a/tools/tests/test_flatpak_setup.py +++ b/tools/tests/test_flatpak_setup.py @@ -1,3 +1,5 @@ +import contextlib +import fcntl import os from pathlib import Path import subprocess @@ -9,12 +11,15 @@ SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/dr class FlatpakSetupTest(unittest.TestCase): - def run_setup(self, uid, pacman, locked=False): - with tempfile.TemporaryDirectory() as directory: + def run_setup(self, uid, pacman, locked=False, busy=False): + with tempfile.TemporaryDirectory() as directory, contextlib.ExitStack() as held: root = Path(directory) if locked: (root / "work/db").mkdir(parents=True) (root / "work/db/db.lck").write_text("") + if busy: + holder = held.enter_context(open(root / "work/setup.lock", "w")) + fcntl.flock(holder, fcntl.LOCK_EX | fcntl.LOCK_NB) bin_dir = root / "bin" bin_dir.mkdir() stubs = {"id": "echo %d" % uid, "curl": "exit 7", "pacman": pacman, "pacman-key": "exit 0"} @@ -52,20 +57,11 @@ class FlatpakSetupTest(unittest.TestCase): self.assertNotIn("unable to lock database", out) self.assertIn("== STEP Downloading Flatpak", out) - def test_a_lock_held_by_a_running_pacman_is_kept(self): - with tempfile.TemporaryDirectory() as directory: - holder = Path(directory) / "pacman" - holder.write_bytes(Path("/bin/sleep").read_bytes()) - holder.chmod(0o755) - running = subprocess.Popen([str(holder), "30"]) - try: - code, out = self.run_setup(0, self.SYNC, locked=True) - finally: - running.kill() - running.wait() + def test_a_setup_that_is_still_running_keeps_its_lock(self): + code, out = self.run_setup(0, self.SYNC, locked=True, busy=True) self.assertEqual(code, 1) - self.assertIn("unable to lock database", out) - + self.assertIn("== FAIL another Flatpak setup is still running", out) + self.assertNotIn("Reading the package lists", out) if __name__ == "__main__": unittest.main()