bitey icon indicating copy to clipboard operation
bitey copied to clipboard

segfault if py_module gets garbage collected

Open bfroehle opened this issue 12 years ago • 1 comments

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:

  1. I could add the module to sys.modules. In this case, I'd suggest a short note in the docstring.
  2. Keep a reference in the python wrapper function object to the execution engine, i.e. set _llvm_engine and _llvm_module as attributes.

bfroehle avatar Aug 25 '12 00:08 bfroehle

If you'd like to play with the %%bitey magic:

bfroehle avatar Aug 31 '12 23:08 bfroehle