GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146041: Avoid lock in sys.intern() for already interned strings by colesbury · Pull Request #146072 · python/cpython

Conversation

@colesbury

@colesbury colesbury commented Mar 17, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fix free-threading scaling bottleneck in sys.intern and PyObject_SetAttr by avoiding the interpreter-wide lock when the string is already interned and immortalized.

  • Issue: Free threading scaling bottleneck in sys.intern() #146041

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@colesbury
pythongh-146041: Avoid lock in sys.intern() for already interned strings
6142b58
@colesbury colesbury added the topic-free-threading label Mar 17, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Mar 17, 2026
Free threading scaling bottleneck in sys.intern() #146041
Closed
@colesbury
colesbury marked this pull request as ready for review March 17, 2026 19:15
@colesbury
colesbury requested review from AA-Turner and ZeroIntensity as code owners March 17, 2026 19:15
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 17, 2026
@colesbury
colesbury requested review from encukou and mpage March 17, 2026 19:15
@colesbury

colesbury commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

@encukou, please let me know if you'd like me to do this differently. I've removed the assertion about unicode state in _Py_SetImmortalUntracked. It was awkward to keep because this PR requires that interned state is set to SSTATE_INTERNED_IMMORTAL as the last step in interning for thread-safety.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mpage
mpage approved these changes Mar 17, 2026
View reviewed changes

@mpage mpage left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks correct to me

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/unicodeobject.c
PyObject *r;
int res = PyDict_GetItemRef(interned, s, &r);
if (res < 0) {
PyErr_Clear();

@mpage mpage Mar 17, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Clearing the error here feels a little weird to me, but it's consistent with the rest of the function.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Mar 17, 2026
colesbury added 3 commits March 17, 2026 22:20
@colesbury
Add scaling benchmark and fix refcount contention on type
4f977e0
@colesbury
Any string for prefix is fine
5ba9f63
@colesbury
Merge branch 'main' into pythongh-146041-intern-lock-free
6e59598
@colesbury colesbury added 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section needs backport to 3.14 bugs and security fixes labels Mar 24, 2026
@bedevere-bot

bedevere-bot commented Mar 24, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 6e59598 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F146072%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Mar 24, 2026
@colesbury
colesbury merged commit 6009309 into python:main Mar 24, 2026
98 of 103 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Mar 24, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 24, 2026
@miss-islington-app

miss-islington-app Bot commented Mar 24, 2026

Copy link
Copy Markdown

Sorry, @colesbury, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 60093096ba62110151d822b072a01061876e9404 3.14
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned colesbury Mar 24, 2026
@colesbury
colesbury deleted the gh-146041-intern-lock-free branch March 24, 2026 18:29
colesbury added a commit to colesbury/cpython that referenced this pull request Mar 24, 2026
@colesbury
[3.14] pythongh-146041: Avoid lock in sys.intern() for already intern…
7fa9a56
…ed strings (pythongh-146072)

Fix free-threading scaling bottleneck in sys.intern and `PyObject_SetAttr` by
avoiding the interpreter-wide lock when the string is already interned and
immortalized.
(cherry picked from commit 6009309)

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

bedevere-app Bot commented Mar 24, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Mar 24, 2026
@colesbury colesbury removed their assignment Mar 24, 2026
colesbury added a commit that referenced this pull request Mar 25, 2026
@colesbury
[3.14] gh-146041: Avoid lock in sys.intern() for already interned str…
f2b5131
…ings (gh-146072) (#146390)

Fix free-threading scaling bottleneck in sys.intern and `PyObject_SetAttr` by
avoiding the interpreter-wide lock when the string is already interned and
immortalized.

(cherry picked from commit 6009309)
@pablogsal pablogsal mentioned this pull request Apr 4, 2026
gh-130273: Fix traceback color output with unicode characters #142529
Merged
@devdanzin devdanzin mentioned this pull request Apr 10, 2026
_Py_SetImmortal must be run on allocating thread (no-gil) #113956
Closed
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Apr 16, 2026
@colesbury @clin1234
pythongh-146041: Avoid lock in sys.intern() for already interned stri…
f6cc8f5
…ngs (pythongh-146072)

Fix free-threading scaling bottleneck in sys.intern and `PyObject_SetAttr` by
avoiding the interpreter-wide lock when the string is already interned and
immortalized.
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@colesbury @ljfp
pythongh-146041: Avoid lock in sys.intern() for already interned stri…
9e07d04
…ngs (pythongh-146072)

Fix free-threading scaling bottleneck in sys.intern and `PyObject_SetAttr` by
avoiding the interpreter-wide lock when the string is already interned and
immortalized.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@mpage mpage mpage approved these changes
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@encukou encukou Awaiting requested review from encukou

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-free-threading

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@colesbury @bedevere-bot @mpage
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.