Commit f4dee84
committed
fix: restore try-then-block for PyThread_type_lock to prevent deadlock under GIL
4443f28 incorrectly simplified the 3.9-3.12 PyThread_type_lock path to
unconditional WAIT_LOCK. This is unsafe under GIL:
PyThread_acquire_lock(lock, WAIT_LOCK) does NOT release the GIL
while waiting, unlike PyMutex_Lock (3.13+) which passes _PY_LOCK_DETACH
and releases GIL when parking.
Deadlock scenario:
1. T1 holds object lock (large data path, GIL released)
2. T2 holds GIL, enters small data path, calls WAIT_LOCK → blocks with GIL
3. T1 finishes hash, tries to reacquire GIL → blocked
4. T2 waits for object lock, T1 waits for GIL → deadlock
Fix: restore the try-then-block in XXHASH_LOCK_ACQUIRE (used under GIL):
- First try NOWAIT_LOCK (fast path)
- If contested, release GIL (Py_BEGIN_ALLOW_THREADS), then WAIT_LOCK
- Reacquire GIL (Py_END_ALLOW_THREADS)
XXHASH_LOCK_ACQUIRE_BLOCKING (used without GIL, large data path) keeps
simple WAIT_LOCK since GIL is already released.
3.13+ PyMutex path remains unconditional (safe due to _PY_LOCK_DETACH).1 parent 4443f28 commit f4dee84
1 file changed
Lines changed: 37 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
56 | 83 | | |
57 | | - | |
| 84 | + | |
58 | 85 | | |
59 | 86 | | |
60 | 87 | | |
61 | 88 | | |
| 89 | + | |
62 | 90 | | |
63 | 91 | | |
64 | 92 | | |
| |||
608 | 636 | | |
609 | 637 | | |
610 | 638 | | |
611 | | - | |
| 639 | + | |
612 | 640 | | |
613 | | - | |
| 641 | + | |
614 | 642 | | |
615 | 643 | | |
616 | 644 | | |
617 | 645 | | |
618 | | - | |
| 646 | + | |
619 | 647 | | |
620 | 648 | | |
621 | 649 | | |
| |||
0 commit comments