Skip to content

Commit c2e7855

Browse files
committed
🐛 fix(api): detect a peer hold while polling
A signal handler can acquire a peer after the initial deadlock check. Check the pinned path again before waiting on that peer. Fixes #1083
1 parent 794e0f2 commit c2e7855

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎src/filelock/_api.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,7 @@ def lock_file(self) -> str:
11171117
def _raise_if_would_deadlock(self, canonical: str, *, timeout: float, blocking: bool) -> None:
11181118
# Fail fast when a *different* live instance already holds this path in the current deadlock scope.
11191119
#
1120-
# Only the first, indefinitely-blocking acquire can self-deadlock this way: waiting in the OS primitive would
1121-
# block on a lock this flow already owns. A finite timeout or ``blocking=False`` keeps the normal Timeout path.
1120+
# A handler can acquire a peer between attempts. Waiting on that hold would block the same thread.
11221121
would_block = self._context.lock_counter == 1 and not self.is_locked and timeout < 0 and blocking
11231122
if would_block and _REGISTRY.held.get(self._registry_key(canonical)) not in {None, self._holder_id}:
11241123
msg = (
@@ -1174,6 +1173,7 @@ def _poll_until_acquired(
11741173
if self.is_locked:
11751174
_LOGGER.debug("Lock %s acquired on %s", lock_id, lock_filename)
11761175
return
1176+
self._raise_if_would_deadlock(self._lock_path, timeout=timeout, blocking=blocking)
11771177
if self._check_give_up(
11781178
blocking=blocking,
11791179
cancel_check=cancel_check,

‎tests/test_filelock.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,48 @@ def reenter(_canonical: str, *, timeout: float, blocking: bool) -> None:
20482048
assert (lock.is_locked, lock.lock_counter) == (False, 0)
20492049

20502050

2051+
@NEEDS_PROMPT_SIGNAL_DELIVERY # pragma: needs prompt-signal-delivery
2052+
@pytest.mark.parametrize("thread_local", [pytest.param(True, id="thread_local"), pytest.param(False, id="shared")])
2053+
@pytest.mark.parametrize(
2054+
("timeout", "blocking", "expected"),
2055+
[
2056+
pytest.param(-1, True, RuntimeError, id="indefinite"),
2057+
pytest.param(1, True, Timeout, id="finite"),
2058+
pytest.param(-1, False, Timeout, id="nonblocking"),
2059+
],
2060+
)
2061+
def test_handler_acquiring_peer_during_poll_detects_deadlock(
2062+
tmp_path: Path, mocker: MockerFixture, thread_local: bool, timeout: float, blocking: bool, expected: type[Exception]
2063+
) -> None:
2064+
path = str(tmp_path / "a")
2065+
outer = SoftFileLock(path, thread_local=thread_local, is_singleton=False)
2066+
peer = SoftFileLock(path, thread_local=thread_local, is_singleton=False)
2067+
original = outer._acquire_with_fork_tracking
2068+
pending = [signal.SIGINT]
2069+
2070+
def signal_then_acquire() -> None:
2071+
signals = pending.copy()
2072+
pending.clear()
2073+
list(map(signal.raise_signal, signals))
2074+
original()
2075+
2076+
def handoff(_signum: int, _frame: FrameType | None) -> None:
2077+
peer.acquire(blocking=False)
2078+
2079+
mocker.patch.object(outer, "_acquire_with_fork_tracking", side_effect=signal_then_acquire, autospec=True)
2080+
previous = signal.signal(signal.SIGINT, handoff)
2081+
try:
2082+
with pytest.raises(expected):
2083+
outer.acquire(timeout=timeout, blocking=blocking, cancel_check=lambda: True)
2084+
assert (outer.is_locked, peer.is_locked) == (False, True)
2085+
with pytest.raises(RuntimeError, match="already held by a different"):
2086+
SoftFileLock(path, is_singleton=False).acquire(cancel_check=lambda: True)
2087+
finally:
2088+
signal.signal(signal.SIGINT, previous)
2089+
peer.release()
2090+
outer.release()
2091+
2092+
20512093
@NEEDS_PROMPT_SIGNAL_DELIVERY # pragma: needs prompt-signal-delivery
20522094
def test_acquire_from_a_signal_handler_during_an_acquire_fails_fast(tmp_path: Path, mocker: MockerFixture) -> None:
20532095
lock: Final[SoftFileLock] = SoftFileLock(str(tmp_path / "a"))

0 commit comments

Comments
 (0)