Mutations to holder type aren't visible to the outer scope
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.9.1
Problem description
When an argument is passed-by-reference in C++, we would expect overwriting the reference to be visible from the outside scope.
Due to how Python argument passing works, this is not the case for wrapped C++ functions called from Python.
However, mutations to the underlying object are visible to the outside.
When an object wrapped in a holder type is passed to C++ (by reference), we would again not expect to see the overwrite in the outside scope.
However, when we update the pointer inside the holder type (mutating it), we would expect the changes to be visible to the outside world. This is not the case though.
Reproducible example code
See https://github.com/lucmans/pybind11_mutating_holder_bug for a minimal reproduction of the bug.
Specifically https://github.com/lucmans/pybind11_mutating_holder_bug/blob/1da5c36e15a3f6745e7a5e1093052203dbcd34e9/problem.cpp#L184
Is this a regression? Put the last known working version here if it is.
Not a regression
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproduction linked from the issue, especially problem.cpp around line 184, then trace the corresponding pybind11 holder and reference-handling path. Done means a pointer mutation inside a holder passed by reference is visible from the outside Python scope, with the behavior verified against the reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100