Skip to content

gh-157242: Do not close io.BytesIO on MemoryError - #157344

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:bytesio
Sep 12, 2026
Merged

gh-157242: Do not close io.BytesIO on MemoryError#157344
vstinner merged 2 commits into
python:mainfrom
vstinner:bytesio

Conversation

@vstinner

@vstinner vstinner commented Sep 12, 2026

Copy link
Copy Markdown
Member

Replace _PyBytes_Resize() with _PyBytes_ResizeKeepOnError().

Fix truncate(): only set string_size on resize success.

Replace _PyBytes_Resize() with _PyBytes_ResizeKeepOnError().

Fix truncate(): only set string_size on resize success.
Revert the string_size change. Instead, restore string_size to its
previous value on error.
@vstinner

Copy link
Copy Markdown
Member Author

io.StringIO makes the object inconsistent on MemoryError! It looks more complicated to fix it, so I focused this PR on io.BytesIO.

@vstinner

Copy link
Copy Markdown
Member Author

There is still an issue in the Python implementation of BytesIO.truncate(): del self._buffer[pos:] is not atomic on MemoryError, it leaves the byterray in an inconsistent state.

I merge this change anyway to make the situation a little bit better. Replacing _PyBytes_Resize() with _PyBytes_ResizeKeepOnError() doesn't impact performance.

@vstinner
vstinner merged commit 23eda00 into python:main Sep 12, 2026
54 checks passed
@vstinner
vstinner deleted the bytesio branch September 12, 2026 17:06
Sign up for free to 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.

1 participant