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

[smart_holder] Wrong caching of overrides #3462

Closed
wants to merge 1 commit into from

Conversation

Trigve
Copy link
Contributor

@Trigve Trigve commented Nov 13, 2021

Description

There was a problem when the python type, which was stored in override
cache for C++ functions, was destroyed and the record wasn't removed from the
override cache. Therefor, dangling pointer was stored there. Then when the
memory was reused and new type was allocated at the given address and the
method with the same name (as previously stored in the cache) was actually
overridden in python, it would wrongly find it in the override cache for C++
functions and therefor override from python wouldn't be called.
The fix is to erase the type from the override cache when the type is destroyed.

Closes: #3369

Suggested changelog entry:

Fix caching of the C++ overrides

@Trigve Trigve requested a review from henryiii as a code owner November 13, 2021 11:17
@Trigve Trigve force-pushed the override_cache branch 3 times, most recently from 0d0ae51 to 7e346fd Compare November 13, 2021 11:47
There was a problem when the python type, which was stored in override
cache for C++ functions, was destroyed and  the record wasn't removed from the
override cache. Therefor, dangling pointer was stored there. Then when the
memory was reused and new type was allocated at the given address and the
method with the same name (as previously stored in the cache) was actually
overridden in python, it would wrongly find it in the override cache for C++
functions and therefor override from python wouldn't be called.
The fix is to erase the type from the override cache when the type is destroyed.
@rwgk
Copy link
Collaborator

rwgk commented Nov 13, 2021

Awesome, thanks! This looks great at first sight. I triggered the CI.

Important, so I can follow your work: please do not force push changes other than rebase-on-master. Ideally force-push immediately after a rebase-on-master, then use only non-force pushes for your work. When this PR is merged, the commits will be squashed, therefore it doesn't matter for that purpose how many commits we have here.

I still have to look in more detail, but I believe we need to turn this into a PR against master, moving the added code in tests/test_class_sh_inheritance.* to tests/test_class.* (first guess). — All fixes that are not in the added code on the smart_holder branch need to go to master first, otherwise it will become very difficult to merge the smart_holder branch into master, which I'm hoping to do very soon (needs some work fixing docs etc.).

@rwgk
Copy link
Collaborator

rwgk commented Nov 13, 2021

The ClangTidy failure looks very easy to fix, could you please do that?

https://github.com/pybind/pybind11/runs/4199226407?check_suite_focus=true

/__w/pybind11/pybind11/tests/test_embed/test_interpreter.cpp:54:17: error: 'virtual' is redundant since the function is already declared 'override' [modernize-use-override,-warnings-as-errors]
    virtual int func() override { PYBIND11_OVERRIDE(int, test_derived, func); }
    ~~~~~~~~    ^
5405 warnings generated.

Edit: looks like 2 errors:

/__w/pybind11/pybind11/include/pybind11/detail/smart_holder_poc.h:84:5: error: delete called on non-final 'pybind11_tests::class_sh_inheritance::test_derived' that has virtual functions but non-virtual destructor [clang-diagnostic-delete-non-abstract-non-virtual-dtor,-warnings-as-errors]
    delete static_cast<T *>(raw_ptr);
    ^

@rwgk
Copy link
Collaborator

rwgk commented Nov 13, 2021

I believe if you fix the ClangTidy errors we will have a lot less failures. All compilers except MSVC seem to be upset about the missing virtual destructor. I'll stop looking at the pile of errors. Let's just run the CI again after the two ClangTidy fixes.

@Trigve
Copy link
Contributor Author

Trigve commented Nov 13, 2021

Awesome, thanks! This looks great at first sight. I triggered the CI.

Important, so I can follow your work: please do not force push changes other than rebase-on-master. Ideally force-push immediately after a rebase-on-master, then use only non-force pushes for your work. When this PR is merged, the commits will be squashed, therefore it doesn't matter for that purpose how many commits we have here.

I still have to look in more detail, but I believe we need to turn this into a PR against master, moving the added code in tests/test_class_sh_inheritance.* to tests/test_class.* (first guess). — All fixes that are not in the added code on the smart_holder branch need to go to master first, otherwise it will become very difficult to merge the smart_holder branch into master, which I'm hoping to do very soon (needs some work fixing docs etc.).

Ok. I've already have changes against master branch, but I the problem is, that in the smart_holder branch the tests would need to be modified (which is not a big deal).

So should I close this PR and open one against the master?

@rwgk
Copy link
Collaborator

rwgk commented Nov 13, 2021 via email

@Trigve
Copy link
Contributor Author

Trigve commented Nov 13, 2021

Typing on my phone. Could you please just leave this PR as is for now and transfer this work with adjustments to a second new PR against master? (When we have the master PR stable we can revisit this one.)

Done as #3465

@rwgk
Copy link
Collaborator

rwgk commented Nov 16, 2021

Hi @Trigve, I just merged the master changes into the smart_holder branch. I believe this PR is obsolete now, but please let me know if I overlooked something.

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.

3 participants