Skip to content

Commit e24318f

Browse files
committed
gh-158194: Pin the type lock in PyType_Freeze()
PyType_Freeze() stopped the world under the type lock without pinning it, so the lock could be released while waiting for the pause. Pin it with type_lock_prevent_release() like the other type-lock callers of types_stop_world().
1 parent 00a1c3a commit e24318f

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix :c:func:`PyType_Freeze` releasing the type lock while waiting for a
2+
stop-the-world pause in the :term:`free-threaded build`. Another thread could
3+
modify the type before the frozen type's caches were invalidated, and the
4+
release could deadlock against a thread waiting for the pause with the type
5+
lock held.

‎Objects/typeobject.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12463,10 +12463,13 @@ PyType_Freeze(PyTypeObject *type)
1246312463
return -1;
1246412464
}
1246512465

12466+
pinned_mutexes_t pinned;
1246612467
BEGIN_TYPE_LOCK();
12468+
type_lock_prevent_release(&pinned);
1246712469
types_stop_world();
1246812470
type_add_flags(type, Py_TPFLAGS_IMMUTABLETYPE);
1246912471
types_start_world();
12472+
type_lock_allow_release(&pinned);
1247012473
ASSERT_TYPE_LOCK_HELD();
1247112474
_PyType_Modified_Unlocked(type);
1247212475
END_TYPE_LOCK();

0 commit comments

Comments
 (0)