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-118138: Adds test for multithreaded writes to files. by elistevens · Pull Request #119107 · python/cpython

Conversation

@elistevens

@elistevens elistevens commented May 17, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

Multithreaded writes to files (including standard calls to print() from threads) can result in assertion failures, missed dropped output, and (in non-debug builds) writing uninitialized memory out due to race conditions in _io_TextIOWrapper_write_impl and _textiowrapper_writeflush.

See: #118138

I've provided a change that fixes the original issue for me on python 3.10.12, but my attempt to turn the repro script into a self-contained test case fails with that patch on both 3.10.12 and 3.14.0a0 (main as of today). That patch makes self->pending_bytes always be a list, and calls PyList_SetSlice(pending, 0, pending_size_at_start, NULL) to remove entries after they've been processed. I strongly suspect that the patch only narrows the time windows for race conditions, not actually eliminate them. I also think that it might introduce the possibility of dropped or repeated output should those race conditions trigger, but I am uncertain if that's actually the case (and they don't happen in practice, according to the test script).

I am out of my depth and would appreciate a maintainer taking a look.

Thanks to @sterwill for the original repro script, which the test case here is a descendent of.

  • Issue: SSL session content bleeds into stdout with lots of threads #118138

Sorry, something went wrong.

Uh oh!

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

AngelJA reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@elistevens
Adds test for multithreaded writes to files.
dca6eb6
Multithreaded writes to files (including standard calls to print() from
threads) can result in assertion failures and potentially missed output due to
race conditions in _io_TextIOWrapper_write_impl and _textiowrapper_writeflush.

See: python#118138
@ghost

ghost commented May 17, 2024 •
edited by ghost
Loading

Uh oh!

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

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

  • [email protected]

Click the button to sign:
CLA not signed

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app

bedevere-app Bot commented May 17, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot added the awaiting review label May 17, 2024
@bedevere-app bedevere-app Bot mentioned this pull request May 17, 2024
SSL session content bleeds into stdout with lots of threads #118138
Closed
@siddhu032d

siddhu032d commented May 17, 2024

Copy link
Copy Markdown

Result: ACCESS

All reactions

Sorry, something went wrong.

Uh oh!

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

elistevens added 2 commits May 17, 2024 11:41
@elistevens
Calls executor.shutdown before closing file.
997dbc2
@elistevens
Fixes race conditions from multithreaded writes.
3f05fd9
@bedevere-app

bedevere-app Bot commented May 17, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

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

@elistevens

elistevens commented May 17, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Author

I believe that the issue with my patch was due to closing the file accepting output before calling executor.shutdown(wait=True). The modified test now passes with my change.

Edit: I also feel like there's probably a cleaner way to handle pending_bytes_count but given that I'm not even certain that this is the right way to solve the problem, I'm not investing too much thought into it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@elistevens

elistevens commented May 24, 2024

Copy link
Copy Markdown
Author

After deploying this patch at scale internally, I can confirm that there are still race condition issues; the test fails about 14% of the time in our CI cluster (interestingly it always passed while running locally, so it might be a load issue; I haven't investigated the exact nature of the failures yet). 14% is better than the 100% failure rate we were seeing prior, but it's still not fully fixed.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum
gvanrossum requested review from colesbury and ericsnowcurrently May 29, 2024 16:18
@elistevens elistevens mentioned this pull request May 30, 2024
gh-119506: fix _io.TextIOWrapper.write() write during flush #119507
Merged
@elistevens
Update test_io_threading.py to not suffer from interleaved prints
d156d3c
@bedevere-app

bedevere-app Bot commented May 30, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

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

@elistevens

elistevens commented May 30, 2024

Copy link
Copy Markdown
Author

I think that the test provided here might be useful (after being adapted to meet cpython standards), but the actual code change likely is not. On #119507 a much simpler patch achieves the same practical "it doesn't assert any longer" effect for my use case.

The issue is also being discussed here: https://discuss.python.org/t/bug-with-multi-threaded-print-write-causing-dropped-output-output-of-uninitialized-memory-assertion-failures/54538 where @pitrou suggests using a mutex to protect the internal state.

Should we close this PR?

All reactions

Sorry, something went wrong.

Uh oh!

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

@elistevens elistevens closed this Jun 4, 2024
@ericsnowcurrently

ericsnowcurrently commented Jun 4, 2024

Copy link
Copy Markdown
Member

@elistevens, thanks for taking the time you did on this PR. While there wasn't much feedback here, that DPO thread definitely served as a proxy, which is probably okay. Regardless, you clearly put a lot of thought into this and demonstrated a healthy dose of determination and skill. I hope there will be more opportunities for you to participate in Python core development. If you have any feedback, please let us know.

All reactions

Sorry, something went wrong.

Uh oh!

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

@elistevens

elistevens commented Jun 4, 2024

Copy link
Copy Markdown
Author

Thanks, Eric! I'm not worried about the relative quiet on this PR; #119507 had a lively discussion and ultimately resulted in a much cleaner fix than the one I proposed here. I also recognize that maintainers are quite busy and I think should focus on the PRs that are likely to merge.

Thank you for all the work that you all do!

All reactions

Sorry, something went wrong.

Uh oh!

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

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

Reviewers

@colesbury colesbury Awaiting requested review from colesbury
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently

Uh oh!

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

Assignees

No one assigned

Labels

awaiting review

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

@elistevens @siddhu032d @ericsnowcurrently
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.