Skip to content

Pre-import numpy lazy submodules before running tests#90

Merged
SwayamInSync merged 2 commits into
numpy:mainfrom
SwayamInSync:workaround-cpython-149728-lazyimport-race
May 13, 2026
Merged

Pre-import numpy lazy submodules before running tests#90
SwayamInSync merged 2 commits into
numpy:mainfrom
SwayamInSync:workaround-cpython-149728-lazyimport-race

Conversation

@SwayamInSync
Copy link
Copy Markdown
Member

Workaround for an upstream CPython bug exposed by our free-threaded CI on cp3.14t.

Why

Our free-threaded CI on python3.14t started failing in test_pandas_strrep with a RecursionError whose entire stack was inside numpy/__init__.py's lazy __getattr__ recursing on import numpy.rec as rec. Root cause is a race in CPython's importlib._bootstrap: _load_unlocked clears spec._initializing = False before _find_and_load_unlocked runs setattr(parent_module, child, module), so the fast path in _find_and_load returns the module without the parent attribute under that window. Under free-threaded preemption a second thread reliably observes the window, falls into numpy's lazy __getattr__, runs import numpy.rec as rec again, fast-paths again, and recurses.

Upstream tracking:

Original failure traceback in this project's CI: #88 (comment)

@SwayamInSync SwayamInSync requested a review from ngoldbaum May 12, 2026 15:45
@SwayamInSync SwayamInSync added the enhancement New feature or request label May 12, 2026
Copy link
Copy Markdown
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine but can you add a comment with a link to the upstream issue? Hopefully you'll be able to delete this when the next CPython bugfix release happens.

@SwayamInSync
Copy link
Copy Markdown
Member Author

Yup will add comment to drop this in future

Add comments to explain pre-importing numpy's lazy submodules to avoid CPython issue.
@SwayamInSync
Copy link
Copy Markdown
Member Author

Merging this in

@SwayamInSync SwayamInSync merged commit dfe4bdd into numpy:main May 13, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants