-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
[3.14] gh-132551: make io.BytesIO thread safe (GH-132616)
#138551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kumaraditya303
merged 4 commits into
python:3.14
from
kumaraditya303:backport-5dd3a3a-3.14
Oct 7, 2025
+399
−108
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b8cff9
[3.14] gh-132551: make `io.BytesIO` thread safe (GH-132616)
tom-pytel 47400f3
Update Lib/test/test_free_threading/test_io.py
kumaraditya303 6a67559
gh-132551: add missing critical sections on BytesIO methods (#137073)
kumaraditya303 2b6ddaf
Merge branch '3.14' into backport-5dd3a3a-3.14
kumaraditya303 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import threading | ||
| from unittest import TestCase | ||
| from test.support import threading_helper | ||
| from random import randint | ||
| from io import BytesIO | ||
| from sys import getsizeof | ||
|
|
||
|
|
||
| class TestBytesIO(TestCase): | ||
| # Test pretty much everything that can break under free-threading. | ||
| # Non-deterministic, but at least one of these things will fail if | ||
| # BytesIO object is not free-thread safe. | ||
|
|
||
| def check(self, funcs, *args): | ||
| barrier = threading.Barrier(len(funcs)) | ||
| threads = [] | ||
|
|
||
| for func in funcs: | ||
| thread = threading.Thread(target=func, args=(barrier, *args)) | ||
|
|
||
| threads.append(thread) | ||
|
|
||
| with threading_helper.start_threads(threads): | ||
| pass | ||
|
|
||
| @threading_helper.requires_working_threading() | ||
| @threading_helper.reap_threads | ||
| def test_free_threading(self): | ||
| """Test for segfaults and aborts.""" | ||
|
|
||
| def write(barrier, b, *ignore): | ||
| barrier.wait() | ||
| try: b.write(b'0' * randint(100, 1000)) | ||
| except ValueError: pass # ignore write fail to closed file | ||
|
|
||
| def writelines(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.write(b'0\n' * randint(100, 1000)) | ||
|
|
||
| def truncate(barrier, b, *ignore): | ||
| barrier.wait() | ||
| try: b.truncate(0) | ||
| except: BufferError # ignore exported buffer | ||
|
|
||
| def read(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.read() | ||
|
|
||
| def read1(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.read1() | ||
|
|
||
| def readline(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.readline() | ||
|
|
||
| def readlines(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.readlines() | ||
|
|
||
| def readinto(barrier, b, into, *ignore): | ||
| barrier.wait() | ||
| b.readinto(into) | ||
|
|
||
| def close(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.close() | ||
|
|
||
| def getvalue(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.getvalue() | ||
|
|
||
| def getbuffer(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.getbuffer() | ||
|
|
||
| def iter(barrier, b, *ignore): | ||
| barrier.wait() | ||
| list(b) | ||
|
|
||
| def getstate(barrier, b, *ignore): | ||
| barrier.wait() | ||
| b.__getstate__() | ||
|
|
||
| def setstate(barrier, b, st, *ignore): | ||
| barrier.wait() | ||
| b.__setstate__(st) | ||
|
|
||
| def sizeof(barrier, b, *ignore): | ||
| barrier.wait() | ||
| getsizeof(b) | ||
|
|
||
| self.check([write] * 10, BytesIO()) | ||
| self.check([writelines] * 10, BytesIO()) | ||
| self.check([write] * 10 + [truncate] * 10, BytesIO()) | ||
| self.check([truncate] + [read] * 10, BytesIO(b'0\n'*204800)) | ||
| self.check([truncate] + [read1] * 10, BytesIO(b'0\n'*204800)) | ||
| self.check([truncate] + [readline] * 10, BytesIO(b'0\n'*20480)) | ||
| self.check([truncate] + [readlines] * 10, BytesIO(b'0\n'*20480)) | ||
| self.check([truncate] + [readinto] * 10, BytesIO(b'0\n'*204800), bytearray(b'0\n'*204800)) | ||
| self.check([close] + [write] * 10, BytesIO()) | ||
| self.check([truncate] + [getvalue] * 10, BytesIO(b'0\n'*204800)) | ||
| self.check([truncate] + [getbuffer] * 10, BytesIO(b'0\n'*204800)) | ||
| self.check([truncate] + [iter] * 10, BytesIO(b'0\n'*20480)) | ||
| self.check([truncate] + [getstate] * 10, BytesIO(b'0\n'*204800)) | ||
| self.check([truncate] + [setstate] * 10, BytesIO(b'0\n'*204800), (b'123', 0, None)) | ||
| self.check([truncate] + [sizeof] * 10, BytesIO(b'0\n'*204800)) | ||
|
|
||
| # no tests for seek or tell because they don't break anything | ||
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2025-04-16-21-02-57.gh-issue-132551.Psa7pL.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Make :class:`io.BytesIO` safe in :term:`free-threaded <free threading>` build. |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.