fix(treespec): avoid casts during interpreter finalization - #294
Merged
XuehaiPan merged 1 commit intoAug 21, 2026
Merged
Conversation
Read PyTreeSpec and PyTreeIter values directly from constructed pybind11 instances in their garbage-collection slots. This avoids interpreter-local type lookups after pybind11 metadata becomes unavailable, where an escaping cast error would terminate the process. Update the unreleased changelog.
XuehaiPan
marked this pull request as ready for review
August 20, 2026 12:01
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
Avoid registry-dependent
py::castcalls from thePyTreeSpecandPyTreeItergarbage-collection slots. The slots now access the constructed C++ value directly from the pybind11 instance wrapper, and the fix is documented in the changelog.Motivation and Context
The CPython 3.14t ppc64le job in release run 32338078331 aborted during
Py_EndInterpreter(). Atp_traverseortp_clearslot calledpy::castafter pybind11's interpreter-local type metadata became unavailable, causing apybind11::cast_errorto escape the C callback and terminate the process.Garbage-collection slots must remain usable throughout interpreter teardown and must not depend on registry state that may already have been finalized. The separate CPython 3.15t s390x concurrent-import failure occurs inside pybind11 before optree module initialization and is not addressed here.
Types of changes
What types of changes does your code introduce? Put an
xin all the boxes that apply:Implemented Tasks
PyTreeSpecGC slots.PyTreeIterGC slots while preserving the unconstructed-holder guard.Checklist
Go over all the following points, and put an
xin all the boxes that apply.If you are unsure about any of these, don't hesitate to ask. We are here to help!
test_type_cache_cleanup_across_subinterpretersregression test covers this bug.make testpasses. (required)Validation
make test: 93,894 passed, 26 skipped, 1 xfailed; 100% coverage.python3 -m pre_commit run --all-files: passed.clang-tidy --fixwas not run on macOS.