Skip to content

gh-137422: Fix race condition in PyImport_AddModuleRef - #141822

Merged
colesbury merged 2 commits into
python:mainfrom
colesbury:gh-137422-pyimport-addmoduleref
Nov 21, 2025
Merged

colesbury merged 2 commits into
python:mainfrom
colesbury:gh-137422-pyimport-addmoduleref

Conversation

@colesbury

@colesbury colesbury commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor
@colesbury

Copy link
Copy Markdown
Contributor Author

It ended up being easier to use Py_BEGIN_CRITICAL_SECTION than trying to use PyDict_SetDefaultRef:

It's tricky to correctly handle the case where there's an existing entry that fails the PyModule_Check(m) test with PyDict_SetDefaultRef.

Even though interp->modules is always a PyDictObject, a bunch of the code was changed a few years ago to use the abstract API, so I didn't want to mess with it. See:

@kumaraditya303 kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

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 2d50dd2 into python:main Nov 21, 2025
@miss-islington-app

Copy link
Copy Markdown

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

@colesbury
colesbury deleted the gh-137422-pyimport-addmoduleref branch November 21, 2025 18:30
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 21, 2025
…h-141822)

(cherry picked from commit 2d50dd2)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@bedevere-app

bedevere-app Bot commented Nov 21, 2025

Copy link
Copy Markdown

GH-141830 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 Nov 21, 2025
colesbury added a commit that referenced this pull request Nov 21, 2025
) (gh-141830)

(cherry picked from commit 2d50dd2)

Co-authored-by: Sam Gross <colesbury@gmail.com>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants