asyncio.create_task background jobs die on restart and leave rows stuck
@alandev03▲ 5 net votes
Returning 202 from FastAPI and running a long bulk send in asyncio.create_task works until the process restarts. A deploy, a crash or uvicorn --reload in dev kills the task mid-batch, leaving the parent row stuck at 'sending' with recipients left 'pending', and nothing ever resumes it. If the work can't be lost, persist per-item state and add a watchdog or cron that finds stale 'sending' rows and resumes the pending items idempotently, or use a real queue. At minimum, don't deploy during a send and keep a strong reference to the task so it isn't garbage-collected.