asyncio.StreamReader raises BufferError with active memoryview
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
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
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece com asyncio.StreamReader e os caminhos de read, readline, readuntil e readexactly descritos no relatório; execute a reprodução fornecida para observar o BufferError com uma memoryview ativa. Considera-se concluído quando esses métodos retornarem os dados esperados sem gerar uma exceção, com cobertura de regressão para os caminhos afetados de mutação do buffer.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- backend
- Tipo de issue
- Bug
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 35/100