sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.28k stars 438 forks source link

Enumerating the 2002 triangulations of a 10 point configuration kills sagemath #38187

Open seblabbe opened 2 months ago

seblabbe commented 2 months ago

Steps To Reproduce

PointConfiguration.set_engine('internal')
points = [[64374, 1170],[28595,16],[1162, 658], [28874, 3308], [29974, 9436], [30590, 22299], [49434, 11393], [56042, 11982], [42392, 33338], [33404, 64878]]
p = PointConfiguration(points)
p_fine = p.restrict_to_fine_triangulations()
it = p_fine.triangulations()
for i,t in enumerate(it):
    if i >= 1475:
        print(i,t) 

Expected Behavior

It should print all of the expected 2002 triangulations:

1475 (<0,1,4>, <0,4,5>, <0,5,6>, <0,6,7>, <0,7,8>, <0,8,9>, <1,2,9>, <1,3,5>, <1,3,9>, <1,4,5>, <3,5,9>, <5,6,8>, <5,8,9>, <6,7,8>)
1476 (<0,1,6>, <0,6,7>, <0,7,9>, <1,2,3>, <1,3,6>, <2,3,6>, <2,4,5>, <2,4,6>, <2,5,9>, <4,5,6>, <5,6,8>, <5,8,9>, <6,7,9>, <6,8,9>)
...

Actual Behavior

It hangs and sage exits by itself (without keyboard interrupt) after printing the 1476-th :

1475 (<0,1,4>, <0,4,5>, <0,5,6>, <0,6,7>, <0,7,8>, <0,8,9>, <1,2,9>, <1,3,5>, <1,3,9>, <1,4,5>, <3,5,9>, <5,6,8>, <5,8,9>, <6,7,8>)
1476 (<0,1,6>, <0,6,7>, <0,7,9>, <1,2,3>, <1,3,6>, <2,3,6>, <2,4,5>, <2,4,6>, <2,5,9>, <4,5,6>, <5,6,8>, <5,8,9>, <6,7,9>, <6,8,9>)
Killed

Sometimes, it also says Cannot allocate memory after printing a cysygnal traceback involving next_triangulation and bad_alloc type of error:

1475 (<0,1,4>, <0,4,5>, <0,5,6>, <0,6,7>, <0,7,8>, <0,8,9>, <1,2,9>, <1,3,5>, <1,3,9>, <1,4,5>, <3,5,9>, <5,6,8>, <5,8,9>, <6,7,8>)
1476 (<0,1,6>, <0,6,7>, <0,7,9>, <1,2,3>, <1,3,6>, <2,3,6>, <2,4,5>, <2,4,6>, <2,5,9>, <4,5,6>, <5,6,8>, <5,8,9>, <6,7,9>, <6,8,9>)
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
------------------------------------------------------------------------
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/cysignals/signals.cpython-311-x86_64-linux-gnu.so(+0x98bb)[0x7f2c6cf0a8bb]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/cysignals/signals.cpython-311-x86_64-linux-gnu.so(+0x9971)[0x7f2c6cf0a971]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/python3.11/site-packages/cysignals/signals.cpython-311-x86_64-linux-gnu.so(+0xb591)[0x7f2c6cf0c591]
/lib/x86_64-linux-gnu/libc.so.6(+0x3ef10)[0x7f2c6f0c5f10]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0xc7)[0x7f2c6f0c5e87]
/lib/x86_64-linux-gnu/libc.so.6(abort+0x141)[0x7f2c6f0c77f1]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x9d0a9)[0x7f2c5ca9f0a9]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0xa8506)[0x7f2c5caaa506]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0xa8571)[0x7f2c5caaa571]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0xa87f5)[0x7f2c5caaa7f5]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x9cceb)[0x7f2c5ca9eceb]
/home/slabbe/GitBox/sage/src/sage/geometry/triangulation/base.cpython-311-x86_64-linux-gnu.so(_ZN14triangulations17add_triang_if_newERK17compact_simplices+0x74)[0x7f2be4790344]
/home/slabbe/GitBox/sage/src/sage/geometry/triangulation/base.cpython-311-x86_64-linux-gnu.so(_ZN14triangulations14add_neighboursERK9simplices+0x150)[0x7f2be4790e00]
/home/slabbe/GitBox/sage/src/sage/geometry/triangulation/base.cpython-311-x86_64-linux-gnu.so(_ZN14triangulations18next_triangulationEv+0x3f)[0x7f2be4790f8f]
/home/slabbe/GitBox/sage/src/sage/geometry/triangulation/base.cpython-311-x86_64-linux-gnu.so(_Z18next_triangulationP14triangulations+0x28)[0x7f2be47910e8]
/home/slabbe/GitBox/sage/src/sage/geometry/triangulation/base.cpython-311-x86_64-linux-gnu.so(+0xefac)[0x7f2be4776fac]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyEval_EvalFrameDefault+0x31fd)[0x7f2c6f57487d]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x165d45)[0x7f2c6f5ddd45]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x166b93)[0x7f2c6f5deb93]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x15e1cd)[0x7f2c6f5d61cd]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyEval_EvalFrameDefault+0x31fd)[0x7f2c6f57487d]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x21d439)[0x7f2c6f695439]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(PyEval_EvalCode+0xa6)[0x7f2c6f6954e6]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x2192e0)[0x7f2c6f6912e0]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyEval_EvalFrameDefault+0x1bfa)[0x7f2c6f57327a]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x165d45)[0x7f2c6f5ddd45]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(PyIter_Send+0x31)[0x7f2c6f5b27c1]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyEval_EvalFrameDefault+0x8260)[0x7f2c6f5798e0]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x165d45)[0x7f2c6f5ddd45]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(PyIter_Send+0x31)[0x7f2c6f5b27c1]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyEval_EvalFrameDefault+0x8260)[0x7f2c6f5798e0]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x165d45)[0x7f2c6f5ddd45]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x1666e9)[0x7f2c6f5de6e9]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x15cd11)[0x7f2c6f5d4d11]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(PyObject_Vectorcall+0x34)[0x7f2c6f5c9ef4]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyEval_EvalFrameDefault+0x2e12)[0x7f2c6f574492]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x21d439)[0x7f2c6f695439]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(PyEval_EvalCode+0xa6)[0x7f2c6f6954e6]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x25baea)[0x7f2c6f6d3aea]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x25bbc7)[0x7f2c6f6d3bc7]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(+0x25bc9f)[0x7f2c6f6d3c9f]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyRun_SimpleFileObject+0x11a)[0x7f2c6f6d676a]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(_PyRun_AnyFileObject+0x3b)[0x7f2c6f6d6c3b]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(Py_RunMain+0x733)[0x7f2c6f6f3663]
/home/slabbe/GitBox/sage/local/var/lib/sage/venv-python3.11.1/lib/libpython3.11.so.1.0(Py_BytesMain+0x39)[0x7f2c6f6f3b69]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f2c6f0a8c87]
python3(_start+0x2a)[0x55cc76bfc79a]
------------------------------------------------------------------------
cysignals fork: Cannot allocate memory
Unhandled SIGABRT: An abort() 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.
------------------------------------------------------------------------
Aborted

Additional Information

First reported by Florent Koechlin on ask.sagemath.org here: https://ask.sagemath.org/question/77736/computing-triangulations-fails/

Environment

- **OS**:Ubuntu 18.04
- **Sage Version**: 10.2.rc4, Release Date: 2023-11-17, Using Python 3.11.1

and

- **OS**:Ubuntu 22.04
- **Sage Version**: 10.2

Checklist

seblabbe commented 2 months ago

Adding sig_on and sig_off around the call to next_triangulation at least avoids sage to get killed:

diff --git a/src/sage/geometry/triangulation/base.pyx b/src/sage/geometry/triangulation/base.pyx
index 61014429fdd..89bf439067f 100644
--- a/src/sage/geometry/triangulation/base.pyx
+++ b/src/sage/geometry/triangulation/base.pyx
@@ -31,6 +31,7 @@ from sage.geometry.triangulation.functions cimport binomial
 from sage.geometry.triangulation.triangulations cimport \
     triangulations_ptr, init_triangulations, next_triangulation, delete_triangulations

+from cysignals.signals cimport sig_on, sig_off

 ########################################################################
 cdef class Point(SageObject):
@@ -986,7 +987,9 @@ cdef class ConnectedTriangulationsIterator(SageObject):
             sage: ci.__next__()
             (9, 10)
         """
+        sig_on()
         t = next_triangulation(self._tp)
+        sig_off()
         if not t:
             raise StopIteration
         return t
seblabbe commented 2 months ago

These are the changes that have been made on the file triangulations.cc since 13 years:

$ git lg src/sage/geometry/triangulation/triangulations.cc
* 439a4ece6b7 - using PyLong API instead of legacy PyInt API (il y a 10 mois) [Frédéric Chapoton]
* b4cf6701caf - (origin/u/galois/mrs/46/size_t) Fix some compiler warnings, mostly use size_t for indexing (il y a 3 ans et 11 mois) [Ivan Komarov]
* 657c3e6db11 - Trac #13899: Fix indentation and trailing spaces (il y a 11 ans) [Jeroen Demeyer]
* 95cfa8a9e5e - Trac 9918: Triangulate point configurations (il y a 13 ans) [Volker Braun]

The code was written by @vbraun in the first place. Maybe he has an idea on what could be done to fix the issue?