Skip to content

Commit eb337e5

Browse files
authored
Merge pull request RustPython#4273 from discord9/fix_dict
Allow retry if `as_mut` fail when insert Dict
2 parents cac9918 + 6a66ae2 commit eb337e5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

vm/src/dictdatatype.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,13 @@ impl<T: Clone> Dict<T> {
248248
if let Some(index) = entry_index.index() {
249249
// Update existing key
250250
if let Some(entry) = inner.entries.get_mut(index) {
251-
let entry = entry
252-
.as_mut()
253-
.expect("The dict was changed since we did lookup.");
251+
let Some(entry) = entry.as_mut() else {
252+
// The dict was changed since we did lookup. Let's try again.
253+
// this is very rare to happen
254+
// (and seems only happen with very high freq gc, and about one time in 10000 iters)
255+
// but still possible
256+
continue;
257+
};
254258
if entry.index == index_index {
255259
let removed = std::mem::replace(&mut entry.value, value);
256260
// defer dec RC

0 commit comments

Comments
 (0)