bitey
bitey copied to clipboard
segfault if py_module gets garbage collected
I was working on an IPython extension which provides a cell magic for running C code via bitey, e.g.:
In [4]: %load_ext biteymagic
In [5]: %%bitey
...: int life() { return 42; }
...:
In [6]: life()
Out[6]: 42
However I was running into random segfaults, often after running %timeit
or similar, indicating that garbage collection was likely to blame. To cut to the chase, the issue was because I called bitey.loader.build_module
to build a module, extracted the functions into the current namespace, but never saved the built module in sys.modules
. When the module was garbage collected (and hence _llvm_engine
/_llvm_module
get deleted).... BOOM!
To fix the bug, either of the following would be sufficient:
- I could add the module to
sys.modules
. In this case, I'd suggest a short note in the docstring. - Keep a reference in the python wrapper function object to the execution engine, i.e. set
_llvm_engine
and_llvm_module
as attributes.
If you'd like to play with the %%bitey
magic:
- Extension (and sample notebook source)
- (Rendered) Sample notebook