You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Runaway memory use when repeatedly printing empty string #151814
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 commentedon Jun 20, 2026
I think it's simply because there is no flushing so I think it's still being buffered.
cc @cmaloney
zware commentedon 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 commentedon 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 theTextIOWrapper'spending_byteslist but added 0 topending_bytes_count(below,bytes_lenis 0 withb''). Since the buffer flush is triggered only whenpending_bytes_countreacheschunk_size, it never flushed but did accumulate the objects.cpython/Modules/_io/textio.c
Lines 1848 to 1853 in aec0aed
cmaloney commentedon Jun 20, 2026
👍 real bug should fix; I think this is missing a path for
bytes_len == 0to avoid appending to the list in that case. Currently in that case inwrite_throughmode we still flush which I think is important to keep.StanFromIreland commentedon 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…