Skip to content

gh-151814: Fix unbounded memory growth from repeated empty writes to io.TextIOWrapper - #151817

Merged
StanFromIreland merged 2 commits into
python:mainfrom
StanFromIreland:textio-acc
Jun 24, 2026
Merged

gh-151814: Fix unbounded memory growth from repeated empty writes to io.TextIOWrapper#151817
StanFromIreland merged 2 commits into
python:mainfrom
StanFromIreland:textio-acc

Conversation

@StanFromIreland

@StanFromIrelandStanFromIreland commented Jun 20, 2026

Copy link
Copy Markdown
Member

Comment threadModules/_io/textio.c
else if (!PyList_CheckExact(self->pending_bytes)) {
PyObject *list = PyList_New(2);
if (list == NULL) {
if (bytes_len > 0) {

@StanFromIrelandStanFromIrelandJun 20, 2026

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you can use ?w=1 on GitHub fwiw

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Cool, thanks! But, it seems to be limited and unfortunately doesn’t support all options, ?ignore-all-space=1 didn't work for me.

@StanFromIrelandStanFromIreland changed the title gh-151814: Fix unbounded memory growth from repeated empty writes to io.TextIOWr…gh-151814: Fix unbounded memory growth from repeated empty writes to io.TextIOWrapperJun 20, 2026
Comment threadModules/_io/textio.c
Comment threadLib/test/test_io/test_textio.py Outdated
@StanFromIrelandStanFromIreland added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 22, 2026
@StanFromIreland
StanFromIreland merged commit c613072 into python:mainJun 24, 2026
60 checks passed
@StanFromIreland
StanFromIreland deleted the textio-acc branch June 24, 2026 11:47
@miss-islington-app

Copy link
Copy Markdown

Thanks @StanFromIreland for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@StanFromIreland

Copy link
Copy Markdown
MemberAuthor

Thanks for the review!

@bedevere-app

Copy link
Copy Markdown

GH-152071 is a backport of this pull request to the 3.15 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 24, 2026
@bedevere-app

Copy link
Copy Markdown

GH-152072 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Jun 24, 2026
@bedevere-app

Copy link
Copy Markdown

GH-152073 is a backport of this pull request to the 3.13 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.13 bugs and security fixes label Jun 24, 2026
StanFromIreland added a commit that referenced this pull request Jun 24, 2026
…tes to `io.TextIOWrapper` (GH-151817)
(cherry picked from commit c613072)
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jun 24, 2026
…tes to `io.TextIOWrapper` (GH-151817)
(cherry picked from commit c613072)
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jun 24, 2026
…tes to `io.TextIOWrapper` (GH-151817) (#152073)
(cherry picked from commit c613072)
Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@StanFromIreland@cmaloney@asottile