Agreed that it seems to be a buffering issue, but since there's not actually anything to buffer it seems there may be a real bug here.
On the other hand this is a degenerate case, so I'm not sure how much effort it's worth to track down :)
+1 for fixing, this is a real bug.
I took a look and it seems straightforward. Each write('') appended an empty bytes object to the TextIOWrapper's pending_bytes list but added 0 to pending_bytes_count (below, bytes_len is 0 with b''). Since the buffer flush is triggered only when pending_bytes_count reaches chunk_size, it never flushed but did accumulate the objects.
👍 real bug should fix; I think this is missing a path for bytes_len == 0 to avoid appending to the list in that case. Currently in that case in write_through mode we still flush which I think is important to keep.
Activity
picnixz commented on Jun 20, 2026
I think it's simply because there is no flushing so I think it's still being buffered.
cc @cmaloney
zware commented on Jun 20, 2026
Agreed that it seems to be a buffering issue, but since there's not actually anything to buffer it seems there may be a real bug here.
On the other hand this is a degenerate case, so I'm not sure how much effort it's worth to track down :)
StanFromIreland commented on Jun 20, 2026
+1 for fixing, this is a real bug.
I took a look and it seems straightforward. Each write('') appended an empty bytes object to the TextIOWrapper's pending_bytes list but added 0 to pending_bytes_count (below, bytes_len is 0 with b''). Since the buffer flush is triggered only when pending_bytes_count reaches chunk_size, it never flushed but did accumulate the objects.
cpython/Modules/_io/textio.c
Lines 1848 to 1853 in aec0aed
cmaloney commented on Jun 20, 2026
👍 real bug should fix; I think this is missing a path for bytes_len == 0 to avoid appending to the list in that case. Currently in that case in write_through mode we still flush which I think is important to keep.
StanFromIreland commented on Jun 20, 2026
Indeed, I already wrote a little patch: #151817 :-)
gh-151814: Fix unbounded memory growth from repeated empty writes to …
[3.15] gh-151814: Fix unbounded memory growth from repeated empty wri…
[3.14] gh-151814: Fix unbounded memory growth from repeated empty wri…
[3.13] gh-151814: Fix unbounded memory growth from repeated empty wri…
gh-151814: Add test for TextIOWrapper.write unbounded memory growth f…