python / python/cpython

asyncio subprocess spawn does not record the awaiting task

Aperta
#157,259 2 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:

The internal subprocess pipe-connect task registers no awaited_by edge

Repro:

import asyncio, sys

async def main():
    spawn = asyncio.create_task(
        asyncio.create_subprocess_exec(sys.executable, '-c', 'pass',
                                       stdin=asyncio.subprocess.PIPE),
        name='spawn')

    await asyncio.sleep(0)
    await asyncio.sleep(0)

    for t in asyncio.all_tasks() - {asyncio.current_task()}:
        asyncio.print_call_graph(t)

    proc = await spawn
    await proc.wait()

asyncio.run(main())

Actual:

* Task(name='spawn', id=0x103e00b90)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/unix_events.py', line 211, in async _UnixSelectorEventLoop._make_subprocess_transport()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1839, in async BaseEventLoop.subprocess_exec()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/subprocess.py', line 249, in async create_subprocess_exec()
* Task(name='Task-2', id=0x103b7d0a0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1753, in async BaseEventLoop.connect_write_pipe()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_subprocess.py', line 188, in async BaseSubprocessTransport._connect_pipes()

Expected:

* Task(name='spawn', id=0x105620b90)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/unix_events.py', line 211, in async _UnixSelectorEventLoop._make_subprocess_transport()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1839, in async BaseEventLoop.subprocess_exec()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/subprocess.py', line 249, in async create_subprocess_exec()
* Task(name='Task-2', id=0x10539d0a0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1753, in async BaseEventLoop.connect_write_pipe()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_subprocess.py', line 194, in async BaseSubprocessTransport._connect_pipes()
  + Awaited by:
    * Task(name='spawn', id=0x105620b90)
      + Call stack:
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/unix_events.py', line 211, in async _UnixSelectorEventLoop._make_subprocess_transport()
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1839, in async BaseEventLoop.subprocess_exec()
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/subprocess.py', line 249, in async create_subprocess_exec()

Proposed fix: register the edge in BaseSubprocessTransport.__init__ and discard it in doe callback

I have a fix ready for that

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

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 in Lib/asyncio/base_subprocess.py, all’interno di BaseSubprocessTransport.init, e analizza il percorso di connessione della pipe del sottoprocesso mostrato nel traceback. Esegui la riproduzione asyncio fornita e verifica che l’attività interna di connessione della pipe segnali un arco “Awaited by” per l’attività di spawn, quindi verifica che l’arco venga scartato dal callback di completamento.

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

Valutazione

Stack tecnologico
python
Ambito
backend, operating-systems
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.