Skip to content

gh-135641: Fix flaky test_capi.test_lock_two_threads test case #135642

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
merged 1 commit into from
Jun 18, 2025

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Jun 17, 2025

The mutex may have the _Py_HAS_PARKED bit set in rare cases.

The mutex may have the _Py_HAS_PARKED bit set in rare cases.
@colesbury colesbury requested a review from mpage June 17, 2025 20:37
@colesbury colesbury added skip news needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jun 17, 2025
@bedevere-app bedevere-app bot added awaiting core review tests Tests in the Lib/test dir labels Jun 17, 2025
Comment on lines +60 to +62
// gh-135641: in rare cases the lock may still have `_Py_HAS_PARKED` set
// (m->_bits == 3) due to bucket collisions in the parking lot hash table
// between this mutex and the `test_data.done` event.
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any consequences of this being set incorrectly? I think another solution, though much more invasive, might be to move num_waiters from Bucket onto wait_entry.

Copy link
Contributor Author

@colesbury colesbury Jun 18, 2025

Choose a reason for hiding this comment

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

Are there any consequences of this being set incorrectly?

The unlock will just take a slower path. The approximate (conservative) setting of _Py_HAS_PARKED is taken from WebKit, so I'm not too worried about it. It's just easy to forget about when writing these sorts of tests.

I think another solution, though much more invasive, might be to move num_waiters from Bucket onto wait_entry.

You can do this nicely and efficiently if you restructure the buckets into a tree of linked lists, which is something we talked about when merging the original parking lot PR, but hasn't been a priority.

There's currently a potential O(N^2) behavior in parking lot due to a linear scan (where N=threads waiting on locks) that using a balanced tree would fix. That's not something I'd want to backport though.

Here's the equivalent fix in Go for their mutex implementation: golang/go@990124d

Copy link
Contributor

@mpage mpage left a comment

Choose a reason for hiding this comment

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

LGTM!

@colesbury colesbury merged commit 17ac393 into python:main Jun 18, 2025
52 checks passed
@colesbury colesbury deleted the gh-135641-test-lock-two-threads branch June 18, 2025 18:24
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 18, 2025
pythongh-135642)

The mutex may have the `_Py_HAS_PARKED` bit set.
(cherry picked from commit 17ac393)

Co-authored-by: Sam Gross <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 18, 2025
pythongh-135642)

The mutex may have the `_Py_HAS_PARKED` bit set.
(cherry picked from commit 17ac393)

Co-authored-by: Sam Gross <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jun 18, 2025

GH-135687 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jun 18, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jun 18, 2025

GH-135688 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 18, 2025
colesbury added a commit that referenced this pull request Jun 18, 2025
…se (gh-135642) (gh-135688)

The mutex may have the `_Py_HAS_PARKED` bit set.
(cherry picked from commit 17ac393)

Co-authored-by: Sam Gross <[email protected]>
colesbury added a commit that referenced this pull request Jun 18, 2025
…se (gh-135642) (gh-135687)

The mutex may have the `_Py_HAS_PARKED` bit set.
(cherry picked from commit 17ac393)

Co-authored-by: Sam Gross <[email protected]>
lkollar pushed a commit to lkollar/cpython that referenced this pull request Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants