You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Whenever a grad function raises an exception instead of displaying the python exception this program generates a segfaut (this is an aritificial example I cooked up from a real dde that was segfaulting for the same reason after iteration 100):
from __future__ importdivisionimportnumpyasnpimportscipyasspimporttimefromscipy.specialimportbinomfromPyDDEimportpyddetau=40d0=1/100G=10** (-2)
a=0.8A=100# NB: B * x_0 < ln(2) for boundaries in U_x to be definedB=0.06x_0=3t_delay=50ddecons=np.array([tau, d0, G, a, A, B, x_0, t_delay])
# Duration of the simulation:stoptime=1000.0numpoints=1000timestep=1.0# this is the timesteps on which the output is givendt=0.1# this is the integration timestep of the solver# Initial ConditionsN0=0.01No0=10N_infty0=0ddeist=np.array([N0, No0, N_infty0])
# Set state-scaling array for use in error control when values are very close to 0ddestsc=np.array([0,0,0])
# Create a dde objectdde_eg=pydde.dde()
defddegrad(s, c, t):
ifint(s[0]) !=1 :
returnnp.array([1,1,1])
raiseBaseException()
ts=np.arange(0.0, stoptime, timestep)
dde_eg.dde(y=ddeist, times=ts,
func=ddegrad, parms=ddecons,
tol=0.000005, dt=dt, hbsize=3000, nlag=0, ssc=ddestsc)
looking at the source (inside wrapper.c) the missing of the exception seems to take place here (pyarraycheck is not working it seems):
Whenever using PyEval_CallObject it is good to check that it does not return NULL. If it returns NULL this means that an exception occurred within grad_func in python and we should EXPLICITLY call the exception (i.e. https://docs.python.org/3.0/extending/extending.html ) otherwise this will most probably lead to a segfault which it does dblArray_from_PyArray is expecting a pyobject sequence and when passing it to numpy to cast to a contiguous array it raises a segfault since it is receiving a NULL object and not a sequence.
This makes it very hard for users to debug what is going on as it my not be obvious why their gradient function is raising an exception since it jumps to a segfault and not the exception message.
The text was updated successfully, but these errors were encountered:
PS. the array check and asserts are not really working object still gets through to the pyarray contiguous function when an exception is raised by grad.
I can see attempts to handle this in the source however an exception from grads seems to make it through.
If input an array pyArrayCheck seems to return 1 , if input a pyobject that is not an array it returns 0 however if it is input NULL it somehow returns NULL . assert(NULL) does not do anything thus the issue.
@franciscovargas did you find a way to fix the segfaults? I also noticed that sometimes KeyboardInterrupt is not handled correctly and also results in a a segfault.
Whenever a grad function raises an exception instead of displaying the python exception this program generates a segfaut (this is an aritificial example I cooked up from a real dde that was segfaulting for the same reason after iteration 100):
looking at the source (inside wrapper.c) the missing of the exception seems to take place here (pyarraycheck is not working it seems):
Whenever using PyEval_CallObject it is good to check that it does not return NULL. If it returns NULL this means that an exception occurred within grad_func in python and we should EXPLICITLY call the exception (i.e. https://docs.python.org/3.0/extending/extending.html ) otherwise this will most probably lead to a segfault which it does
dblArray_from_PyArray
is expecting a pyobject sequence and when passing it to numpy to cast to a contiguous array it raises a segfault since it is receiving a NULL object and not a sequence.This makes it very hard for users to debug what is going on as it my not be obvious why their gradient function is raising an exception since it jumps to a segfault and not the exception message.
The text was updated successfully, but these errors were encountered: