Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear local internals after finalizing interpreter #2101 #3744

Merged
merged 3 commits into from Feb 20, 2022

Conversation

StarQTius
Copy link
Contributor

@StarQTius StarQTius commented Feb 17, 2022

Description

The author of #2101 proposed to clear the local internals after finalizing an interpreter. It seems that when registering a type as local, some of the data is managed by the current interpreter through pointers, which are then dangling when the interpreter is finalized. I implemented the solution suggested by the author, as it does not seem that keeping the local internals between two consecutive interpreters is the expected behavior.

Suggested changelog entry:

Local internals are now cleared after finalizing the interpreter.

Copy link
Collaborator

@henryiii henryiii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me, though I don't use this functionality much. @Skylion007 or @rwgk?

@rwgk
Copy link
Collaborator

rwgk commented Feb 18, 2022

Seems fine to me, though I don't use this functionality much. @Skylion007 or @rwgk?

+1

I could run this through Google's global testing, then merge if there are no issues.

There is a chance that even the global testing doesn't hit this change, embedding is on our "don't use" list.

But this PR looks good to me, nice test.

Copy link
Collaborator

@Skylion007 Skylion007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Although I would appreciate in a comment in the test which links back to the issue, and maybe another comment in embed.h about why clearing the interpreters is necessary.

@StarQTius
Copy link
Contributor Author

This looks good to me. Although I would appreciate in a comment in the test which links back to the issue, and maybe another comment in embed.h about why clearing the interpreters is necessary.

It's done. I'm sorry for the extra commit.

@rwgk
Copy link
Collaborator

rwgk commented Feb 20, 2022 via email

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global testing went fine.

I actually ran it twice:

  1. Exactly this PR: everything worked (10s of millions of tests).
  2. With an added pybind11_fail in pybind11/embed.h: exactly 1 failure (this one fwiw).

Learned:

  • This PR is good.
  • I'm beginning to think adding test_embed to the build config for internal testing isn't difficult. (But later, maybe.)

// Local internals contains data managed by the current interpreter, so we must clear them to
// avoid undefined behaviors when initializing another interpreter
detail::get_local_internals().registered_types_cpp.clear();
detail::get_local_internals().registered_exception_translators.clear();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note: I verified that new TEST_CASE covers .registered_types_cpp.clear(), but it does not cover .registered_exception_translators.clear().
I think that's fine though, although if someone could add that test coverage in a follow-on PR that would be ideal.

** "covers" in the sense that the test breaks if the call is removed.

@rwgk rwgk merged commit 9aa676d into pybind:master Feb 20, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Feb 20, 2022
henryiii pushed a commit to henryiii/pybind11 that referenced this pull request Mar 2, 2022
…ybind#3744)

* Clear local internals after finalizing interpreter

* Add descriptive comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
henryiii pushed a commit that referenced this pull request Mar 2, 2022
* Clear local internals after finalizing interpreter

* Add descriptive comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Mar 29, 2022
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.

None yet

4 participants