Debugging python extension core dumps or segfaults in CI

December 28, 2021

If a python extension causes a segfault when running with CI it just stops abruptly with a Aborted (core dumped) message.

To get it to print out more about where the core dump happened the tests can be run with gdb. First make sure gdb is installed.

Replace the pytest command (or any command) with the following gdb command:

gdb -ex run -ex bt -ex quit --args python -m pytest tests

Where anything after the --args is the normal pytest arguments.

When run, gdb will launch python and run pytest. If the test core dumps gdb will execute the bt command and print out the stack and then exit giving you a full trace.

Eg

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7ca9537 in __GI_abort () at abort.c:79
#2  0x00007ffff5b849bc in QMessageLogger::fatal(char const*, ...) const ()
   from /opt/conda/envs/test/lib/python3.9/site-packages/PyQt5/Qt5/lib/libQt5Core.so.5
#3  0x00007ffff1c56fa0 in QGuiApplicationPrivate::createPlatformIntegration()
    ()
# and a gazilliion more lines depending on the trace...
A debugging session is active.
    Inferior 1 [process 3414] will be killed.
Quit anyway? (y or n) [answered Y; input not from terminal]

Hope it helps!