neuronsimulator / nrn

NEURON Simulator
http://nrn.readthedocs.io
Other
410 stars 118 forks source link

h.NetCon to integers causes h.finitialize to segfault #760

Closed Helveg closed 1 year ago

Helveg commented 4 years ago

Update: reducing the test setup further revealed that it's just h.Netcon(n,m) that cause a segfault:

>>> from neuron import h
>>> h.NetCon(0,0)
bad stack access: expecting (double *); really (double)
NEURON: interpreter stack type error
 near line 0
 objref hoc_obj_[2]
                   ^
        NetCon(0, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: hoc error
>>> h.finitialize()
Segmentation fault (core dumped)

(Original post)

Sorry for the issue titles, I have a similar one (https://github.com/neuronsimulator/nrn/issues/434), but they're all just strange segfaults that I encounter when testing, I hope the reports are valuable. If you install the dependencies and copy paste the Setup code into a python session you'll get some python errors and the segmentation fault. The setup code is made up of 2 test files I use: one to check error handling, so it tries to do all kinds of wrong things, and the other for an IClamp wrapper. Seperately they run fine, but together they cause the segfault.

Dependencies

git clone --branch section-ref https://github.com/Helveg/patch
cd patch
pip install -e .
cd
python

Setup

from patch import p
v = p.Vector()
v.__neuron__().record(p.Section())
from neuron import h
h.NetCon(5, 12)
h.NetCon(5, 12)
s = p.Section()
t = p.Vector()
p.NetCon(s, t)
s0 = p.Section()
s0.record()
s1 = p.Section()
s1.record()
s2 = p.Section()
s2.record()
s3 = p.Section()
s3.record()
s4 = p.Section()
s4.record()
p.time
s1.iclamp(amplitude=10, delay=5)
c = p.IClamp(sec=s2)
c.amp = 10
c.dur = 10
c2 = s3.iclamp(amplitude=10)
s4.iclamp(amplitude=[-10 for _ in range(int(10 / p.dt))])
p.finitialize()

Error

[robin-ZenBook-UX533FN:25480] *** Process received signal ***
[robin-ZenBook-UX533FN:25480] Signal: Segmentation fault (11)
[robin-ZenBook-UX533FN:25480] Signal code:  (9)
[robin-ZenBook-UX533FN:25480] Failing at address: 0x7f
[robin-ZenBook-UX533FN:25480] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3efd0)[0x7f85f5396fd0]
[robin-ZenBook-UX533FN:25480] [ 1] /home/robin/nrn/build/install/lib/libnrniv.so(_ZN8NetCvode11init_eventsEv+0x1c7)[0x7f85df4fd917]
[robin-ZenBook-UX533FN:25480] [ 2] /home/robin/nrn/build/install/lib/libnrniv.so(nrn_finitialize+0x3d9)[0x7f85df55a6b9]
[robin-ZenBook-UX533FN:25480] [ 3] /home/robin/nrn/build/install/lib/libnrniv.so(finitialize+0x3a)[0x7f85df55a8fa]
[robin-ZenBook-UX533FN:25480] [ 4] /home/robin/nrn/build/install/lib/libnrniv.so(hoc_call+0xc4)[0x7f85df556264]
[robin-ZenBook-UX533FN:25480] [ 5] /home/robin/nrn/build/install/lib/libnrnpython3.so(+0x1669f)[0x7f85df9df69f]
[robin-ZenBook-UX533FN:25480] [ 6] /home/robin/nrn/build/install/lib/libnrniv.so(_ZN10OcJumpImpl7fpycallEPFPvS0_S0_ES0_S0_+0x3e)[0x7f85df52700e]
[robin-ZenBook-UX533FN:25480] [ 7] /home/robin/nrn/build/install/lib/libnrnpython3.so(+0x1023b)[0x7f85df9d923b]
[robin-ZenBook-UX533FN:25480] [ 8] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(_PyObject_FastCallDict+0x89)[0x7f85f57bdf29]
[robin-ZenBook-UX533FN:25480] [ 9] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(+0x16acd9)[0x7f85f58b3cd9]
[robin-ZenBook-UX533FN:25480] [10] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x3be1)[0x7f85f58b8391]
[robin-ZenBook-UX533FN:25480] [11] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(+0x16ab7a)[0x7f85f58b3b7a]
[robin-ZenBook-UX533FN:25480] [12] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(+0x16ae12)[0x7f85f58b3e12]
[robin-ZenBook-UX533FN:25480] [13] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x3be1)[0x7f85f58b8391]
[robin-ZenBook-UX533FN:25480] [14] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(+0x16ab7a)[0x7f85f58b3b7a]
[robin-ZenBook-UX533FN:25480] [15] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(PyEval_EvalCodeEx+0x3e)[0x7f85f58b41de]
[robin-ZenBook-UX533FN:25480] [16] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(PyEval_EvalCode+0x1b)[0x7f85f58b420b]
[robin-ZenBook-UX533FN:25480] [17] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(+0x1a0e40)[0x7f85f58e9e40]
[robin-ZenBook-UX533FN:25480] [18] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(PyRun_InteractiveLoopFlags+0x86)[0x7f85f58ea0a6]
[robin-ZenBook-UX533FN:25480] [19] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(PyRun_AnyFileExFlags+0x3e)[0x7f85f58ea8be]
[robin-ZenBook-UX533FN:25480] [20] /home/robin/.pyenv/versions/3.6.10/lib/libpython3.6m.so.1.0(Py_Main+0xf92)[0x7f85f5908732]
[robin-ZenBook-UX533FN:25480] [21] /home/robin/.pyenv/versions/3.6.10/bin/python(main+0x170)[0x55ef63aafb40]
[robin-ZenBook-UX533FN:25480] [22] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f85f5379b97]
[robin-ZenBook-UX533FN:25480] [23] /home/robin/.pyenv/versions/3.6.10/bin/python(_start+0x2a)[0x55ef63aafbfa]
[robin-ZenBook-UX533FN:25480] *** End of error message ***
Segmentation fault (core dumped)
ramcdougal commented 4 years ago

What are you trying to do with h.NetCon(5, 12)? That should raise an Exception; does it not?

Helveg commented 4 years ago

Yes, most statements at the start will raise an Exception, I use them inside of unit tests that test whether I catch the RuntimeError and raise semantic Python exceptions like NetConError etc. I've reduced the setup code as much as possible to demonstrate that some of these earlier strange commands like h.NetCon(5,12) probably cause some kind of problem that isn't fixed before the exception is raised and prevent finitialize from functioning later on.

Helveg commented 4 years ago

It gets a lot simpler actually:

Python 3.6.10 (default, Mar 20 2020, 00:41:24) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from neuron import h
>>> h.NetCon(5, 12)
bad stack access: expecting (double *); really (double)
NEURON: interpreter stack type error
 near line 0
 objref hoc_obj_[2]
                   ^
        NetCon(5, 12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: hoc error
>>> h.finitialize()
Segmentation fault (core dumped)
nrnhines commented 4 years ago

These are helpful. Thanks. I will look into the cause.

nrnhines commented 4 years ago

PR #762 is a starting attempt at fixing this in general. Unfortunately HOC's original design for error recovery using longjump is not nearly as adequate as Python's method where every function returns an error indicator on failure. In the above case, the Object structure was created and added to the list of hoc objects before the argument error was noticed, which was before the Object structure was filled by the constructor. Later use of that still empty object generated the segfault. #762 fixes a lot of practical cases, but may fail if the contructor creates new hoc objects itself prior to an error, or if the destructor has trouble with unfilled or partially filled Object.

nrnhines commented 2 years ago

Perhaps this is closable because of #1989

alexsavulescu commented 1 year ago
(venv) savulesc@bbd-cjngk03:~/Workspace/nrn/build$ python
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from neuron import h
>>> h.NetCon(0,0)
bad stack access: expecting (double *); really (double)
NEURON: interpreter stack type error
 near line 0
 objref hoc_obj_[2]
                   ^
        NetCon(0, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: hocobj_call error
>>>