python / python/cpython

asyncio.StreamReader raises BufferError with active memoryview

Aperta
#146,379 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-asyncio type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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
  • read
  • readline
  • readuntil
  • readexactly
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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con asyncio.StreamReader e i percorsi read, readline, readuntil e readexactly descritti nel report; esegui la riproduzione fornita per osservare il BufferError con una memoryview attiva. Il lavoro è completato quando questi metodi restituiscono i dati previsti senza sollevare eccezioni, con una copertura di regressione per i percorsi interessati di mutazione del buffer.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.