MagicStack / MagicStack/asyncpg

Application Flow Stuck Unless Connection is Explicitly Closed

Aperta
#1,216 0 commenti 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 version: 0.30.0
  • PostgreSQL version: 16
  • Do you use a PostgreSQL SaaS? If so, which?: Yes, I am using NeonDB
  • Can you reproduce the issue with a local PostgreSQL install?: Didn't attempt it.
  • Python version: 3.11.0
  • Platform: Windows
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?: [Your Cython version if applicable]
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : [Yes/No] I did not try uvloop

I'm encountering an issue where the flow of my application gets stuck unless the database connection is explicitly closed after a query, even though it should be managed automatically by the context manager.

Code Example:

async def read_record(self, request_details: dict):
    """
    Retrieve records based on conditions and request_type.
    
    Args:
        request_details (dict): The conditions including request_type.
    
    Returns:
        list: The records that match the conditions.
    """
    table = self.get_table_name(request_details['request_type'])
    conditions = {key: request_details[key] for key in request_details if key != 'request_type'}
    condition_str = " AND ".join(f"{key} = ${i+1}" for i, key in enumerate(conditions.keys()))
    query = f"SELECT * FROM {table} WHERE {condition_str};"

    async with self.get_connection() as conn:
        conn.add_log_listener(lambda connection, message: logger.info("PostgreSQL log message: %s", message))
        result = await conn.fetch(query, *conditions.values())
        logger.info("Fetched records: %s", result)
        # await conn.close() # get's stuck without this line

    return result

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 con l’esempio asyncpg 0.30.0 fornito e riproducilo su Python 3.11, PostgreSQL 16 e Windows, verificando innanzitutto se il problema si verifica con un’installazione locale di PostgreSQL. Analizza il comportamento del context manager asincrono intorno a self.get_connection() e confrontalo con il caso con conn.close() esplicito; il lavoro è completo quando il flusso termina senza richiedere quella chiusura esplicita.

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

Valutazione

Stack tecnologico
postgresql, python
Ambito
backend, databases
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.