Skip to content

locks.py: composition over inheritance for dummy lock#52499

Open
haampie wants to merge 5 commits into
developfrom
hs/fix/locks-noop
Open

locks.py: composition over inheritance for dummy lock#52499
haampie wants to merge 5 commits into
developfrom
hs/fix/locks-noop

Conversation

@haampie
Copy link
Copy Markdown
Member

@haampie haampie commented Jun 6, 2026

Replaces #52488 and #52479.

Condense the if self._enabled branches into a single one in the constructor, instead of sprinkling them in every method. It works by extracting the syscalls to a backend class in a composition over inheritance style. On Windows the backend is a no-op version.

This is best reviewed commit by commit, with git show --color-moved=dimmed-zebra c94402845c6eda4fb9a36108a6037a68880a9507 for the commit that moves code.

In a follow-up PR we could use a mock backend in tests to drop fcntl monkeypatching.

@haampie haampie force-pushed the hs/fix/locks-noop branch 2 times, most recently from a684fed to 3ca9255 Compare June 6, 2026 08:31
@haampie haampie added this to the v1.2.0 milestone Jun 6, 2026
@haampie haampie force-pushed the hs/fix/locks-noop branch from 3ca9255 to 079c59c Compare June 6, 2026 14:00
@haampie haampie force-pushed the hs/fix/locks-noop branch 3 times, most recently from c88946d to 343c81d Compare June 6, 2026 14:10
@haampie haampie changed the title locks.py: PosixBackend, DummyBackend, merge modules locks.py: composition over inheritance for dummy lock Jun 6, 2026
@haampie haampie force-pushed the hs/fix/locks-noop branch from 343c81d to 91752e4 Compare June 6, 2026 14:32
@johnwparent johnwparent self-assigned this Jun 6, 2026
@haampie haampie force-pushed the hs/fix/locks-noop branch 3 times, most recently from 1105ac1 to b25f01e Compare June 7, 2026 07:08
haampie added 4 commits June 7, 2026 16:25
Prep for backend extraction: makes _poll_lock body byte-identical to the
future PosixBackend.poll so the move is reviewable. _log_debug is just
tty.debug(*args, level=kwargs.get("level", 2)).

Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Pure rename + small wrapper:

- Add Lock.prepare(op) bundling _ensure_valid_handle() + the LOCK_EX/rb
  LockROFileError check that was inlined in _lock and try_acquire_write.
- Rename Lock._poll_lock -> Lock.poll.
- Rename Lock._unlock -> Lock.release.

The subclass in spack.util.lock and the _poll_lock test are updated to
match. No behavior change.

Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Move the fcntl-based primitives (_ensure_valid_handle, prepare, poll,
_read_log_debug_data, _write_log_debug_data, release, cleanup) and the
pid/host/_file_ref/_cached_key state off Lock and onto a new
PosixBackend. Add a DummyBackend with no-op equivalents.

Lock now delegates to self.backend (PosixBackend on POSIX, DummyBackend
on Windows). Method bodies are unchanged; review with
--color-moved=zebra --color-moved-ws=allow-indentation-change.

Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Lock.__init__ now takes enable: bool = True; when False (or on Windows)
it uses DummyBackend. spack.util.lock no longer needs its own subclass
to disable locking, so it just re-exports Lock from spack.llnl.util.lock.

Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
@haampie haampie force-pushed the hs/fix/locks-noop branch from b25f01e to bc4af55 Compare June 7, 2026 14:27
becker33
becker33 previously approved these changes Jun 7, 2026
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants