MagicStack / MagicStack/asyncpg

asyncpg __del__ methods call asyncio APIs directly, which isn't guaranteed to work and indeed, doesn't work

Aperta
#376 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
8.1k
Fork
468
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

asyncpg has several `__del__` methods that call into asyncio APIs. But... you can't safely call into asyncio APIs from a `__del__` method, because `__del__` methods can be run in arbitrary threads, or at arbitrary moments when the loop's internal data structures are in an inconsistent state.

Discovered here: https://github.com/python-trio/trio-asyncio/issues/44

Looking at the traceback in that issue, I suspect that the reason this hasn't been noticed before is that the `__del__` method ultimately ends up calling `loop.call_soon(...)`. And if you do that from another thread, then with regular asyncio, things will mostly seem to work – it won't actually wake up the loop the way `call_soon_threadsafe` would, but if the loop is still running then it will eventually get run, and the default `call_soon` will not explode or otherwise notice if it's called from the wrong thread. But in that issue, someone's using asyncpg with an alternative asyncio loop that fails when `call_soon` is called from the wrong thread, and this breaks asyncpg.

I guess asyncpg should go through all its `__del__` methods and wrap them in a `loop.call_soon_threadsafe`?

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

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 facendo l’inventario dei metodi __del__ di asyncpg e dei percorsi loop.call_soon coinvolti dalla issue collegata di Trio. Verifica ogni percorso di pulizia rispetto ai problemi di thread safety e di stato del loop descritti nella issue. Il lavoro è completato quando il comportamento di pulizia interessato funziona con implementazioni alternative del loop asyncio senza chiamate dirette non sicure.

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

Valutazione

Stack tecnologico
postgresql, python
Ambito
databases
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 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.