Skip to content
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

BugFix: Don't INCREF a Py section just allocated #3039

Merged
merged 15 commits into from
Sep 19, 2024
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
15 changes: 2 additions & 13 deletions src/nrnpython/nrnpy_nrn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,20 +1881,9 @@ static PyObject* mech_of_segment_iter_safe(NPySegObj* self) {
static Object* seg_from_sec_x(Section* sec, double x) {
auto pyseg = nb::steal((PyObject*) PyObject_New(NPySegObj, psegment_type));
auto* pseg = (NPySegObj*) pyseg.ptr();
auto* pysec = static_cast<NPySecObj*>(sec->prop->dparam[PROP_PY_INDEX].get<void*>());
if (pysec) {
pseg->pysec_ = pysec;
Py_INCREF(pysec);
} else {
pysec = (NPySecObj*) psection_type->tp_alloc(psection_type, 0);
pysec->sec_ = sec;
pysec->name_ = 0;
pysec->cell_weakref_ = 0;
Py_INCREF(pysec);
pseg->pysec_ = pysec;
}
pseg->pysec_ = newpysechelp(sec); // newpysechelp() already increfs
pseg->x_ = x;
return nrnpy_pyobject_in_obj(pyseg.ptr());
return nrnpy_pyobject_in_obj((PyObject*) pseg);
}

static Object** pp_get_segment(void* vptr) {
Expand Down
55 changes: 55 additions & 0 deletions test/unit_tests/hoc_python/test_refcounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from neuron import h, nrn
import gc
import sys


def test_seg_from_sec_x_ref_python():
"""
A reproducer of calling `seg_from_sec_x` with a Python Section
"""
soma1 = nrn.Section("soma1")
nc1 = h.NetCon(soma1(0.5)._ref_v, None)
seg1 = nc1.preseg()
del soma1

assert sys.getrefcount(seg1) == 2
# It is unclear why we get 3 ref-counts when we use plain python. With nrniv we get 2.
# At least it is consistent with the test below and we assert that the section
# really gets deleted (in hoc)
assert sys.getrefcount(seg1.sec) == 3 # seg->pysec_ + 1

del seg1 # NOTE: section fully destroyed together with segment
gc.collect()

h("n_sections=0")
h("forall { n_sections+=1 }")
h('soma_is_valid=section_exists("soma1")')
assert h.n_sections == 0
assert not h.soma_is_valid


def test_seg_from_sec_x_ref_hocpy():
"""A reproducer of calling `seg_from_sec_x` without a Python Section

Without the fix we would get one extra ref to sec and possibly (upon interpreter exit):
"Fatal Python error: bool_dealloc: "deallocating True or False: bug likely caused by a
refcount error in a C extension"
"""
h("create soma")
h("objref nc")
h("objref nil")
h("soma nc = new NetCon(&v(0.5), nil)")
nc = h.nc
seg = nc.preseg() # uses `seg_from_sec_x`

assert sys.getrefcount(seg) == 2
assert sys.getrefcount(seg.sec) == 3 # seg->pysec_ + 1

del seg # NOTE: section py wrapper destroyed with segment, hoc-level section kept
gc.collect()

h("n_sections=0")
h("forall { n_sections+=1 }")
h('soma_is_valid=section_exists("soma")')
assert h.n_sections == 1
assert h.soma_is_valid
Loading