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-135410: Use a critical section around `StringIO.__next__` by ZeroIntensity · Pull Request #135412 · python/cpython

Conversation

@ZeroIntensity

@ZeroIntensity ZeroIntensity commented Jun 11, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

I'm generally not a fan of adding locks around __next__, because concurrent iteration isn't generally a real use case, but readlines calls into __next__.

  • Issue: Segfault from calling StringIO methods in threads on free-threading debug build #135410

Sorry, something went wrong.

Uh oh!

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

All reactions
ZeroIntensity added 3 commits June 11, 2025 19:05
@ZeroIntensity
Fix thread safety in StringIO.
5f3825b
@ZeroIntensity
Add blurb.
25d2f27
@ZeroIntensity
Add a test.
455bfba
@ZeroIntensity ZeroIntensity added the topic-free-threading label Jun 11, 2025
@bedevere-app bedevere-app Bot added the awaiting review label Jun 11, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jun 11, 2025
Segfault from calling StringIO methods in threads on free-threading debug build #135410
Closed
@corona10

corona10 commented Jun 11, 2025

Copy link
Copy Markdown
Member

Let's consider the following options.

  1. Prevent segfault only.
  2. Guarantee thread safety also. But IIRC, we do not guarantee all stdlib iterations.
All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity

ZeroIntensity commented Jun 11, 2025

Copy link
Copy Markdown
Member Author

Yeah, I totally agree on that. There was an issue similar to this a few weeks ago, where a TextIOWrapper.__next__ didn't have a critical section on it, which ended up causing crashes. I originally rejected it, but I was eventually convinced that it is worth doing for IO objects.

All reactions

Sorry, something went wrong.

Uh oh!

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

@corona10

corona10 commented Jun 12, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

I am fine with if this change will not regress overall performance. But if the object is shared by multiple threads user should care about the correctness if the object itself does not gurentee it.
cc @colesbury

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner approved these changes Jun 12, 2025
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

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.

LGTM.

Without the fix, the test fails randomly with:

FAIL: test_concurrent_use (test.test_memoryio.PyStringIOPickleTest.test_concurrent_use)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/vstinner/python/main/Lib/test/test_memoryio.py", line 742, in test_concurrent_use
    self.assertIsNone(cm.exc_value)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
AssertionError: UnboundLocalError("cannot access local variable 'dec_flags' where it is not associated with a value") is not None

So the test works as expected!

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 review labels Jun 12, 2025
@vstinner

vstinner commented Jun 12, 2025

Copy link
Copy Markdown
Member

But if the object is shared by multiple threads user should care about the correctness if the object itself does not gurentee it.

Currently, the doc says nothing about io.StringIO: https://docs.python.org/dev/library/io.html#multi-threading

All reactions

Sorry, something went wrong.

Uh oh!

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

kumaraditya303
kumaraditya303 approved these changes Jun 12, 2025
View reviewed changes
@kumaraditya303
kumaraditya303 merged commit e6c3039 into python:main Jun 12, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 12, 2025
@kumaraditya303 kumaraditya303 added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Jun 12, 2025
@miss-islington-app

miss-islington-app Bot commented Jun 12, 2025

Copy link
Copy Markdown

Thanks @ZeroIntensity for the PR, and @kumaraditya303 for merging it 🌮🎉.. 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.

@miss-islington-app

miss-islington-app Bot commented Jun 12, 2025

Copy link
Copy Markdown

Thanks @ZeroIntensity for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 12, 2025
@ZeroIntensity @miss-islington
pythongh-135410: use a critical section around StringIO.__next__ (p…
ead9fb0
…ythonGH-135412)

(cherry picked from commit e6c3039)

Co-authored-by: Peter Bierma <[email protected]>
@miss-islington-app

miss-islington-app Bot commented Jun 12, 2025

Copy link
Copy Markdown

