Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add atomic snapshot for dict/dict_keys in extract_elements
Add fast paths for dict and dict_keys types in
extract_elements_with, matching _list_extend() in CPython
Objects/listobject.c. Each branch takes an atomic snapshot
under a single read lock, preventing race conditions from
concurrent dict mutation without the GIL.

Remove expectedFailure from test_thread_safety.
  • Loading branch information
youknowone committed Mar 25, 2026
commit b2aef250bb39980302576c3b8886e6b787362fb7
2 changes: 0 additions & 2 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4814,8 +4814,6 @@ def test_finalize(self):
self.assertEqual(result, ['a', 'b', 'd10', 'd03', 'd02', 'd01', 'e'])

@support.requires_resource('cpu')
# TODO: RUSTPYTHON; dict iteration races with concurrent GC mutations
@unittest.expectedFailure
def test_thread_safety(self):
# bpo-24484: _run_finalizers() should be thread-safe
def cb():
Expand Down
14 changes: 10 additions & 4 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
self, PyBaseExceptionRef, PyDict, PyDictRef, PyInt, PyList, PyModule, PyStr, PyStrInterned,
PyStrRef, PyTypeRef, PyUtf8Str, PyUtf8StrInterned, PyWeak,
code::PyCode,
dict::{PyDictItems, PyDictValues},
dict::{PyDictItems, PyDictKeys, PyDictValues},
pystr::AsPyStr,
tuple::PyTuple,
},
Expand Down Expand Up @@ -1822,24 +1822,30 @@ impl VirtualMachine {
where
F: Fn(PyObjectRef) -> PyResult<T>,
{
// Extract elements from item, if possible:
// Type-specific fast paths corresponding to _list_extend() in CPython
// Objects/listobject.c. Each branch takes an atomic snapshot to avoid
// race conditions from concurrent mutation (no GIL).
let cls = value.class();
let list_borrow;
let slice = if cls.is(self.ctx.types.tuple_type) {
value.downcast_ref::<PyTuple>().unwrap().as_slice()
} else if cls.is(self.ctx.types.list_type) {
list_borrow = value.downcast_ref::<PyList>().unwrap().borrow_vec();
&list_borrow
} else if cls.is(self.ctx.types.dict_type) {
let keys = value.downcast_ref::<PyDict>().unwrap().keys_vec();
return keys.into_iter().map(func).collect();
} else if cls.is(self.ctx.types.dict_keys_type) {
let keys = value.downcast_ref::<PyDictKeys>().unwrap().dict.keys_vec();
return keys.into_iter().map(func).collect();
} else if cls.is(self.ctx.types.dict_values_type) {
// Atomic snapshot of dict values - prevents race condition during iteration
let values = value
.downcast_ref::<PyDictValues>()
.unwrap()
.dict
.values_vec();
return values.into_iter().map(func).collect();
} else if cls.is(self.ctx.types.dict_items_type) {
// Atomic snapshot of dict items - prevents race condition during iteration
let items = value
.downcast_ref::<PyDictItems>()
.unwrap()
Expand Down
Loading