python-symengine crashes due to tcmalloc

Description:

This is similar to the issue I described in symengine#1 (closed), but it affects python-sympy from [extra] rather than a package from AUR.

Scripts crash with the following error:

src/tcmalloc.cc:309] Attempt to free invalid pointer 0x59da2c3c68d0 
[1]    91818 illegal hardware instruction (core dumped)  python

Unit tests from symengine catch this issue. For example, applying the following patch to the PKGBUILD of python-symengine would cause a crash like above:

diff --git a/PKGBUILD b/PKGBUILD
index d98ded4..69f2af2 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -15,6 +15,9 @@ depends=(gcc-libs
          libmpc
          mpfr
          python
+         python-numpy
+         python-scipy
+         python-sympy
          symengine)
 makedepends=(boost
              cereal
@@ -25,6 +28,7 @@ makedepends=(boost
              python-build
              python-installer
              python-setuptools)
+checkdepends=(python-pytest)
 license=(MIT)
 source=(git+https://github.com/symengine/symengine.py#tag=v$pkgver)
 sha256sums=('56c6c675d85c2b8448b4eb1e21c46b7dbc5d2fa3dcd26921a37a5ba64d95b821')
@@ -34,6 +38,15 @@ build() {
   python -m build --wheel --no-isolation
 }
 
+check() {
+    cd $_pkgname
+    local python_version=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
+    python -m installer --destdir=../test_dir dist/*.whl
+    mv symengine/tests .
+    rm -r symengine build
+    PYTHONPATH="$PWD/../test_dir/usr/lib/python$python_version/site-packages" pytest tests
+}
+
 package() {
   cd $_pkgname
   python -m installer --destdir="$pkgdir" dist/*.whl

Alternatively, I include below a small script that produces a crash with current versions of symengine, python-symengine and python-sympy from [extra].

A workaround is to compile symengine without tcmalloc until the (symengine?) bug is solved upstream (I will update the issue description to make it more general than just qiskit).

Additional info:

Steps to reproduce:

  1. pacman -S python-symengine python-sympy
  2. python -c "import symengine as se;import sympy as sp;cb = sp.lambdify;Mtx = sp.Matrix;x, y = se.symbols('x, y');args = Mtx(2, 1, [x, y]);v = Mtx(2, 1, [x**3 * y,(x+1)*(y+1)]);jac = v.jacobian(args);exprs = [jac, x+y, v, (x+1)*(y+1)];lmb = cb(args, exprs)"

The script above (more readible) is the following extracted from the unit test:

import symengine as se
import sympy as sp

cb = sp.lambdify
Mtx = sp.Matrix
x, y = se.symbols('x, y')
args = Mtx(2, 1, [x, y])
v = Mtx(2, 1, [x**3 * y, (x+1)*(y+1)])
jac = v.jacobian(args)
exprs = [jac, x+y, v, (x+1)*(y+1)]
lmb = cb(args, exprs)  # Crash here
Edited by Iyán Mendez Veiga