[mypyc] Fix segfault when top level raises exception #10586
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes mypyc/mypyc#813
This prevents the compiled code from segfaulting when the top level code raises an exception. In that case, previously, the compiled code would store an internal pointer to the module object and would just return that pointer if the same module was initialized more than once. However, if an exception was raised while running top level code, the internal pointer would still point to the module object even though not everything was completely initialized, leading future attempts to import that module to use functions and classes that were never completely initialized.
This fixes that bug by resetting the internal module pointer to NULL when an error occurs so that the top level code is run again on future attempts to import that same function.
Test Plan
I added a run test to run-misc.test that reproduces this segfault and made sure it passed after my changes.
I wasn't really sure where I should put that test so I ended up putting it in run-misc.test, but let me know if you want me to move it somewhere else.