Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segfault when shutting down kernel that has a shutdown callback #464

Open
garfieldnate opened this issue May 22, 2024 · 0 comments
Open
Labels

Comments

@garfieldnate
Copy link
Collaborator

If you don't remember to call UnregisterForSystemEvent after registering a shutdown handler, when the kernel is shutdown you get a segfault. Here's a sample in Python, but I also did it in Tcl. Originally I was looking into why we had to have a special atexit handler in the Python SML test script in the past, and I whittled down to this MBE; I don't really see how an atexit handler that shuts down the kernel would help here, though, so it might be a separate issue.

try:
    from Python_sml_ClientInterface import *
except ImportError:
    from soar_sml import *

# also fails
# kernel = Kernel.CreateKernelInNewThread()
kernel = Kernel.CreateKernelInCurrentThread()
shutdownCallbackId = kernel.RegisterForSystemEvent(smlEVENT_BEFORE_SHUTDOWN, lambda: 1+1, None)

# Fixes the segfault!
# kernel.UnregisterForSystemEvent(shutdownCallbackId)

print("shutting down kernel")
# segfaults
kernel.Shutdown()

# never prints
print("deleting kernel")
del kernel
print("finished")
garfieldnate added a commit that referenced this issue May 22, 2024
Previous behavior here was to segfault! An exception is much more user-friendly.

Add the SWIG machinery for catching thrown exceptions in the SML binding
libraries, too. We wrap every SML function with a try/catch. I don't know if
this is actually heavy or not, but at least one implementation out there thought
it was and went an alternative route where each method has to be explicitly
marked to catch exceptions:
https://github.com/KiCad/kicad-source-mirror/blob/47e4ebb32a1366d60649879381eac819f7c7131d/common/swig/ki_exception.i#L41

Add tests for TCL, Python and Java that demonstrate the exceptions being
translated for each host language. We don't have C# tests yet T_T.

Notice we did have to add the `atexit` handler back to prevent a segfault when
the exception is not caught correctly; I don't know exactly why. Filed #464.

Fixes #451.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant