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

gh-123321: Make Parser/myreadline.c locking safe in free-threaded build #123690

Merged
merged 2 commits into from
Sep 6, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Sep 4, 2024

Use a PyMutex to avoid the race in mutex initialization. Use relaxed atomics to avoid the data race on reading _PyOS_ReadlineTState when checking for re-entrant calls.

…ed build

Use a `PyMutex` to avoid the race in mutex initialization. Use relaxed
atomics to avoid the data race on reading _PyOS_ReadlineTState when
checking for re-entrant calls.
@colesbury colesbury marked this pull request as ready for review September 4, 2024 19:30
@colesbury colesbury requested a review from vstinner September 4, 2024 19:30
@colesbury
Copy link
Contributor Author

@bharel - here are the modifications for the free-threaded build

}
}

Py_BEGIN_ALLOW_THREADS
Copy link
Contributor

Choose a reason for hiding this comment

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

What about the GIL though? We don't release it for the regular build?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's right. We don't need it for the PyMutex, which handles it internally when it blocks, but we probably still need to release it for the readline call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does PyMutex take the GIL if it's called outside of the GIL?
Or is it supposed to be called only within the GIL?

Not next to the code atm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can call PyMutex_Lock (and PyMutex_Unlock) with or without the GIL.

If the GIL is currently held AND PyMutex_Lock() needs to wait on the mutex, it will release the GIL while it waits on the mutex and reacquire the GIL before returning.

If the GIL is not currently held, then it's not touched.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM. static PyMutex is convenient!

if (PyOS_ReadlineFunctionPointer == NULL) {
PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
}

if (_PyOS_ReadlineLock == NULL) {
_PyOS_ReadlineLock = PyThread_allocate_lock();
Copy link
Member

Choose a reason for hiding this comment

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

I understand that this PR removes a race condition on this line by using static PyMutex _PyOS_ReadlineLock; which is initialized statically and so avoids the race condition.

The PyOS_ReadlineFunctionPointer and PyOS_StdioReadline expect to be
called with the GIL released.
Copy link
Contributor

@bharel bharel left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the explanations 😄

@colesbury colesbury merged commit 0c080d7 into python:main Sep 6, 2024
35 checks passed
@colesbury colesbury deleted the gh-123321-free-threaded branch September 6, 2024 19:07
@colesbury colesbury added the needs backport to 3.13 bugs and security fixes label Sep 6, 2024
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 6, 2024
…ed build (pythonGH-123690)

Use a `PyMutex` to avoid the race in mutex initialization. Use relaxed
atomics to avoid the data race on reading `_PyOS_ReadlineTState` when
checking for re-entrant calls.
(cherry picked from commit 0c080d7)

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

bedevere-app bot commented Sep 6, 2024

GH-123798 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 Sep 6, 2024
@colesbury
Copy link
Contributor Author

I set up the backport PR for 3.13, but since 3.13.0rc2 is just around the corner, I expect it will wait until after the 3.13.0 final release.

Yhg1s pushed a commit that referenced this pull request Sep 6, 2024
…ded build (GH-123690) (#123798)

gh-123321: Make Parser/myreadline.c locking safe in free-threaded build (GH-123690)

Use a `PyMutex` to avoid the race in mutex initialization. Use relaxed
atomics to avoid the data race on reading `_PyOS_ReadlineTState` when
checking for re-entrant calls.
(cherry picked from commit 0c080d7)

Co-authored-by: Sam Gross <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants