Skip to content

fix: handle null py::handle and add tests for py::scoped_critical_section #5706

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

Merged

Conversation

XuehaiPan
Copy link
Contributor

@XuehaiPan XuehaiPan commented May 30, 2025

Description

A minor improvement to check if the handle is null.

Suggested changelog entry:

  • handle null py::handle for py::scoped_crititcal_section
  • Add tests for py::scoped_crititcal_section

@Skylion007
Copy link
Collaborator

@rwgk
Copy link
Collaborator

rwgk commented May 30, 2025

@XuehaiPan Could you please add tests?

@XuehaiPan XuehaiPan changed the title chore: handle null py::handle for py::scoped_crititcal_section chore: handle null py::handle for py::scoped_critical_section May 30, 2025
@henryiii
Copy link
Collaborator

henryiii commented May 30, 2025

What did you have in mind for tests? It's not that easy to test that this is or is not locking. You could just make sure it was callable with nullptr (since that does nothing), I suppose?

@XuehaiPan XuehaiPan force-pushed the handle-null-for-scoped_critical_section branch 2 times, most recently from 8828478 to 032fef2 Compare May 31, 2025 16:50
@rwgk
Copy link
Collaborator

rwgk commented May 31, 2025

What did you have in mind for tests? It's not that easy to test that this is or is not locking. You could just make sure it was callable with nullptr (since that does nothing), I suppose?

Sorry I somehow missed this question before.

Just covering all situations with nullptr. Does that make sense?

Basic code coverage.

Currently we have only this:

                py::scoped_critical_section lock(locals);

@rwgk
Copy link
Collaborator

rwgk commented May 31, 2025

I forgot to mention: I'm wondering, how is py::scoped_critical_section(handle()) useful? It'd be great to explain that somewhere, e.g. here in the PR description.

@XuehaiPan XuehaiPan requested a review from henryiii as a code owner May 31, 2025 17:34
@XuehaiPan XuehaiPan force-pushed the handle-null-for-scoped_critical_section branch from 58763fb to 4725667 Compare May 31, 2025 17:39
@henryiii
Copy link
Collaborator

henryiii commented Jun 1, 2025

Looks like C++20+ has a problem.

@henryiii
Copy link
Collaborator

henryiii commented Jun 3, 2025

Do you think you can solve the segfaults for C++20 and 23?

@XuehaiPan

This comment was marked as outdated.

@XuehaiPan XuehaiPan force-pushed the handle-null-for-scoped_critical_section branch 4 times, most recently from d6084c0 to fcfcddb Compare June 3, 2025 15:21
@XuehaiPan
Copy link
Contributor Author

The test failure on macOS with Python 3.14t seems unrelated and I cannot reproduce locally.

@henryiii henryiii force-pushed the handle-null-for-scoped_critical_section branch from fcfcddb to 950c15c Compare June 3, 2025 17:07
@XuehaiPan XuehaiPan changed the title chore: handle null py::handle for py::scoped_critical_section test: handle null py::handle and add tests for py::scoped_critical_section Jun 3, 2025
@henryiii henryiii force-pushed the handle-null-for-scoped_critical_section branch from cd724f0 to 009756f Compare June 3, 2025 20:20
@rwgk
Copy link
Collaborator

rwgk commented Jun 3, 2025

Drive-by comment.

The new test is much more comprehensive than I was hoping! I'll need to find some time to look carefully. (Hopefully later today.)

@henryiii
Copy link
Collaborator

henryiii commented Jun 4, 2025

I don't think it's valid in the free-threading build to call gc.collect and expect things to be collected. Collections happen as part of "stop-the-world" events, which that's not going to trigger, as it needs to sync the dual ref-counts. I think that's why the 3.14t test is failing.

Generally gc-based tests are not very good, there really aren't strong language-level guarantees about things getting cleaned up, it's implementation based.

Comment on lines +15 to +29
namespace test_scoped_critical_section_ns {

void test_one_nullptr() { py::scoped_critical_section lock{py::handle{}}; }

void test_two_nullptrs() { py::scoped_critical_section lock{py::handle{}, py::handle{}}; }

void test_first_nullptr() {
py::dict d;
py::scoped_critical_section lock{py::handle{}, d};
}

void test_second_nullptr() {
py::dict d;
py::scoped_critical_section lock{d, py::handle{}};
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

When I was suggesting to add tests, all I had in mind was something simple like this.

The additional tests look interesting though.

Could you please add comments to explain what the tests are for? (I'd ask my favorite LLM for suggestions, usually it's really quick that way.)

@rwgk
Copy link
Collaborator

rwgk commented Jun 4, 2025

This crossed my mind:

If it's troublesome to get the new comprehensive tests to work, I'd be in favor of splitting this PR, to just keep the changes in pybind11/critical_section.h plus the minimal tests meant to exercise that nullptrs don't cause segfaults.

@XuehaiPan
Copy link
Contributor Author

If it's troublesome to get the new comprehensive tests to work

Moved the cause of the flaky test of dynamic attributes to #5715.

Note that we should eventually address #5715 before the official Python 3.14 release.

@rwgk
Copy link
Collaborator

rwgk commented Jun 4, 2025

Everything passes, awesome!

Could you please review this analysis and distill out source code comments suitable for adding to this PR?

https://chatgpt.com/share/683fea11-df78-8008-b76d-749f9e8f0df8

@XuehaiPan XuehaiPan requested a review from rwgk June 4, 2025 06:49
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.

Nice! Just one minor suggestion.

@henryiii to you want to include this in RC3? (I think that could be useful.)

@henryiii henryiii merged commit c786d34 into pybind:master Jun 4, 2025
82 checks passed
@henryiii henryiii changed the title test: handle null py::handle and add tests for py::scoped_critical_section fix: handle null py::handle and add tests for py::scoped_critical_section Jun 4, 2025
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs changelog Possibly needs a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants