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: Fix `test_memoryio` refleak buildbots by ZeroIntensity · Pull Request #135430 · python/cpython

Conversation

@ZeroIntensity

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

Uh oh!

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

Copy link
Copy Markdown
Member

The problem is that we were testing both _pyio.StringIO and _io.StringIO. The former, in _pyio, isn't really thread-safe (but it doesn't crash, and that's what we care about). I'm not sure why it only revealed itself on the refleak buildbots, but it's probably something about the threads being slower.

Anyways, I'm fixing this by only testing the C implementation of StringIO. Making the Python version thread-safe is a separate project that we shouldn't deal with here. I also changed the assertIsNone to an if that actually raises the exception, so we get a better traceback when dealing with failures.

  • 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 2 commits June 12, 2025 09:34
@ZeroIntensity
Fix memory leak buildbots.
b98c4fb
@ZeroIntensity
Remove newlines.
b6f2011
@ZeroIntensity
ZeroIntensity requested review from kumaraditya303 and vstinner June 12, 2025 13:40
@ZeroIntensity ZeroIntensity added skip news needs backport to 3.14 bugs and security fixes labels Jun 12, 2025
@bedevere-app bedevere-app Bot added awaiting review tests Tests in the Lib/test dir labels Jun 12, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jun 12, 2025
Segfault from calling StringIO methods in threads on free-threading debug build #135410
Closed
@ZeroIntensity

ZeroIntensity commented Jun 12, 2025

Copy link
Copy Markdown
Member Author

!buildbot nogil refleak

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

🤖 New build scheduled with the buildbot fleet by @ZeroIntensity for commit b6f2011 🤖

Results will be shown at:

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

The command will test the builders whose names match following regular expression: nogil refleak

The builders matched are:

  • s390x Fedora Rawhide NoGIL refleaks PR
  • AMD64 CentOS9 NoGIL Refleaks PR
  • aarch64 Fedora Rawhide NoGIL refleaks PR
  • PPC64LE Fedora Rawhide NoGIL refleaks PR
  • AMD64 Fedora Rawhide NoGIL refleaks PR
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
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Jun 12, 2025
@corona10

corona10 commented Jun 12, 2025

Copy link
Copy Markdown
Member

The problem is that we were testing both _pyio.StringIO and _io.StringIO. The former, in _pyio, isn't really thread-safe (but it doesn't crash, and that's what we care about)

Sorry for challenging your changes recently, but why don’t we ensure consistent behavior between the pure Python and C versions?
Can we first focus on preventing the segmentation fault, rather than introducing behavioral differences between the two implementations? And just document it that we do not gurentee the thread safe?

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

Please hold on: "IMO this change was merged too quickly and I suggest to revert it." #135412 (comment)

All reactions

Sorry, something went wrong.

Uh oh!

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

@kumaraditya303

kumaraditya303 commented Jun 12, 2025

Copy link
Copy Markdown
Contributor

Sorry for challenging your changes recently, but why don’t we ensure consistent behavior between the pure Python and C versions?

The pure Python will not crash the interpreter while the C implementation can crash and the easiest way to fix the latter is to add critical section. We are trying to fix thread safety in regard to avoid being able to crash the interpreter.

Another somewhat related is #130095 where abc's pure Python implementation is not thread safe but it cannot crash the interp so we aren't actively fixing that, rather skipping the pure python tests of it.

As for performance, critical sections are optimized for case where single thread acquires it so it is minimal, trying to write to same StringIO object would perform worse but that's not something users should do or we should try to optimize for.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity ZeroIntensity added the DO-NOT-MERGE label Jun 12, 2025
@ZeroIntensity

ZeroIntensity commented Jun 12, 2025

Copy link
Copy Markdown
Member Author

Sorry for challenging your changes recently

No worries, I don't take it personally :)

Can we first focus on preventing the segmentation fault, rather than introducing behavioral differences between the two implementations? And just document it that we do not gurentee the thread safe?

That's effectively what I was trying to do. #135412 was trying to solely fix the segfault. Unfortunately, the only easy way to do that is by slapping a critical section onto __next__, which incidentally makes it work pretty sane across threads. I'm not a huge fan of it either, but I can't think of a different way to do this.

I agree with Kumar that this probably won't be noticeable for single-threaded code. I pushed back on locking TextIOWrapper.__next__ a few weeks ago because of performance, but the reality is that critical sections are quite fast when they're uncontended.

All reactions

Sorry, something went wrong.

Uh oh!

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

@corona10

corona10 commented Jun 12, 2025

Copy link
Copy Markdown
Member

The pure Python will not crash the interpreter while the C implementation can crash and the easiest way to fix the latter is to add critical section. We are trying to fix thread safety in regard to avoid being able to crash the interpreter.

I know pure Python code doesn’t crash the interpreter, but that’s not the point. The problem is that it behaves differently from the C implementation: the Python version is not thread-safe, while the C version is. We should aim for consistent behavior between them and consider what we’ve done so far toward that goal. That’s why I suggested we revisit our approach to make the behavior consistent between the pure Python and C implementations.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity

ZeroIntensity commented Jun 13, 2025

Copy link
Copy Markdown
Member Author

Closing now that the original change has been reverted.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity ZeroIntensity closed this Jun 13, 2025
@ZeroIntensity
ZeroIntensity deleted the fix-stringio-bots branch June 13, 2025 00:07
@AA-Turner AA-Turner removed the needs backport to 3.14 bugs and security fixes label Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@vstinner vstinner Awaiting requested review from vstinner

Uh oh!

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

Assignees

No one assigned

Labels

awaiting merge DO-NOT-MERGE skip news tests Tests in the Lib/test dir

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 @bedevere-bot @corona10 @vstinner @kumaraditya303 @AA-Turner
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.