Skip to content

Fix set___name__ and set___qualname__ deadlock #5956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4261,7 +4261,6 @@ class C(object):
C.__name__ = 'D.E'
self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))

@unittest.skip("TODO: RUSTPYTHON, rustpython hang")
def test_evil_type_name(self):
# A badly placed Py_DECREF in type_set_name led to arbitrary code
# execution while the type structure was not in a sane state, and a
Expand Down
18 changes: 16 additions & 2 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,15 @@ impl PyType {
.heaptype_ext
.as_ref()
.expect("HEAPTYPE should have heaptype_ext");
*heap_type.qualname.write() = str_value;

// Use std::mem::replace to swap the new value in and get the old value out,
// then drop the old value after releasing the lock
let _old_qualname = {
let mut qualname_guard = heap_type.qualname.write();
std::mem::replace(&mut *qualname_guard, str_value)
};
// old_qualname is dropped here, outside the lock scope

Ok(())
}

Expand Down Expand Up @@ -837,7 +845,13 @@ impl PyType {
return Err(vm.new_value_error("type name must not contain null characters"));
}

*self.heaptype_ext.as_ref().unwrap().name.write() = name;
// Use std::mem::replace to swap the new value in and get the old value out,
// then drop the old value after releasing the lock (similar to CPython's Py_SETREF)
let _old_name = {
let mut name_guard = self.heaptype_ext.as_ref().unwrap().name.write();
std::mem::replace(&mut *name_guard, name)
};
// old_name is dropped here, outside the lock scope

Ok(())
}
Expand Down
Loading