Sorry, @ZeroIntensity and @kumaraditya303, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker e6c3039cb39e68ae9af9ddcaca341c5af8f9cf23 3.13
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 kumaraditya303 Jun 12, 2025
@bedevere-app

bedevere-app Bot commented Jun 12, 2025

Copy link
Copy Markdown

GH-135425 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 Jun 12, 2025
kumaraditya303 pushed a commit that referenced this pull request Jun 12, 2025
@miss-islington @ZeroIntensity
[3.14] gh-135410: use a critical section around StringIO.__next__ (G…
8cb7d9a
…H-135412) (#135425)

gh-135410: use a critical section around `StringIO.__next__` (GH-135412)
(cherry picked from commit e6c3039)

Co-authored-by: Peter Bierma <[email protected]>
@bedevere-bot

bedevere-bot commented Jun 12, 2025

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 CentOS9 NoGIL 3.14 (tier-1) has failed when building commit 8cb7d9a.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1733/builds/261) and take a look at the build logs.
  4. Check if the failure is related to this commit (8cb7d9a) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1733/builds/261

Failed tests:

  • test_memoryio

Failed subtests:

  • test_concurrent_use - test.test_memoryio.PyStringIOPickleTest.test_concurrent_use

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.14.itamaro-centos-aws.nogil/build/Lib/test/test_memoryio.py", line 742, in test_concurrent_use
    self.assertIsNone(cm.exc_value)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
AssertionError: UnboundLocalError("cannot access local variable 'dec_flags' where it is not associated with a value") is not None
All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot

bedevere-bot commented Jun 12, 2025

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 CentOS9 NoGIL Refleaks 3.x (tier-1) has failed when building commit e6c3039.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1610/builds/1566) and take a look at the build logs.
  4. Check if the failure is related to this commit (e6c3039) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1610/builds/1566

Failed tests:

  • test_memoryio

Failed subtests:

  • test_concurrent_use - test.test_memoryio.PyStringIOPickleTest.test_concurrent_use
  • test_concurrent_use - test.test_memoryio.PyStringIOTest.test_concurrent_use

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.itamaro-centos-aws.refleak.nogil/build/Lib/test/test_memoryio.py", line 742, in test_concurrent_use
    self.assertIsNone(cm.exc_value)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
AssertionError: UnboundLocalError("cannot access local variable 'dec_flags' where it is not associated with a value") is not None
All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot

bedevere-bot commented Jun 12, 2025

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 CentOS9 NoGIL Refleaks 3.14 (tier-1) has failed when building commit 8cb7d9a.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1774/builds/185) and take a look at the build logs.
  4. Check if the failure is related to this commit (8cb7d9a) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1774/builds/185

Failed tests:

  • test_memoryio

Failed subtests:

  • test_concurrent_use - test.test_memoryio.PyStringIOPickleTest.test_concurrent_use
  • test_concurrent_use - test.test_memoryio.PyStringIOTest.test_concurrent_use

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.14.itamaro-centos-aws.refleak.nogil/build/Lib/test/test_memoryio.py", line 742, in test_concurrent_use
    self.assertIsNone(cm.exc_value)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
AssertionError: UnboundLocalError("cannot access local variable 'dec_flags' where it is not associated with a value") is not None
All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity
ZeroIntensity deleted the fix-stringio branch June 12, 2025 13:29
@ZeroIntensity

ZeroIntensity commented Jun 12, 2025

Copy link
Copy Markdown
Member Author

Those buildbot failures look related. I'll quickly investigate and if I can't figure it out in the next half an hour or so, I'll put up a revert.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Jun 12, 2025

Copy link
Copy Markdown
Member

IMO this change was merged too quickly and I suggest to revert it.

  • I was awaiting for @colesbury to review the change.
  • @corona10 asked for benchmarks, this request was ignored.
  • I pointed out that the documentation says nothing about io.StringIO thread safety. I would prefer to address this point at the same time.
  • And now, Refleak buildbots are failing. It seems like the Python implementation (_pyio) behaves differently than the C implementation (io).

