Avoid leaking module object on numpy C-API import failure - #263
Merged
Conversation
antonwolfy
requested review from
jharlow-intel,
ndgrigorian,
vlad-perevezentsev and
xaleryb
as code owners
August 31, 2026 14:11
ndgrigorian
reviewed
Aug 31, 2026
ndgrigorian
previously approved these changes
Aug 31, 2026
ndgrigorian
left a comment
Collaborator
There was a problem hiding this comment.
other than nit LGTM
import_array() and import_umath() are macros that expand to `return NULL;` on failure. When they were called after PyModule_Create(), a failing import would return directly out of PyInit__ufuncs without releasing the module object, leaking the strong reference created by PyModule_Create(). Move the imports before the module is created so there is no owned reference to leak when an import fails. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
antonwolfy
force-pushed
the
fix-ufuncs-module-leak
branch
from
September 1, 2026 09:28
681567a to
e30fc8e
Compare
ndgrigorian
approved these changes
Sep 1, 2026
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.
Summary
import_array()andimport_umath()are macros that expand toreturn NULL;on failure. InPyInit__ufuncsthey were called afterPyModule_Create(), so a failing numpy C-API import returned directly out of the init function without releasing the module object — leaking the strong reference created byPyModule_Create().This moves the imports before the module is created, so there is no owned reference to leak when an import fails. This is the pattern documented for the numpy import macros, which are designed to be called from a context where a bare
return NULL;is valid.Notes
Latent, pre-existing bug; only triggers on the (rare, usually fatal) failure of numpy's C-API import at module init. No behavior change on the success path.