File “sage/symbolic/pynac_impl.pxi”, Exception. Unhandled SIGSEGV: A segmentation fault occurred

I am not sure if this is a bug in Python or sagemath.

Using sagemath 9.5 on Linux Arch.
I’ll describe the problem in words, then given MWE to reproduce it.
I have an integrand integrand = "F(x)*(-x^2+x)^(1/2)" as string. I pass it to a subprocess using multiprocessing. When I put the intergrand in the queue using SR(integrand) the crash happens. If I put the integrand in the queue as just intergand (i.e. string) no crash happens.

It is a matter of using SR(integrand) in the parent sagemath process or doing SR on the integrand in the subprocess before calling integrate there.

And the strange thing, this ONLY happens when the string (integrand) contains F(x) in it. If it is only F, then the crash goes away.

Is this a Python issue or sagemath? Why is using SR(integrand) causes this crash?

Here is the sage file (it is a script) meant to be run from Linux terminal. Will show the command to run it below. Call this file as test_one_integral_in_sage_direct.sage

#!/usr/bin/env sage

import os, sys, time, datetime, ntpath
import multiprocessing as mp
from sage.all import *

def doTheIntegration(THE_INPUT):

    problem = THE_INPUT.get()        
    try:        
        anti = integrate(problem[0],problem[1],algorithm="giac") 
        print("inside subprocess. integrate returned ", anti)
        resultBack = [str(anti)]                

    except Exception as ee:
        anti = "Exception raised: " + type(ee).__name__ +  " >> " +ee.args[0]
        print(anti)
        resultBack = [anti]                            

    THE_INPUT.put(resultBack)

if __name__ == "__main__":

    var("x F")
    mp.set_start_method('spawn')    
    print("Test starting.....")

    integrand = "F(x)*(-x^2+x)^(1/2)"
    variable = x

    theQueue = mp.Queue()
    theQueue.put([SR(integrand),variable])
    process = mp.Process(group=None,target=doTheIntegration, args=(theQueue,))  
    process.start()                            
    process.join(4*60)

Now from the terminal type the following

  sage ./test_one_integral_in_sage_direct.sage

This gives

Test starting.....
Traceback (most recent call last):
  File "sage/symbolic/pynac_impl.pxi", line 500, in sage.symbolic.expression.py_print_function_pystring (build/cythonized/sage/symbolic/expression.cpp:10274)
TypeError: sequence item 0: expected str instance, bytes found
Exception ignored in: 'sage.symbolic.expression.py_print_function'
Traceback (most recent call last):
  File "sage/symbolic/pynac_impl.pxi", line 500, in sage.symbolic.expression.py_print_function_pystring (build/cythonized/sage/symbolic/expression.cpp:10274)