I suggest to revert the two changes (main and 3.14 branches) and spend more time to design this fix. I don't think that there is any urgency to fix this issue.

corona10 and ZeroIntensity reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Jun 12, 2025
gh-135410: Fix test_memoryio refleak buildbots #135430
Closed
@ZeroIntensity

ZeroIntensity commented Jun 12, 2025

Copy link
Copy Markdown
Member Author

Sorry for the hassle everyone! I didn't get a chance to reply to any comments before it was merged. I'll put up a revert while we discuss this.

vstinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

ZeroIntensity added a commit to ZeroIntensity/cpython that referenced this pull request Jun 12, 2025
@ZeroIntensity
Revert "pythongh-135410: use a critical section around `StringIO.__ne…
4ab61b3
…xt__` (python#135412)"

This reverts commit e6c3039.
corona10 pushed a commit that referenced this pull request Jun 12, 2025
@ZeroIntensity
Revert "gh-135410: use a critical section around StringIO.__next__ (#…
7343135
…135412)" (#135439)

This reverts commit e6c3039.
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 12, 2025
@ZeroIntensity @miss-islington
Revert "pythongh-135410: use a critical section around `StringIO.__ne…
fcc3493
…xt__` (pythonGH-135412)" (pythonGH-135439)

This reverts commit e6c3039.
(cherry picked from commit 7343135)

Co-authored-by: Peter Bierma <[email protected]>
corona10 pushed a commit that referenced this pull request Jun 12, 2025
@miss-islington @ZeroIntensity
[3.14] Revert "gh-135410: use a critical section around `StringIO.__n…
b3efd3d
…ext__` (GH-135412)" (GH-135439) (gh-135449)

Revert "gh-135410: use a critical section around `StringIO.__next__` (GH-135412)" (GH-135439)

This reverts commit e6c3039.
(cherry picked from commit 7343135)

Co-authored-by: Peter Bierma <[email protected]>
lkollar pushed a commit to lkollar/cpython that referenced this pull request Jun 19, 2025
@ZeroIntensity @lkollar
pythongh-135410: use a critical section around StringIO.__next__ (p…
c4d421c
…ython#135412)
lkollar pushed a commit to lkollar/cpython that referenced this pull request Jun 19, 2025
@ZeroIntensity @lkollar
Revert "pythongh-135410: use a critical section around `StringIO.__ne…
dd95024
…xt__` (python#135412)" (python#135439)

This reverts commit e6c3039.
@brianschubert brianschubert removed the needs backport to 3.13 only security fixes label Jun 24, 2025
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@ZeroIntensity @Pranjal095
pythongh-135410: use a critical section around StringIO.__next__ (p…
f58e3d9
…ython#135412)
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@ZeroIntensity @Pranjal095
Revert "pythongh-135410: use a critical section around `StringIO.__ne…
8ce3f1a
…xt__` (python#135412)" (python#135439)

This reverts commit e6c3039.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@ZeroIntensity @taegyunkim
pythongh-135410: use a critical section around StringIO.__next__ (p…
02a55c4
…ython#135412)
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@ZeroIntensity @taegyunkim
Revert "pythongh-135410: use a critical section around `StringIO.__ne…
902e279
…xt__` (python#135412)" (python#135439)

This reverts commit e6c3039.
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
@ZeroIntensity @Agent-Hellboy
pythongh-135410: use a critical section around StringIO.__next__ (p…
9b3cc43
…ython#135412)
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
@ZeroIntensity @Agent-Hellboy
Revert "pythongh-135410: use a critical section around `StringIO.__ne…
0313047
…xt__` (python#135412)" (python#135439)

This reverts commit e6c3039.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes

Uh oh!

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

Assignees

@kumaraditya303 kumaraditya303

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.

6 participants

@ZeroIntensity @corona10 @vstinner @bedevere-bot @kumaraditya303 @brianschubert
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 01402503 — 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.