From 4795640f7e13726e33b049c8a971db752a1921af Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Mon, 2 Jun 2025 14:46:11 -0600 Subject: [PATCH] avoid extra refcount increment in GetField When returning the payload of a non-`None` `Optional` (i.e. just the value we were passed) in `GetField`, we were accidentally incrementing its refcount, resulting in a leak. Fixes #152 Signed-off-by: Joel Dice --- runtime/src/lib.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 359fc34..c9a9640 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -672,7 +672,10 @@ pub extern "C" fn componentize_py_get_field<'a>( .into_pyobject(*py) .unwrap() .into_any(), - PAYLOAD_FIELD_INDEX => value.to_owned(), + PAYLOAD_FIELD_INDEX => unsafe { + // Avoid incrementing `value`'s refcount while returning it: + Bound::from_owned_ptr(*py, value.as_ptr()) + }, _ => unreachable!(), }, Type::NestingOption => match i32::try_from(field).unwrap() { @@ -682,7 +685,11 @@ pub extern "C" fn componentize_py_get_field<'a>( .into_any(), PAYLOAD_FIELD_INDEX => { if value.is_none() { - value.to_owned() + unsafe { + // Avoid incrementing `value`'s refcount while returning + // it: + Bound::from_owned_ptr(*py, value.as_ptr()) + } } else { value.getattr("value").unwrap() }