FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-133982: Test _pyio.BytesIO in free-threaded tests by cmaloney · Pull Request #136218 · python/cpython · GitHub

/ cpython Public

gh-133982: Test _pyio.BytesIO in free-threaded tests - #136218

Merged
corona10 merged 7 commits into
python:mainfrom
cmaloney:pyio_bytesio_threading
Jul 4, 2025
Merged

gh-133982: Test _pyio.BytesIO in free-threaded tests#136218
corona10 merged 7 commits into
python:mainfrom
cmaloney:pyio_bytesio_threading

Conversation

cmaloney commented Jul 2, 2025
edited
Loading

Copy link
Copy Markdown
Contributor

The test was only checking one of the two I/O implementations. Ideally the two implementations should match behavior (and guarantees) in free-threaded Python.

Followed https://py-free-threading.github.io/porting/#general-considerations-for-porting as a general guide for "make multi-threaded safe". I have a general project to build benchmarks around I/O in my backlog (python/pyperformance#399) where I will likely work on optimizing _io / _pyio / _experimentalio performance down the line including in threaded contexts. For now though, goal is simple functional thread safety iterating to better.

_pyio.BytesIO has two parts to its state, _pos and _buffer that get updated independently at times (ex. seek just changes _pos) but often together (ex. write updates _pos, maybe extends _buffer, and copies data into _buffer). When updated together multiple threads simultaneously operating could cause issues, so introduced a lock self._lock to cover those cases.

The test was only checking one of the two I/O implementations. Ideally
the two implementations should match.

cmaloney commented Jul 2, 2025

Copy link
Copy Markdown
Contributor Author

The ThreadSanitizer data race failure looks real, investigating

cmaloney marked this pull request as draft July 2, 2025 22:27

cmaloney commented Jul 3, 2025

Copy link
Copy Markdown
Contributor Author

Updated to lock operations that effect multiple members which need to stay "in sync" (ex. buffer length + position in buffer during write). Believe this is ready for review.

cmaloney marked this pull request as ready for review July 3, 2025 04:42
cmaloney changed the title gh-133982: Test _pyio.BytesIO in free-threaded gh-133982: Test _pyio.BytesIO in free-threaded tests Jul 3, 2025

ZeroIntensity commented Jul 3, 2025
edited
Loading

Copy link
Copy Markdown
Member

cc @corona10 @kumaraditya303 (related to the problems in gh-135410).

corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is the current C implementation of BytesIO thread-safe?

I couldn’t find any mention in the documentation (https://docs.python.org/3/library/io.html#binary-i-o) explicitly stating whether BytesIO is thread-safe. If the C implementation is already thread-safe, I’d like to suggest updating the documentation to clarify that.

cmaloney commented Jul 3, 2025
edited
Loading

Copy link
Copy Markdown
Contributor Author

The C implementation (_io) was made thread safe in GH-132616, the _pyio version was not updated at that time. I don't believe _pyio is in any current CPython benchmarks so this shouldn't be critical for the performance metric.

Added a note about thread safety to the docs. Would be nice if there was a standard sphinx tag / annotation that could be added to objects to mark them as "safe to interact with from multiple threads in free-threaded build"

cmaloney commented Jul 3, 2025

Copy link
Copy Markdown
Contributor Author

merged main to rerun/work around flaky test gh-136186

corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM, yeah we don't have to think deeply about fallback implementation.

corona10 merged commit 48cb9b6 into python:main Jul 4, 2025
cmaloney deleted the pyio_bytesio_threading branch July 4, 2025 02:39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL