Skip to content

BUG: clean up failed extension initialization - #631

Closed
FFspiky wants to merge 1 commit into
pydata:masterfrom
FFspiky:fix/module-init-cleanup
Closed

FFspiky wants to merge 1 commit into
pydata:masterfrom
FFspiky:fix/module-init-cleanup

Conversation

@FFspiky

@FFspiky FFspiky commented Nov 16, 2026

Copy link
Copy Markdown

Fixes #530.

When NumPy's C API cannot initialize, each failed import currently leaves a Bottleneck extension module alive even after garbage collection. For example, replacing NumPy's _ARRAY_API capsule with None and trying to load any of the four extensions three times leaves three modules behind. Failing intern_strings() has the same missing cleanup.

Initialize the NumPy C API before creating the module, retaining the existing import_array() exception behavior. If string initialization fails after module creation, release the module before returning the original exception. Apply this to all four module templates.

Add subprocess regressions for missing and invalid NumPy API capsules. They exercise repeated failures, count retained modules after collection, and check that restoring the capsule permits a successful import. All eight regressions fail against the original source and pass with the patch.

Validation on macOS arm64:

  • Python 3.12.14 / NumPy 2.5.5: full suite, 216 passed and one existing expected failure.
  • Python 3.11.16 / NumPy 2.0.2 (minimum supported Python and NumPy series): installed-package full suite, 216 passed and one existing expected failure.
  • Repository pre-commit hooks, Ruff lint/format checks, and git diff --check pass.
  • A separate validation build injected a MemoryError at PyUnicode_InternFromString("a"). Each original extension retained three modules after three failed imports; each patched extension retained none. The exception message survived, and import succeeded after disabling the injection. The compiler shim is not included in the patch.

Linux, Windows, and free-threaded Python have not been run locally. This addresses the module references in #530, not the separate lifetime of global interned strings.

Prepared with OpenAI Codex for investigation, implementation, tests, validation, and this description.

@FFspiky FFspiky closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Module object leaked on import_array/intern_strings failure in all PyInit functions

1 participant