Commit e1cbca0
committed
Fix memory safety race condition in CPython 3.13 free-threaded mode
Add per-object locking via Py_BEGIN_CRITICAL_SECTION at all public-facing
entry points in the C extension to prevent use-after-free crashes when
iterating and mutating a MultiDict concurrently.
The root cause is _md_resize() reallocating md->keys without any
synchronization, causing concurrent iterators to access freed memory.
This fix wraps all public methods in Py_BEGIN_CRITICAL_SECTION(self),
matching CPython's own dict locking model. On GIL builds the macros
are no-ops (via pythoncapi_compat.h).
Adds tests/test_free_threading.py with a concurrent stress test that
previously crashed with SIGSEGV and now completes cleanly.1 parent 7667c9e commit e1cbca0
6 files changed
Lines changed: 557 additions & 146 deletions
File tree
- CHANGES
- multidict
- _multilib
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
0 commit comments