Skip to content

fix: use from_borrowed_ptr for Py_None to prevent invalid DECREF - #113

Merged
awolverp merged 1 commit into
mainfrom
fix/none-dealloc
Oct 28, 2026
Merged

awolverp merged 1 commit into
mainfrom
fix/none-dealloc

Conversation

@awolverp

Copy link
Copy Markdown
Owner

Bound::from_owned_ptr was used on Py_None(). This incorrectly takes ownership of the immortal None singleton.

When the Bound is dropped it issues a Py_DECREF. On some Python versions / builds this causes Python to attempt to deallocate None and panic.

None is immortal on CPython 3.12+, but the incorrect ownership is still wrong and can panic on other builds or older Python.

Fix

Replace from_owned_ptr with from_borrowed_ptr so the INCREF/DECREF pair is balanced.

// before
Ok(pyo3::Bound::from_owned_ptr(py, pyo3::ffi::Py_None()).unbind())

// after
Ok(pyo3::Bound::from_borrowed_ptr(py, pyo3::ffi::Py_None()).unbind())

@awolverp
awolverp merged commit f765863 into main Oct 28, 2026
12 checks passed
@awolverp
awolverp deleted the fix/none-dealloc branch October 29, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant