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.
I was working on an IPython extension which provides a cell magic for running C code via bitey, e.g.:
However I was running into random segfaults, often after running
%timeitor similar, indicating that garbage collection was likely to blame. To cut to the chase, the issue was because I calledbitey.loader.build_moduleto build a module, extracted the functions into the current namespace, but never saved the built module insys.modules. When the module was garbage collected (and hence_llvm_engine/_llvm_moduleget deleted).... BOOM!To fix the bug, either of the following would be sufficient:
sys.modules. In this case, I'd suggest a short note in the docstring._llvm_engineand_llvm_moduleas attributes.