Skip to content

Import hook: never wrap an already-wrapped loader - #143

Merged
OwenPriceSkelly merged 1 commit into
mainfrom
owen/import-hook-no-rewrap
Sep 11, 2026
Merged

OwenPriceSkelly merged 1 commit into
mainfrom
owen/import-hook-no-rewrap

Conversation

@OwenPriceSkelly

Copy link
Copy Markdown
Member

Summary

GroundhogImportHook.find_spec wrapped spec.loader unconditionally. Another finder on sys.meta_path can re-enter the import system and hand back a spec this hook already wrapped, and each re-entry added another GroundhogLoader layer.

Found on ALCF Polaris compute nodes, where PYTHONPATH=/soft/xalt/.../site_packages installs XALT's sitecustomize.RecorderRTM meta-path finder: inside a groundhog task, import ase ended up 494 loaders deep and importing a script that pulls in academy died with RecursionError (or a segfault, exit 139) inside pydantic. The same env on macOS wraps once.

Fix: only wrap loaders that are not already GroundhogLoader. Test simulates a re-entrant finder that caches and returns the same spec and asserts the depth stays 1.

Test plan

  • uv run pytest (385 passed), uv run ruff check ., uv run ty check .
  • Polaris probe task with this commit (in progress; the previous attempt died at import)

🤖 Generated with Claude Code

Another finder on sys.meta_path can re-enter the import system and hand back
a spec this hook has already wrapped. ALCF's XALT sitecustomize finder
(RecorderRTM, on PYTHONPATH on Polaris compute nodes) does exactly that, and
the hook then nested GroundhogLoader hundreds deep (494 for `import ase`),
overflowing the stack while importing the user script (RecursionError or a
segfault, exit 139). Wrap only loaders that are not GroundhogLoader already.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@OwenPriceSkelly
OwenPriceSkelly merged commit bd2f465 into main Sep 11, 2026
2 checks passed
@OwenPriceSkelly
OwenPriceSkelly deleted the owen/import-hook-no-rewrap branch September 11, 2026 17:51
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.

1 participant