Debugging some cython code / Pynac

14 views
Skip to first unread message

Dr. David Kirkby

unread,
Mar 31, 2011, 7:37:46 PM3/31/11
to sage-devel
I've built Sage 64-bit on OpenSolaris but it crashes at startup. I've run gdb
and find the bit of code that's causing the crash is this.

./devel/sage-main/sage/symbolic/function.cpp


/* "sage/symbolic/function.pyx":109
* raise ValueError, real_fname + " parameter must be callable"
*
* if not self._is_registered(): # <<<<<<<<<<<<<<
* self._register_function()
*
*/


commenting out the next few lines of
./devel/sage-main/sage/symbolic/function.cpp

/* __pyx_t_2 = ((struct __pyx_vtabstruct_4sage_8symbolic_8function_Function
*)((struct __pyx_obj_4sage_8symbolic_8function_Function
*)__pyx_v_self)->__pyx_vtab)->_is_registered(((struct
__pyx_obj_4sage_8symbolic_8function_Function *)__pyx_v_self)); if
(unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 109;
__pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_2);
__pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_6 < 0))
{__pyx_filename = __pyx_f[1]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto
__pyx_L1_error;}
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_t_5 = (!__pyx_t_6);
*/

allows Sage to run. It's not 100% stable, but much better than before.

This comes from:

./devel/sage-main/build/sage/symbolic/function.pyx (around line 109)

#
###### if not self._is_registered():
###### self._register_function()
######
###### global sfunction_serial_dict
###### sfunction_serial_dict[self._serial] = self
#

Can anyone see if there might be something wrong with this code, which is
causing Sage to segfault?

I've not tied it down to a specific line yet, but just removed everything under
the 'if' statement.

Why when I edit the .pyx file and type

./sage -b

does it not rebuild the C++ code? I ended up having to edit the C++ manually to
comment out the lines causing the crash.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

Dave

Burcin Erocal

unread,
Apr 1, 2011, 7:54:47 AM4/1/11
to sage-...@googlegroups.com
On Fri, 01 Apr 2011 00:37:46 +0100
"Dr. David Kirkby" <david....@onetel.net> wrote:

> I've built Sage 64-bit on OpenSolaris but it crashes at startup. I've
> run gdb and find the bit of code that's causing the crash is this.
>
> ./devel/sage-main/sage/symbolic/function.cpp
>
>
> /* "sage/symbolic/function.pyx":109
> * raise ValueError, real_fname + " parameter must
> be callable" *
> * if not self._is_registered(): # <<<<<<<<<<<<<<
> * self._register_function()
> *
> */

<snip>


> allows Sage to run. It's not 100% stable, but much better than before.
>

<snip>


> Can anyone see if there might be something wrong with this code,
> which is causing Sage to segfault?

It might be due to the linker initializing the library in a different
order. Can you post a backtrace so we can see which function it is
trying to construct?

Moving the lines

# This must come before Calculus -- it initializes the Pynac library.
import sage.symbolic.pynac

in sage/all.py higher in the file might work around this problem. The
correct solution would be to import the offending function lazily or
locally.


Cheers,
Burcin

David Kirkby

unread,
Apr 1, 2011, 9:46:05 AM4/1/11
to sage-...@googlegroups.com

I just started to download 4.7.alpha3. Once I have that downloaded and
built it, I'll post a backtrace. If it helps, I'm willing to give you
an account on the machine "hawk" which is used by the buildbot. Unlike
t2.math, it is pretty quick (quad core 3.33 GHz Xeon).

Dave

Dr. David Kirkby

unread,
Apr 1, 2011, 12:20:57 PM4/1/11
to sage-...@googlegroups.com
On 04/ 1/11 12:54 PM, Burcin Erocal wrote:
> On Fri, 01 Apr 2011 00:37:46 +0100
> "Dr. David Kirkby"<david....@onetel.net> wrote:
>
>> I've built Sage 64-bit on OpenSolaris but it crashes at startup. I've
>> run gdb and find the bit of code that's causing the crash is this.
>>
>> ./devel/sage-main/sage/symbolic/function.cpp
>>
>>
>> /* "sage/symbolic/function.pyx":109
>> * raise ValueError, real_fname + " parameter must
>> be callable" *
>> * if not self._is_registered(): #<<<<<<<<<<<<<<
>> * self._register_function()
>> *
>> */
> <snip>
>> allows Sage to run. It's not 100% stable, but much better than before.
>>
> <snip>
>> Can anyone see if there might be something wrong with this code,
>> which is causing Sage to segfault?
>
> It might be due to the linker initializing the library in a different
> order. Can you post a backtrace so we can see which function it is
> trying to construct?

Here's a backtrace

drkirkby@hawk:~/64/sage-4.7.alpha3$ ./sage -gdb
Building Sage on Solaris in 64-bit mode
Creating SAGE_LOCAL/lib/sage-64.txt since it does not exist
Detected SAGE64 flag
Building Sage on Solaris in 64-bit mode
----------------------------------------------------------------------
| Sage Version 4.7.alpha3, Release Date: 2011-03-31 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
**********************************************************************
* *
* Warning: this is a prerelease version, and it may be unstable. *
* *
**********************************************************************
/export/home/drkirkby/64/sage-4.7.alpha3/local/bin/sage-ipython
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-pc-solaris2.11"...
warning: Lowest section in /lib/amd64/libdl.so.1 is .dynamic at 00000000000000b0
Python 2.6.4 (r264:75706, Apr 1 2011, 15:07:52)
[GCC 4.5.0] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
warning: Lowest section in /lib/amd64/libintl.so.1 is .dynamic at 00000000000000b0
warning: Lowest section in /lib/amd64/libpthread.so.1 is .dynamic at
00000000000000b0

Program received signal SIGSEGV, Segmentation fault.
0x00000000003eb0a5 in ?? ()
(gdb) bt
#0 0x00000000003eb0a5 in ?? ()
#1 0xfffffd7fff2ac5d1 in _Unwind_RaiseException_Body () from /lib/64/libc.so.1
#2 0xfffffd7fff2ac855 in _Unwind_RaiseException () from /lib/64/libc.so.1
#3 0xfffffd7ff91d6729 in __cxa_throw (obj=<value optimized out>, tinfo=<value
optimized out>, dest=<value optimized out>)
at ../../../../../gcc-4.5.0/libstdc++-v3/libsupc++/eh_throw.cc:78
#4 0xfffffd7fcec6d5ff in GiNaC::function::find_function (name=@0x4a359b0,
nparams=2) at function.cpp:1446
#5 0xfffffd7fce9454ad in
__pyx_f_4sage_8symbolic_8function_15BuiltinFunction__is_registered
(__pyx_v_self=0x4a142f0) at sage/symbolic/function.cpp:7301
#6 0xfffffd7fce950755 in __pyx_pf_4sage_8symbolic_8function_8Function___init__
(__pyx_v_self=0x4a142f0, __pyx_args=<value optimized out>,
__pyx_kwds=<value optimized out>) at sage/symbolic/function.cpp:2374
#7 0xfffffd7fffde7a70 in ?? ()
#8 0x00000016745f5f63 in ?? ()
#9 0x0000000004a0e5a8 in ?? ()
#10 0x0000000004a142f0 in ?? ()
#11 0x000000000000000b in ?? ()
#12 0x0000000004a0e5a8 in ?? ()
#13 0x0000000002c913e8 in ?? ()
#14 0xfffffd7fd76c2b30 in module_members () from
/export/home/drkirkby/64/sage-4.7.alpha3/local/lib//libpython2.6.so.1.0
#15 0x0000002752657572 in ?? ()
#16 0xfffffd7fd76d5c60 in ?? () from
/export/home/drkirkby/64/sage-4.7.alpha3/local/lib//libpython2.6.so.1.0
#17 0x2800000040520000 in ?? ()
#18 0xfffffd7fd76d5920 in ?? () from
/export/home/drkirkby/64/sage-4.7.alpha3/local/lib//libpython2.6.so.1.0
#19 0x0000000000000005 in ?? ()
#20 0x00000000049e4db8 in ?? ()
---Type <return> to continue, or q <return> to quit---
#21 0x0000000000000000 in ?? ()

The line starting:

__pyx_t_2 = ((struct __pyx_vtabstruct_4sage_8symbolic_8function_Function *)

below is sage/symbolic/function.cpp line 2374


/* "sage/symbolic/function.pyx":109
* raise ValueError, real_fname + " parameter must be callable"
*
* if not self._is_registered(): # <<<<<<<<<<<<<<
* self._register_function()
*
*/

__pyx_t_2 = ((struct __pyx_vtabstruct_4sage_8symbolic_8function_Function
*)((struct __pyx_obj_4sage_8symbolic_8function_Function
*)__pyx_v_self)->__pyx_vtab)->_is_registered(((struct
__pyx_obj_4sage_8symbolic_8function_Function *)__pyx_v_self)); if
(unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 109;
__pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_2);
__pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_6 < 0))
{__pyx_filename = __pyx_f[1]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto
__pyx_L1_error;}
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_t_5 = (!__pyx_t_6);

if (__pyx_t_5) {

Dave

Burcin Erocal

unread,
Apr 1, 2011, 6:46:52 PM4/1/11
to sage-...@googlegroups.com
On Fri, 01 Apr 2011 17:20:57 +0100

"Dr. David Kirkby" <david....@onetel.net> wrote:

> On 04/ 1/11 12:54 PM, Burcin Erocal wrote:
> > On Fri, 01 Apr 2011 00:37:46 +0100
> > "Dr. David Kirkby"<david....@onetel.net> wrote:
> >
> >> I've built Sage 64-bit on OpenSolaris but it crashes at startup. I've
> >> run gdb and find the bit of code that's causing the crash is this.
> >>
> >> ./devel/sage-main/sage/symbolic/function.cpp
> >>
> >>
> >> /* "sage/symbolic/function.pyx":109
> >> * raise ValueError, real_fname + " parameter must
> >> be callable" *
> >> * if not self._is_registered(): #<<<<<<<<<<<<<<
> >> * self._register_function()
> >> *
> >> */
> > <snip>
> >> allows Sage to run. It's not 100% stable, but much better than before.
> >>
> > <snip>
> >> Can anyone see if there might be something wrong with this code,
> >> which is causing Sage to segfault?
> >
> > It might be due to the linker initializing the library in a different
> > order. Can you post a backtrace so we can see which function it is
> > trying to construct?
>
> Here's a backtrace

<snip>


>
> Program received signal SIGSEGV, Segmentation fault.
> 0x00000000003eb0a5 in ?? ()
> (gdb) bt
> #0 0x00000000003eb0a5 in ?? ()
> #1 0xfffffd7fff2ac5d1 in _Unwind_RaiseException_Body () from /lib/64/libc.so.1
> #2 0xfffffd7fff2ac855 in _Unwind_RaiseException () from /lib/64/libc.so.1
> #3 0xfffffd7ff91d6729 in __cxa_throw (obj=<value optimized out>, tinfo=<value
> optimized out>, dest=<value optimized out>)
> at ../../../../../gcc-4.5.0/libstdc++-v3/libsupc++/eh_throw.cc:78
> #4 0xfffffd7fcec6d5ff in GiNaC::function::find_function (name=@0x4a359b0,
> nparams=2) at function.cpp:1446
> #5 0xfffffd7fce9454ad in
> __pyx_f_4sage_8symbolic_8function_15BuiltinFunction__is_registered
> (__pyx_v_self=0x4a142f0) at sage/symbolic/function.cpp:7301
> #6 0xfffffd7fce950755 in __pyx_pf_4sage_8symbolic_8function_8Function___init__
> (__pyx_v_self=0x4a142f0, __pyx_args=<value optimized out>,
> __pyx_kwds=<value optimized out>) at sage/symbolic/function.cpp:2374
> #7 0xfffffd7fffde7a70 in ?? ()

<the stack is corrupted so the rest is useless>

David gave me an account on the machine. Here is the Python call stack:

File "/export/home/burcin/sage-4.7.alpha3/local/bin/sage-ipython", line 21, in <module>
ipy_sage = IPython.Shell.start()
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/IPython/Shell.py", line 1233, in start
return shell(user_ns = user_ns)
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/IPython/Shell.py", line 78, in __init__
debug=debug,shell_class=shell_class)
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/IPython/ipmaker.py", line 644, in make_IPython
force_import(profmodname)
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/IPython/ipmaker.py", line 66, in force_import
__import__(modname)
File "ipy_profile_sage.py", line 7, in <module>
import sage.all_cmdline
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/all_cmdline.py", line 14, in <module>
from sage.all import *
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/all.py", line 75, in <module>
from sage.schemes.all import *
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/schemes/all.py", line 25, in <module>
from hyperelliptic_curves.all import *
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/schemes/hyperelliptic_curves/all.py", line 1, in <module>
from constructor import HyperellipticCurve
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/schemes/hyperelliptic_curves/constructor.py", line 11, in <module>
from sage.schemes.generic.all import ProjectiveSpace
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/schemes/generic/all.py", line 4, in <module>
from affine_space import AffineSpace, is_AffineSpace
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/schemes/generic/affine_space.py", line 24, in <module>
import algebraic_scheme
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/schemes/generic/algebraic_scheme.py", line 143, in <module>
import toric_variety
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/schemes/generic/toric_variety.py", line 236, in <module>
from sage.geometry.cone import Cone, is_Cone
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/geometry/cone.py", line 174, in <module>
from sage.combinat.posets.posets import FinitePoset
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/combinat/posets/posets.py", line 24, in <module>
from sage.graphs.all import DiGraph
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/graphs/all.py", line 16, in <module>
from graph_editor import graph_editor
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/graphs/graph_editor.py", line 22, in <module>
from sagenb.misc.support import EMBEDDED_MODE
File "/export/home/burcin/sage-4.7.alpha3/devel/sagenb/sagenb/misc/support.py", line 563, in <module>
from sage.symbolic.all import Expression, SR
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/symbolic/all.py", line 9, in <module>
from sage.symbolic.relation import solve, solve_mod, solve_ineq
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/symbolic/relation.py", line 314, in <module>
from sage.calculus.calculus import maxima
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/calculus/calculus.py", line 374, in <module>
from sage.symbolic.integration.integral import indefinite_integral, \
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/symbolic/integration/integral.py", line 129, in <module>
indefinite_integral = IndefiniteIntegral()
File "/export/home/burcin/sage-4.7.alpha3/local/lib/python2.6/site-packages/sage/symbolic/integration/integral.py", line 62, in __init__
BuiltinFunction.__init__(self, "integrate", nargs=2)


