pybind / pybind/pybind11

Mutations to holder type aren't visible to the outer scope

Open
#4,746 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.