asyncio.StreamReader raises BufferError with active memoryview
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
Summary
When StreamReader has an active memoryview export of its internal buffer, methods that mutate the buffer in place can raise:
BufferError: Existing exports of data: object cannot be re-sized
This appears in real workloads with async database drivers, but the root cause is in StreamReader buffer mutation behavior.
Steps to reproduce
import asyncio
async def main():
r = asyncio.StreamReader()
r.feed_data(b"AABBCCDD")
# Simulate a consumer that keeps a memoryview alive
mv = memoryview(r._buffer)
# Triggers BufferError in current implementation
await r.readexactly(4)
mv.release()
asyncio.run(main())
Expected behavior
readexactly(4) should return b"AABB" without raising BufferError.
Actual behavior
BufferError: Existing exports of data: object cannot be re-sized
Why this happens
Some StreamReader paths mutate bytearray in place, for example slice deletion and clear operations. In-place resize is not allowed while a memoryview export exists.
Affected methods
readreadlinereaduntilreadexactly
Environment
- Python: 3.13.x, 3.14.x (also reproducible in local tests with newer builds)
- OS: macOS (also observed in Linux server workloads)
- Third-party context where this is frequently hit: async DB drivers using memoryview over packet buffers
Possible fix direction
Replace in-place buffer mutations with non-mutating replacement assignments, for example:
- replace slice delete with reassignment to remaining bytes
- replace clear with assigning a new empty bytearray
If useful, I can open a PR with tests covering the regression paths.
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
- gh-146380
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với asyncio.StreamReader và các nhánh read, readline, readuntil và readexactly được mô tả trong báo cáo; chạy bản tái hiện được cung cấp để quan sát BufferError khi có một memoryview đang hoạt động. Được xem là hoàn thành khi các phương thức này trả về dữ liệu mong đợi mà không phát sinh ngoại lệ, đồng thời có kiểm thử hồi quy bao phủ các nhánh biến đổi buffer bị ảnh hưởng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100