Menu

1 / 3

asyncio Holds Only a Weak Reference to Your Task. Fire-and-Forget Can Vanish Mid-Await

DEV Community: python·Gabriel Anhaia·about 18 hours ago
#X0uiBdr4
#devtopython#devto#dev#task#asyncio
Long article (2217 words) — no AI summary yet.
Reading 0:00
15s threshold

Book: Hexagonal Architecture in Python

Companion code: hexagonal-python-examples — every listing from the book, free on GitHub

My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools

Me: xgabriel.com | GitHub

You write asyncio.create_task(send_webhook(order)) in a request handler, return the response, and move on. Most days the webhook goes out. Some days it doesn't, and the only trace is one log line: Task was destroyed but it is pending!

That is documented behavior. The asyncio docs for create_task carry an "Important" box: "Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn't referenced elsewhere may get garbage collected at any time, even before it's done."

The box came out of bpo-44665 , filed in 2021.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.