gh-151814: Fix unbounded memory growth from repeated empty writes to io.TextIOWrapper#151817
Open
StanFromIreland wants to merge 2 commits into
Open
gh-151814: Fix unbounded memory growth from repeated empty writes to io.TextIOWrapper#151817StanFromIreland wants to merge 2 commits into
io.TextIOWrapper#151817StanFromIreland wants to merge 2 commits into
Conversation
StanFromIreland
commented
Jun 20, 2026
| else if (!PyList_CheckExact(self->pending_bytes)) { | ||
| PyObject *list = PyList_New(2); | ||
| if (list == NULL) { | ||
| if (bytes_len > 0) { |
Member
Author
There was a problem hiding this comment.
Git seems to render the diff poorly, locally I see with -w (--ignore-all-space):
$ git show -w HEAD -- Modules/_io/textio.c
commit c6b5163133619febd0fbe8c327e52399b1a54ffd (HEAD -> textio-acc, origin/textio-acc)
Author: Stan Ulbrych <stan@python.org>
Date: Sat Jun 20 21:16:54 2026 +0100
Fix unbounded memory growth from repeated empty writes to io.TextIOWrapper
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 24e08cec88f..5b2a20a30c2 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -1820,6 +1820,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
}
}
+ if (bytes_len > 0) {
if (self->pending_bytes == NULL) {
assert(self->pending_bytes_count == 0);
self->pending_bytes = b;
@@ -1846,6 +1847,11 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
}
self->pending_bytes_count += bytes_len;
+ }
+ else {
+ Py_DECREF(b);
+ }
+
if (self->pending_bytes_count >= self->chunk_size || needflush ||
text_needflush) {
if (_textiowrapper_writeflush(self) < 0)io.TextIOWrapper
cmaloney
reviewed
Jun 20, 2026
cmaloney
approved these changes
Jun 21, 2026
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
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.