TypeError: sequence item 0: expected str instance, bytes found
------------------------------------------------------------------------
/usr/lib/python3.10/site-packages/cysignals/signals.cpython-310-x86_64-linux-gnu.so(+0x76fd)[0x7f2707d1e6fd]
/usr/lib/python3.10/site-packages/cysignals/signals.cpython-310-x86_64-linux-gnu.so(+0x77bc)[0x7f2707d1e7bc]
/usr/lib/python3.10/site-packages/cysignals/signals.cpython-310-x86_64-linux-gnu.so(+0xa537)[0x7f2707d21537]
/usr/lib/libc.so.6(+0x42560)[0x7f2708736560]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(_ZNK5GiNaC8function5printERKNS_13print_contextEj+0x555)[0x7f26bb6f51f5]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(_ZNK5GiNaC3mul14print_exvectorERKSt6vectorINS_2exESaIS2_EERKNS_13print_contextEPKc+0x50)[0x7f26bb761b30]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(_ZNK5GiNaC3mul17do_print_rat_funcERKNS_13print_contextEjb+0xb1b)[0x7f26bb76787b]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(_ZN5GiNaClsERSoRKNS_2exE+0xb3)[0x7f26bb78c213]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(+0x172b34)[0x7f26bb6c6b34]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(+0x14bb21)[0x7f26bb69fb21]
/usr/lib/python3.10/site-packages/sage/symbolic/ring.cpython-310-x86_64-linux-gnu.so(+0x1b603)[0x7f26bb4fb603]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(+0x16e21e)[0x7f26bb6c221e]
/usr/lib/python3.10/site-packages/sage/symbolic/expression.cpython-310-x86_64-linux-gnu.so(+0xc2986)[0x7f26bb616986]
/usr/lib/python3.10/site-packages/sage/structure/sage_object.cpython-310-x86_64-linux-gnu.so(+0x12b5e)[0x7f2706c3db5e]
/usr/lib/python3.10/site-packages/sage/structure/sage_object.cpython-310-x86_64-linux-gnu.so(+0xb1a5)[0x7f2706c361a5]
/usr/lib/libpython3.10.so.1.0(PyObject_Str+0x160)[0x7f2708a65380]
/usr/lib/libpython3.10.so.1.0(PyFile_WriteObject+0x41)[0x7f2708afcd21]
/usr/lib/libpython3.10.so.1.0(+0x1fe612)[0x7f2708afc612]
/usr/lib/libpython3.10.so.1.0(+0x14892f)[0x7f2708a4692f]
/usr/lib/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x349)[0x7f2708a3e769]
/usr/lib/libpython3.10.so.1.0(_PyFunction_Vectorcall+0x7c)[0x7f2708a4e73c]
/usr/lib/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x2e05)[0x7f2708a41225]
/usr/lib/libpython3.10.so.1.0(_PyFunction_Vectorcall+0x7c)[0x7f2708a4e73c]
/usr/lib/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x67b)[0x7f2708a3ea9b]
/usr/lib/libpython3.10.so.1.0(_PyFunction_Vectorcall+0x7c)[0x7f2708a4e73c]
/usr/lib/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x67b)[0x7f2708a3ea9b]
/usr/lib/libpython3.10.so.1.0(_PyFunction_Vectorcall+0x7c)[0x7f2708a4e73c]
/usr/lib/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x349)[0x7f2708a3e769]
/usr/lib/libpython3.10.so.1.0(_PyFunction_Vectorcall+0x7c)[0x7f2708a4e73c]
/usr/lib/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x13b9)[0x7f2708a3f7d9]
/usr/lib/libpython3.10.so.1.0(+0x13f392)[0x7f2708a3d392]
/usr/lib/libpython3.10.so.1.0(PyEval_EvalCode+0x94)[0x7f2708af5c14]
/usr/lib/libpython3.10.so.1.0(+0x208b54)[0x7f2708b06b54]
/usr/lib/libpython3.10.so.1.0(+0x20455b)[0x7f2708b0255b]
/usr/lib/libpython3.10.so.1.0(PyRun_StringFlags+0x81)[0x7f2708afa5f1]
/usr/lib/libpython3.10.so.1.0(PyRun_SimpleStringFlags+0x41)[0x7f2708afa541]
/usr/lib/libpython3.10.so.1.0(Py_RunMain+0x118)[0x7f2708b13038]
/usr/lib/libpython3.10.so.1.0(Py_BytesMain+0x3d)[0x7f2708ae669d]
/usr/lib/libc.so.6(+0x2d310)[0x7f2708721310]
/usr/lib/libc.so.6(__libc_start_main+0x81)[0x7f27087213c1]
/usr/bin/python3(_start+0x2e)[0x56274619004e]
------------------------------------------------------------------------
/usr/bin/cysignals-CSI:42: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.spawn import find_executable
Attaching gdb to process id 2048496.
Cannot find gdb installed
GDB is not installed.
Install gdb for enhanced tracebacks.
------------------------------------------------------------------------
Unhandled SIGSEGV: A segmentation fault occurred.
This probably occurred because a *compiled* module has a bug
in it and is not properly wrapped with sig_on(), sig_off().
Python will now terminate.

Now I changed the script so that I do not do SR(integrand) in the sagemath process, but pass it as is (string) and in the subprocess apply the SR on it before calling integrate.

Now no crash. (the integral will not be solved, which is expected. This is just a test integral).

#!/usr/bin/env sage

import os, sys, time, datetime, ntpath
import multiprocessing as mp
from sage.all import *

def doTheIntegration(THE_INPUT):

    problem = THE_INPUT.get()        
    try:        
        anti = integrate(SR(problem[0]),problem[1],algorithm="giac") 
        print("inside subprocess. integrate returned ", anti)
        resultBack = [str(anti)]                

    except Exception as ee:
        anti = "Exception raised: " + type(ee).__name__ +  " >> " +ee.args[0]
        print(anti)
        resultBack = [anti]                            

    THE_INPUT.put(resultBack)

if __name__ == "__main__":

    var("x F")
    mp.set_start_method('spawn')    
    print("Test starting.....")

    integrand = "F(x)*(-x^2+x)^(1/2)"
    variable = x

    theQueue = mp.Queue()
    theQueue.put([integrand,variable])
    process = mp.Process(group=None,target=doTheIntegration, args=(theQueue,))  
    process.start()

Now

   >sage ./test_one_integral_in_sage_direct.sage

no crash. It gives

Test starting.....
inside doTheIntegration integrand= F(x)*(-x^2+x)^(1/2)
inside doTheIntegration x= x
inside subprocess. integrate returned  integrate(sqrt(-x^2 + x)*F(x), x)

Question is: Is this sagemath issue with mp python package? Why does it crash when I do SR(integrand) from the sagemath side? Note that this ONLY affect this integral since it has F(x) in it. If I change F(x) to F then the crash goes away! So the following now gives no crash

#!/usr/bin/env sage

import os, sys, time, datetime, ntpath
import multiprocessing as mp
from sage.all import *

def doTheIntegration(THE_INPUT):

    problem = THE_INPUT.get()        
    try:        
        anti = integrate(problem[0],problem[1],algorithm="giac") 
        print("inside subprocess. integrate returned ", anti)
        resultBack = [str(anti)]                

    except Exception as ee:
        anti = "Exception raised: " + type(ee).__name__ +  " >> " +ee.args[0]
        print(anti)
        resultBack = [anti]                            

    THE_INPUT.put(resultBack)

if __name__ == "__main__":

    var("x F")
    mp.set_start_method('spawn')    
    print("Test starting.....")

    integrand = "F*(-x^2+x)^(1/2)"
    variable = x

    theQueue = mp.Queue()
    theQueue.put([SR(integrand),variable]) 
    process = mp.Process(group=None,target=doTheIntegration, args=(theQueue,))  
    process.start()                            
    process.join(4*60)

And now

>sage ./test_one_integral_in_sage_direct.sage

No crash:

Test starting.....
inside doTheIntegration integrand= sqrt(-x^2 + x)*F
inside doTheIntegration x= x
inside subprocess. integrate returned  1/8*(2*sqrt(-x^2 + x)*(2*x - 1) + arcsin(2*x - 1))*F

This is strange, since in sagemath, doing SR(integrand) works with no problem:

sage: var("x F")
    (x, F)
sage: SR("F(x)*(-x^2+x)^(1/2)")
   sqrt(-x^2 + x)*F(x)

Next, I tried making x as var and F as function to see if this helps. But it did not.

#!/usr/bin/env sage

import os, sys, time, datetime, ntpath
import multiprocessing as mp
from sage.all import *

def doTheIntegration(THE_INPUT):

    problem = THE_INPUT.get()        
    try:        
        anti = integrate(problem[0],problem[1],algorithm="giac") 
        print("inside subprocess. integrate returned ", anti)
        resultBack = [str(anti)]                

    except Exception as ee:
        anti = "Exception raised: " + type(ee).__name__ +  " >> " +ee.args[0]
        print(anti)
        resultBack = [anti]                            

    THE_INPUT.put(resultBack)

if __name__ == "__main__":

    x = var("x")
    F = function('F')        
    mp.set_start_method('spawn')    
    print("Test starting.....")

    integrand = "F(x)*(-x^2+x)^(1/2)"
    variable = x

    theQueue = mp.Queue()
    theQueue.put([SR(integrand),variable]) 
    process = mp.Process(group=None,target=doTheIntegration, args=(theQueue,))  
    process.start()                            
    process.join(4*60)

And now

  sage ./test_one_integral_in_sage_direct.sage

Gives same crash as before.

Any one know why? and if this is sagemath problem? When the crash only happens when there is F(x) in the integrand when putting it in the Queue

  >sage --version
  SageMath version 9.5, Release Date: 2022-01-30

   >which python
       /bin/python
   >python --version
     Python 3.10.2

Read more here: Source link