Skip to content

Commit 018ad08

Browse files
authored
fix: CombinedLock.locked() now correctly calls lock.locked() method (Fixes #10843) (#11022)
1 parent 8d271fb commit 018ad08

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

xarray/backends/locks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def __exit__(self, *args):
233233
lock.__exit__(*args)
234234

235235
def locked(self):
236-
return any(lock.locked for lock in self.locks)
236+
return any(lock.locked() for lock in self.locks)
237237

238238
def __repr__(self):
239239
return f"CombinedLock({list(self.locks)!r})"

xarray/tests/test_backends_locks.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import threading
44

55
from xarray.backends import locks
6+
from xarray.backends.locks import CombinedLock, SerializableLock
67

78

89
def test_threaded_lock() -> None:
@@ -13,3 +14,77 @@ def test_threaded_lock() -> None:
1314

1415
lock3 = locks._get_threaded_lock("bar")
1516
assert lock1 is not lock3
17+
18+
19+
def test_combined_lock_locked_returns_false_when_no_locks_acquired() -> None:
20+
"""CombinedLock.locked() should return False when no locks are held."""
21+
lock1 = threading.Lock()
22+
lock2 = threading.Lock()
23+
combined = CombinedLock([lock1, lock2])
24+
25+
assert combined.locked() is False
26+
assert lock1.locked() is False
27+
assert lock2.locked() is False
28+
29+
30+
def test_combined_lock_locked_returns_true_when_one_lock_acquired() -> None:
31+
"""CombinedLock.locked() should return True when any lock is held."""
32+
lock1 = threading.Lock()
33+
lock2 = threading.Lock()
34+
combined = CombinedLock([lock1, lock2])
35+
36+
lock1.acquire()
37+
try:
38+
assert combined.locked() is True
39+
finally:
40+
lock1.release()
41+
42+
assert combined.locked() is False
43+
44+
45+
def test_combined_lock_locked_returns_true_when_all_locks_acquired() -> None:
46+
"""CombinedLock.locked() should return True when all locks are held."""
47+
lock1 = threading.Lock()
48+
lock2 = threading.Lock()
49+
combined = CombinedLock([lock1, lock2])
50+
51+
lock1.acquire()
52+
lock2.acquire()
53+
try:
54+
assert combined.locked() is True
55+
finally:
56+
lock1.release()
57+
lock2.release()
58+
59+
assert combined.locked() is False
60+
61+
62+
def test_combined_lock_locked_with_serializable_locks() -> None:
63+
"""CombinedLock.locked() should work with SerializableLock instances."""
64+
lock1 = SerializableLock()
65+
lock2 = SerializableLock()
66+
combined = CombinedLock([lock1, lock2])
67+
68+
assert combined.locked() is False
69+
70+
lock1.acquire()
71+
try:
72+
assert combined.locked() is True
73+
finally:
74+
lock1.release()
75+
76+
assert combined.locked() is False
77+
78+
79+
def test_combined_lock_locked_with_context_manager() -> None:
80+
"""CombinedLock.locked() should reflect state when using context manager."""
81+
lock1 = threading.Lock()
82+
lock2 = threading.Lock()
83+
combined = CombinedLock([lock1, lock2])
84+
85+
assert combined.locked() is False
86+
87+
with combined:
88+
assert combined.locked() is True
89+
90+
assert combined.locked() is False

0 commit comments

Comments
 (0)