Test that acquiring locks in different order raises error instead of deadlock.
(testrepo: Repository)
| 190 | |
| 191 | |
| 192 | def test_transaction_deadlock_prevention(testrepo: Repository) -> None: |
| 193 | """Test that acquiring locks in different order raises error instead of deadlock.""" |
| 194 | # Create two test refs |
| 195 | ref1_name = 'refs/heads/deadlock-test-1' |
| 196 | ref2_name = 'refs/heads/deadlock-test-2' |
| 197 | target = Oid(hex='5ebeeebb320790caf276b9fc8b24546d63316533') |
| 198 | testrepo.create_reference(ref1_name, target) |
| 199 | testrepo.create_reference(ref2_name, target) |
| 200 | |
| 201 | thread1_ref1_locked = threading.Event() |
| 202 | thread2_ref2_locked = threading.Event() |
| 203 | errors = [] |
| 204 | successes = [] |
| 205 | |
| 206 | def thread1_task() -> None: |
| 207 | try: |
| 208 | with testrepo.transaction() as txn: |
| 209 | txn.lock_ref(ref1_name) |
| 210 | thread1_ref1_locked.set() |
| 211 | thread2_ref2_locked.wait(timeout=5) |
| 212 | # this would cause a deadlock, so will throw (GitError) |
| 213 | txn.lock_ref(ref2_name) |
| 214 | # shouldn't get here |
| 215 | successes.append('thread1') |
| 216 | except Exception as e: |
| 217 | errors.append(('thread1', type(e).__name__, str(e))) |
| 218 | |
| 219 | def thread2_task() -> None: |
| 220 | try: |
| 221 | with testrepo.transaction() as txn: |
| 222 | txn.lock_ref(ref2_name) |
| 223 | thread2_ref2_locked.set() |
| 224 | thread1_ref1_locked.wait(timeout=5) |
| 225 | # this would cause a deadlock, so will throw (GitError) |
| 226 | txn.lock_ref(ref2_name) |
| 227 | # shouldn't get here |
| 228 | successes.append('thread2') |
| 229 | except Exception as e: |
| 230 | errors.append(('thread2', type(e).__name__, str(e))) |
| 231 | |
| 232 | thread1 = threading.Thread(target=thread1_task) |
| 233 | thread2 = threading.Thread(target=thread2_task) |
| 234 | |
| 235 | thread1.start() |
| 236 | thread2.start() |
| 237 | thread1.join(timeout=5) |
| 238 | thread2.join(timeout=5) |
| 239 | |
| 240 | # At least one thread should fail with an error (not deadlock) |
| 241 | # If both threads are still alive, we have a deadlock |
| 242 | assert not thread1.is_alive(), 'Thread 1 deadlocked' |
| 243 | assert not thread2.is_alive(), 'Thread 2 deadlocked' |
| 244 | |
| 245 | # Both can't succeed. |
| 246 | # libgit2 doesn't *wait* for locks, so it's possible for neither to succeed |
| 247 | # if they both try to take the second lock at basically the same time. |
| 248 | # The other possibility is that one thread throws, exits its transaction, |
| 249 | # and the other thread is able to acquire the second lock. |
nothing calls this directly
no test coverage detected
searching dependent graphs…