Digiaru

Digiaru started this conversation 1 week ago.

Asyncio pitfalls: orphan tasks, CancelledError, and subtle bugs

When I use asyncio.create_task() or async generators, I often get silent bugs—tasks never run, exceptions go unnoticed, or cancellations don't behave as expected. Why does asyncio misbehave this way?

Kar

Posted 1 week ago

Key pitfalls in Python's asyncio include: • Unretrieved task exceptions: If tasks created via create_task() are never awaited or collected, exceptions are suppressed, leaving silent failures Reddit+3Reddit+3Reddit+3. • CancelledError handling: If you catch generic Exception blocks without re raising CancelledError, tasks (or interrupts like KeyboardInterrupt) may not properly terminate Reddit+2Reddit+2Reddit+2. • Improper use of asyncio.run() in nested environments leads to conflicts (especially in Jupyter), requiring workarounds like nest_asyncio or manual loop management Reddit. 🛠️ How to Fix • Save references to created tasks, and either await them later or explicitly check .exception() to handle failures. • Catch CancelledError separately, e.g.: python Copy code try: await task() except asyncio.CancelledError: raise except Exception as e: handle(e) • Use asyncio.run() only once at top-level; inside Jupyter or complex apps, avoid it and instead await coroutines directly or manage the loop yourself. • Consider using structured concurrency libraries (e.g. Trio) for more predictable async behavior Reddit.


🧷 Suggested Tags for Posting • Issue 1: python, memory-leaks, closures, mutable-default-argument, weakref • Issue 2: python, subprocess, memory-leak, Windows • Issue 3: python, asyncio, create_task, CancelledError, asyncio-bugs