Skip to content

gh-146379: Avoid StreamReader BufferError with active memoryview exports#146380

Open
MadLongTom wants to merge 2 commits intopython:mainfrom
MadLongTom:fix-streamreader-bufferror
Open

gh-146379: Avoid StreamReader BufferError with active memoryview exports#146380
MadLongTom wants to merge 2 commits intopython:mainfrom
MadLongTom:fix-streamreader-bufferror

Conversation

@MadLongTom
Copy link

@MadLongTom MadLongTom commented Mar 24, 2026

Description

Summary

This PR fixes BufferError in asyncio.StreamReader when a memoryview export exists on the internal buffer and StreamReader performs in-place bytearray mutation.

Problem

Current StreamReader logic includes in-place mutations (slice delete / clear). If a memoryview is active, bytearray resizing raises:

BufferError: Existing exports of data: object cannot be re-sized

Changes

  • Update StreamReader buffer-consumption paths to avoid in-place resize operations.
  • Use replacement assignment patterns that do not mutate exported bytearray objects in place.
  • Apply the fix consistently across:
    • read
    • readline
    • readuntil
    • readexactly

Tests

  • Added regression tests for each affected method with an active memoryview on the internal buffer.
  • Added coverage for both normal consume paths and EOF/error paths.
  • Verified tests fail before fix and pass after fix.

Behavioral impact

  • No API change.
  • No semantic change to returned data.
  • Fixes crashes under valid runtime scenarios where memoryview exports exist.

Checklist

  • Added/updated tests
  • Ran relevant test suite successfully
  • Added NEWS entry if required by core team guidance
  • Linked issue in title/body and followed CPython contribution workflow

Fixes #146379

…exports

StreamReader methods (read, readline, readuntil, readexactly) raise
BufferError when a memoryview is held on the internal buffer, because
buffer mutations (take_bytes, del slice, clear) cannot resize a
bytearray with active exports.

Add _consume_buffer() helper that tries take_bytes() first, then
falls back to non-mutating object replacement when BufferError is
raised. Replace all buffer-consumption call sites in StreamReader
to use _consume_buffer().

Also fix readline's LimitOverrunError handler which still used
del-slice and .clear() instead of take_bytes().

Add 7 regression tests covering all affected methods with an active
memoryview on the internal buffer.
@bedevere-app
Copy link

bedevere-app bot commented Mar 24, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@MadLongTom MadLongTom marked this pull request as ready for review March 24, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

asyncio.StreamReader raises BufferError with active memoryview

1 participant