LuxCore icon indicating copy to clipboard operation
LuxCore copied to clipboard

Python crashes when calling sys.exit()

Open Dade916 opened this issue 7 years ago • 4 comments

Python crashes when calling sys.exit() if pyluxcore.Init() was called with an call back. This is due to pyluxcore keeping a reference to the callback in a static boost::python::object. Everything works fine if python code exit normally without calling sys.exit().

I haven't yet found a work around to this problem.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Dade916 avatar Dec 31 '17 13:12 Dade916

I have added a new SetLogHandler() function to the LuxCore API so it is now possible a workaround:

try:
	pyluxcore.Init(loghandler.LuxCoreLogHandler)

	cmd.main()
finally:
	pyluxcore.SetLogHandler(None)

Calling "pyluxcore.SetLogHandler(None)" before the end fix the problem.

Dade916 avatar Jan 07 '18 13:01 Dade916

So the destructor of the static object attempts to clean up but the interpreter's gone?

There's atexit in Python (similar to std::atexit in C++) that lets you register a callback that is invoked before the interpreter dies. That might do to perform a safe tear-down...

tschw avatar Mar 23 '18 16:03 tschw

Yes there is a static object holding the reference to a python object (i.e. a function call back to print log messages) and when the destructor of the static object is called, the python interpreter as already gone.

It can be solved with the try/finally or with the atexit but both solutions are not transparent for the API user: they must be coded in the application.

Dade916 avatar Mar 23 '18 16:03 Dade916

Yes, I meant doing the python atexit call programmatically from within C++ code, actually.

Another solution would be to have everything, including the system context in classes, then it would get destroyed when the interpreter shuts down and the cleanup could happen there.

tschw avatar Mar 23 '18 18:03 tschw