Skip to content

Commit e91bc11

Browse files
authored
Fix dict race condition (#6720)
1 parent 4d7a289 commit e91bc11

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎crates/vm/src/builtins/dict.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ impl PyDict {
6262
&self.entries
6363
}
6464

65+
/// Returns all keys as a Vec, atomically under a single read lock.
66+
/// Thread-safe: prevents "dictionary changed size during iteration" errors.
67+
pub fn keys_vec(&self) -> Vec<PyObjectRef> {
68+
self.entries.keys()
69+
}
70+
71+
/// Returns all values as a Vec, atomically under a single read lock.
72+
/// Thread-safe: prevents "dictionary changed size during iteration" errors.
73+
pub fn values_vec(&self) -> Vec<PyObjectRef> {
74+
self.entries.values()
75+
}
76+
77+
/// Returns all items as a Vec, atomically under a single read lock.
78+
/// Thread-safe: prevents "dictionary changed size during iteration" errors.
79+
pub fn items_vec(&self) -> Vec<(PyObjectRef, PyObjectRef)> {
80+
self.entries.items()
81+
}
82+
6583
// Used in update and ior.
6684
pub(crate) fn merge_object(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
6785
let casted: Result<PyRefExact<Self>, _> = other.downcast_exact(vm);

‎crates/vm/src/dict_inner.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,22 @@ impl<T: Clone> Dict<T> {
553553
.collect()
554554
}
555555

556+
pub fn values(&self) -> Vec<T> {
557+
self.read()
558+
.entries
559+
.iter()
560+
.filter_map(|v| v.as_ref().map(|v| v.value.clone()))
561+
.collect()
562+
}
563+
564+
pub fn items(&self) -> Vec<(PyObjectRef, T)> {
565+
self.read()
566+
.entries
567+
.iter()
568+
.filter_map(|v| v.as_ref().map(|v| (v.key.clone(), v.value.clone())))
569+
.collect()
570+
}
571+
556572
pub fn try_fold_keys<Acc, Fold>(&self, init: Acc, f: Fold) -> PyResult<Acc>
557573
where
558574
Fold: FnMut(Acc, &PyObject) -> PyResult<Acc>,

‎crates/vm/src/vm/mod.rs‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ use crate::{
2020
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
2121
builtins::{
2222
PyBaseExceptionRef, PyDict, PyDictRef, PyInt, PyList, PyModule, PyStr, PyStrInterned,
23-
PyStrRef, PyTypeRef, code::PyCode, pystr::AsPyStr, tuple::PyTuple,
23+
PyStrRef, PyTypeRef,
24+
code::PyCode,
25+
dict::{PyDictItems, PyDictKeys, PyDictValues},
26+
pystr::AsPyStr,
27+
tuple::PyTuple,
2428
},
2529
codecs::CodecsRegistry,
2630
common::{hash::HashSecret, lock::PyMutex, rc::PyRc},
@@ -808,6 +812,29 @@ impl VirtualMachine {
808812
} else if cls.is(self.ctx.types.list_type) {
809813
list_borrow = value.downcast_ref::<PyList>().unwrap().borrow_vec();
810814
&list_borrow
815+
} else if cls.is(self.ctx.types.dict_keys_type) {
816+
// Atomic snapshot of dict keys - prevents race condition during iteration
817+
let keys = value.downcast_ref::<PyDictKeys>().unwrap().dict.keys_vec();
818+
return keys.into_iter().map(func).collect();
819+
} else if cls.is(self.ctx.types.dict_values_type) {
820+
// Atomic snapshot of dict values - prevents race condition during iteration
821+
let values = value
822+
.downcast_ref::<PyDictValues>()
823+
.unwrap()
824+
.dict
825+
.values_vec();
826+
return values.into_iter().map(func).collect();
827+
} else if cls.is(self.ctx.types.dict_items_type) {
828+
// Atomic snapshot of dict items - prevents race condition during iteration
829+
let items = value
830+
.downcast_ref::<PyDictItems>()
831+
.unwrap()
832+
.dict
833+
.items_vec();
834+
return items
835+
.into_iter()
836+
.map(|(k, v)| func(self.ctx.new_tuple(vec![k, v]).into()))
837+
.collect();
811838
} else {
812839
return self.map_py_iter(value, func);
813840
};

0 commit comments

Comments
 (0)