It seems that cones.py looks for posets.py, which needs the graphs
module, which initializes the graph_editor. The graph editor tries to
see if it's in the notebook or the command line, but sagenb imports SR
and Expression from sage.symbolic.all (line 563 of
sagenb/misc/support.py). This tries to initialize the functions
(integrate in this case) before pynac is initialized...

We need a better solution for making sure modules are initialized
properly before anything is imported from them. I thought putting an
__init__.py file in sage/symbolic/ with "import pynac" would solve the
problem. However, it seems that python just ignores that file.

Any suggestions on how to solve the general problem properly?


BTW, telling people to import from .all if they are outside the current
module is just asking for trouble. We can't put initialization code in
all.py, since it would just get imported to the main namespace.


Cheers,
Burcin

Robert Bradshaw

unread,
Apr 1, 2011, 9:29:05 PM4/1/11
to sage-...@googlegroups.com

Why doesn't importing from sage.symbolic.pynac initialize pynac?

> We need a better solution for making sure modules are initialized
> properly before anything is imported from them. I thought putting an
> __init__.py file in sage/symbolic/ with "import pynac" would solve the
> problem. However, it seems that python just ignores that file.
>
> Any suggestions on how to solve the general problem properly?
>
>
> BTW, telling people to import from .all if they are outside the current
> module is just asking for trouble.

Isn't that better than specifying what modules to import in what
order, and having them manually, or manually call initialization code?
I'd rather make "import sage.foo.all" just work. Maybe there's
something special with pynac... ideally not every party of Sage should
have to know abou that though.

> We can't put initialization code in
> all.py, since it would just get imported to the main namespace.

You can use the __all__ variable, but I don't think that's necessary.

- Robert

Dr. David Kirkby

unread,
Apr 2, 2011, 5:19:06 AM4/2/11
to sage-...@googlegroups.com
On 04/ 1/11 11:46 PM, Burcin Erocal wrote:
> On Fri, 01 Apr 2011 17:20:57 +0100
> "Dr. David Kirkby"<david....@onetel.net> wrote:
>
>> On 04/ 1/11 12:54 PM, Burcin Erocal wrote:
>>> On Fri, 01 Apr 2011 00:37:46 +0100
>>> "Dr. David Kirkby"<david....@onetel.net> wrote:
>>>
>>>> I've built Sage 64-bit on OpenSolaris but it crashes at startup. I've
>>>> run gdb and find the bit of code that's causing the crash is this.

> It seems that cones.py looks for posets.py, which needs the graphs


> module, which initializes the graph_editor. The graph editor tries to
> see if it's in the notebook or the command line, but sagenb imports SR
> and Expression from sage.symbolic.all (line 563 of
> sagenb/misc/support.py). This tries to initialize the functions
> (integrate in this case) before pynac is initialized...
>
> We need a better solution for making sure modules are initialized
> properly before anything is imported from them. I thought putting an
> __init__.py file in sage/symbolic/ with "import pynac" would solve the
> problem. However, it seems that python just ignores that file.
>
> Any suggestions on how to solve the general problem properly?

> BTW, telling people to import from .all if they are outside the current
> module is just asking for trouble. We can't put initialization code in
> all.py, since it would just get imported to the main namespace.
>
>
> Cheers,
> Burcin
>

This issue is now

http://trac.sagemath.org/sage_trac/ticket/11116

Reply all
Reply to author
Forward
0 new messages