fix: leave ref_small_vector source empty after move - #6161
Open
henryiii wants to merge 2 commits into
Open
Conversation
Fixes item 24 of pybind#6159.
Skylion007
approved these changes
Sep 2, 2026
henryiii
force-pushed
the
fix/ref-small-vector-move
branch
3 times, most recently
from
September 2, 2026 22:52
eea78d0 to
c523b41
Compare
The enum restart test kept a py::module_ from the old interpreter alive across finalize_interpreter(), then decref'd that stale pointer into memory the new interpreter had reused. Depending on allocation layout this corrupted a live object and crashed in the next finalize (seen on Clang 5 / Python 3.9 CI after the ref_small_vector change shifted the allocation pattern).
henryiii
force-pushed
the
fix/ref-small-vector-move
branch
from
September 3, 2026 04:05
2b4f94e to
a50e6af
Compare
henryiii
commented
Sep 3, 2026
| auto enum_mod = py::module_::import("enum_module"); | ||
| REQUIRE(enum_mod.attr("SomeEnum").attr("value1").attr("name").cast<std::string>() == "value1"); | ||
| { | ||
| // Scoped so that no reference to the old interpreter's module survives the restart. |
Collaborator
Author
There was a problem hiding this comment.
This was pretty terrible to debug. At least, it was for Claude, I mostly left it in the background. :)
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.
🤖 AI text below 🤖
inline_array_or_vectorused the implicit move constructor of its inline array, which copies the array and its size. The source kept its elements. Forref_small_vector, which owns a reference for each element, both the source and the destination then released the same references.The inline array now has a move constructor and a move assignment operator that set the source size to 0. The heap branch already leaves the source empty, because
std::vectorguarantees it. The comment in theref_small_vectormove constructor is now correct.The problem is latent today, because the only user relies on copy elision.
The change made the Clang 5 / Python 3.9 CI job segfault in the Catch "Threads" test, during interpreter finalization. The cause is a pre-existing bug in the "Enum module survives restart" test: it kept a
py::module_from the old interpreter acrossfinalize_interpreter(), then reassigned it, which decrefs a stale pointer into memory the new interpreter had reused. The changed allocation pattern made that stale write hit a live object. The test now scopes the first import.Fixes item 24 of #6159.
Suggested changelog entry:
detail::ref_small_vectornow leaves the source empty.