fix(py_loader): handle TYPE_THROWABLE to prevent fatal errors in Python 3.14+#717
Draft
k5602 wants to merge 4 commits intometacall:developfrom
Draft
fix(py_loader): handle TYPE_THROWABLE to prevent fatal errors in Python 3.14+#717k5602 wants to merge 4 commits intometacall:developfrom
k5602 wants to merge 4 commits intometacall:developfrom
Conversation
Reproduce regression where a callable returned from Node to Python as TYPE_THROWABLE which could cause a Python 3.14+ fatal error because py_loader did not convert throwables into Python exceptions leading to NULL without an exception (fatal in newer Python releases). Add a test that loads a Node 'flip' helper and a Python script exercising a normal and a throwing callback; asserts correct values and no SEGFAULT Also register the test target in CMakeLists
Member
|
@k5602 add better naming |
Contributor
Author
|
Add defensive checks and error handling when converting MetaCall values to Python objects Return NULL on conversion failure and set a Python exception when none is present Avoid inserting NULL into PyList/PyDict and release references on error Include exception stacktrace when available for MetaCall exceptions Clean up tuple/list/dict refs and free temporary allocations in function invoke/await paths to prevent leaks and crashes
Add tests and improve handling of exceptions thrown across Node.js and Python. This commit addresses issues with how exceptions are propagated between Node.js and Python when using MetaCall. Specifically, it resolves a segmentation fault that occurred in Python 3.14+ due to unhandled `TYPE_THROWABLE` return values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR fixes a critical regression where a callable returned from Node to Python as
TYPE_THROWABLEcould cause a Python 3.14+ fatal error (segmentation fault).Previously,
py_loader_impl_value_to_capi()did not handleTYPE_THROWABLEvalues, leading it to returnNULLwithout setting a proper Python exception that made the segfault in the mac ci. In newer Python releases (3.14+), returningNULLfrom a C API extension without an active exception state is treated as a fatal error.Changes:
TYPE_THROWABLE(id18) inpy_loader_impl_value_to_capi(). It now properly extracts the innerTYPE_EXCEPTIONlabel and message and correctly sets the Python error state usingPyErr_Format(PyExc_RuntimeError, ...).py_loader_impl_func_callto ensurePyErr_SetStringis called whenever a foreign function call fails to convert a return value and returnsNULLwithout an existing exception state.metacall-node-python-throwable-testto the test suite to validate cross-language error propagation (Python throwing callback passed to a Node helper).Type of change
Checklist:
make testorctest -VV -R <test-name>)../docker-compose.sh test &> outputand attached the output.OPTION_BUILD_ADDRESS_SANITIZERor./docker-compose.sh test-address-sanitizer &> outputandOPTION_TEST_MEMORYCHECK.OPTION_BUILD_THREAD_SANITIZERor./docker-compose.sh test-thread-sanitizer &> output.Helgrindin case my code works with threading.make clang-formatin order to format my code and my code follows the style guidelines.