Skip to content

Commit af7ec41

Browse files
committed
Prevent edge case race condition, hopefully
1 parent 229a9c8 commit af7ec41

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ once_cell = "1.4.1"
2020
siphasher = "0.3"
2121
rand = "0.8"
2222
volatile = "0.3"
23-
radium = "0.6"
23+
radium = "0.7"
2424
libc = "0.2.101"
2525
ascii = "1.0"
2626
unic-ucd-category = "0.9"

common/src/refcount.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ impl RefCount {
3838
}
3939
}
4040

41+
/// Returns true if successful
42+
#[inline]
43+
pub fn safe_inc(&self) -> bool {
44+
self.strong
45+
.fetch_update(AcqRel, Acquire, |prev| (prev != 0).then(|| prev + 1))
46+
.is_ok()
47+
}
48+
4149
/// Decrement the reference count. Returns true when the refcount drops to 0.
4250
#[inline]
4351
pub fn dec(&self) -> bool {

vm/src/pyobjectrc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,12 @@ cfg_if::cfg_if! {
314314

315315
impl PyWeak {
316316
pub(crate) fn upgrade(&self) -> Option<PyObjectRef> {
317-
// TODO: figure out orderings for here, in drop, and the store in WeakRefList::clear
318317
let guard = unsafe { self.parent.as_ref().lock() };
319318
let obj_ptr = guard.obj?;
320319
unsafe {
321-
obj_ptr.as_ref().0.refcount.inc();
320+
if !obj_ptr.as_ref().0.refcount.safe_inc() {
321+
return None;
322+
}
322323
Some(PyObjectRef::from_raw(obj_ptr.as_ptr()))
323324
}
324325
}

0 commit comments

Comments
 (0)