DRAFT: Segfault with custom events in wxPython

Published on 2019-08-13.

This is a work in progress that will change. Like to see it finished? Let me know by sending me an email.

We are working on porting Timeline to Python 3. We ran into a problem where a test caused a segfault. I managed to find the offending code, and here is a stripped down example.

  1. test_wx.py
from unittest.mock import Mock
import unittest

import wx
import wx.lib.newevent

CustomEvent, EVT_CUSTOM = wx.lib.newevent.NewEvent()

class WxTest(unittest.TestCase):

    def test_wx(self):
        mock = Mock()
        mock.PostEvent(CustomEvent())

if __name__ == "__main__":
    unittest.main()
$ python3 test_wx.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Segmentation fault (core dumped)
$ gdb python3
GNU gdb (GDB) Fedora 8.2.91.20190401-23.fc30
...
(gdb) run test_wx.py 
...
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

Program received signal SIGSEGV, Segmentation fault.
dict_dealloc (mp=0x7fffe712f9d8) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/dictobject.c:1901
1901	/usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/dictobject.c: No such file or directory.
Missing separate debuginfos, use: dnf debuginfo-install fontconfig-2.13.1-6.fc30.x86_64 libXcursor-1.1.15-5.fc30.x86_64 libgcrypt-1.8.4-3.fc30.x86_64 libxkbcommon-0.8.3-1.fc30.x86_64 lz4-libs-1.8.3-2.fc30.x86_64 python3-sip-4.19.17-1.fc30.x86_64
(gdb) bt
#0  dict_dealloc (mp=0x7fffe712f9d8) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/dictobject.c:1901
#1  0x00007fffea02e1cc in wxPyEvtDict::~wxPyEvtDict (this=0x5555556f3ee8, __in_chrg=<optimized out>) at ../../../../src/pyevent.h:48
#2  wxPyEvent::~wxPyEvent (this=0x5555556f3e90, __in_chrg=<optimized out>) at ../../../../src/pyevent.h:96
#3  sipwxPyEvent::~sipwxPyEvent (this=0x5555556f3e90, __in_chrg=<optimized out>) at ../../../../sip/cpp/sip_corewxPyEvent.cpp:56
#4  0x00007fffea02e24d in sipwxPyEvent::~sipwxPyEvent (this=0x5555556f3e90, __in_chrg=<optimized out>) at ../../../../sip/cpp/sip_corewxPyEvent.cpp:56
#5  0x00007fffea02dfd2 in release_wxPyEvent (sipCppV=0x5555556f3e90, sipState=<optimized out>) at ../../../../sip/cpp/sip_corewxPyEvent.cpp:261
#6  0x00007fffe72ff4ce in ?? () from /usr/lib64/python3.7/site-packages/sip.so
#7  0x00007fffe72ff51d in ?? () from /usr/lib64/python3.7/site-packages/sip.so
#8  0x00007ffff7c14869 in subtype_dealloc (self=<_Event at remote 0x7fffea4ddd38>) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/typeobject.c:1256
#9  0x00007ffff7b8892b in tupledealloc (op=0x7fffe712cda0) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/tupleobject.c:246
#10 0x00007ffff7b8892b in tupledealloc (op=0x7fffea4d7620) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/tupleobject.c:246
#11 0x00007ffff7c14869 in subtype_dealloc (self=<_Call at remote 0x7fffea4d7620>) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/typeobject.c:1256
#12 0x00007ffff7b882ae in list_dealloc (op=0x7fffe70ba228) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/listobject.c:324
#13 0x00007ffff7c14869 in subtype_dealloc (self=<_CallList at remote 0x7fffe70ba228>) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/typeobject.c:1256
#14 0x00007ffff7b8c813 in free_keys_object (keys=0x555555855080) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/gcmodule.c:776
#15 dict_dealloc (mp=0x7fffe712f630) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/dictobject.c:1913
#16 subtype_clear (self=<optimized out>) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Objects/typeobject.c:1101
#17 delete_garbage (old=<optimized out>, collectable=<optimized out>) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/gcmodule.c:769
#18 collect (generation=2, n_collected=0x7fffffffd230, n_uncollectable=0x7fffffffd228, nofail=0) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/gcmodule.c:924
#19 0x00007ffff7c4ac4e in collect_with_callback (generation=generation@entry=2) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/gcmodule.c:1036
#20 0x00007ffff7ca7331 in PyGC_Collect () at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/gcmodule.c:1581
#21 0x00007ffff7caaf03 in Py_FinalizeEx () at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Python/pylifecycle.c:1185
#22 0x00007ffff7cab048 in Py_Exit (sts=sts@entry=0) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Python/pylifecycle.c:2278
#23 0x00007ffff7cab0ff in handle_system_exit () at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Python/pythonrun.c:636
#24 0x00007ffff7cab1e6 in PyErr_PrintEx (set_sys_last_vars=1) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Python/pythonrun.c:646
#25 0x00007ffff7cab651 in PyRun_SimpleFileExFlags (fp=<optimized out>, filename=<optimized out>, closeit=<optimized out>, flags=0x7fffffffd410) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Python/pythonrun.c:435
#26 0x00007ffff7cad864 in pymain_run_file (p_cf=0x7fffffffd410, filename=<optimized out>, fp=0x5555555a20d0) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/main.c:427
#27 pymain_run_filename (cf=0x7fffffffd410, pymain=0x7fffffffd520) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/main.c:1627
#28 pymain_run_python (pymain=0x7fffffffd520) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/main.c:2877
#29 pymain_main (pymain=0x7fffffffd520) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/main.c:3038
#30 0x00007ffff7cadc0c in _Py_UnixMain (argc=<optimized out>, argv=<optimized out>) at /usr/src/debug/python3-3.7.3-1.fc30.x86_64/Modules/main.c:3073
#31 0x00007ffff7e12f33 in __libc_start_main (main=0x555555555050 <main>, argc=2, argv=0x7fffffffd678, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffd668) at ../csu/libc-start.c:308
#32 0x000055555555508e in _start ()
$ python3
Python 3.7.3 (default, Mar 27 2019, 13:36:35)
[GCC 9.0.1 20190227 (Red Hat 9.0.1-0.8)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> wx.version()
'4.0.4 gtk3 (phoenix) wxWidgets 3.0.4'

Site proudly generated by Hakyll.