Skip to content

Debugging · Async completion

Why is it ready before the files are?

Illustrative walkthrough written for this guide. This is an authored example, not a recorded AI grading result.

Scenario and requirements

Debug this async worker function. save_report(report_id) is a coroutine that finishes when the report file has been stored and raises an exception if generation or storage fails.

Key requirements

Store every file before returning ready, and pass any raised exception to the caller. Running the work concurrently is optional.

async def generate_export(report_ids):
    for report_id in report_ids:
        asyncio.create_task(save_report(report_id))
    return {"status": "ready"}

Public requirements

  • Each call generates one to five independent files. Return ready only after every file has been stored.
  • If any file operation raises an exception, propagate it to the caller.
  • Running the work concurrently is optional. Explain the cause, a fix, and how you would verify it.
  • Canceling other operations, rolling back stored files, and automatic retries are outside this example's requirements.

Initial answer

The cause is that create_task starts the work but nothing waits for it. I would add await asyncio.sleep(0) before returning.

Feedback and evidence

You found the cause correctly. The proposed fix still does not guarantee that file storage finishes before the function returns.

From the answercreate_task starts the work but nothing waits for it
Why it mattersCorrect: the outer function does not wait for the file work.
From the answerawait asyncio.sleep(0)
Why it mattersSleeping hands control to the event loop once. It does not wait for the stored files.
From the answerI would add await asyncio.sleep(0) before returning.
Why it mattersThe answer ends with a change but no verification plan. Specify how to check pending storage, successful completion, and failure propagation.

Improved answer

Await every file operation before returning ready.

await asyncio.gather(*(save_report(report_id) for report_id in report_ids))
return {"status": "ready"}

Let an exception from a file operation propagate to the caller instead of turning it into a success response.

Alternatives and boundaries

Different implementations can be valid when they meet the same public requirements.

Does the fix have to use asyncio.gather?

No. A sequential for loop that awaits each call also meets these requirements. Running the work concurrently and a particular syntax are not required.

asyncio.gather passes the first exception to the caller; by default it does not cancel the other operations. Cancellation, rollback, and retry behavior are not promised here.

Proposed checks

These are proposed checks for this example, not reported test results.

  • Keep one storage call pending and verify that generate_export is still pending, too.
  • Let every storage call finish and verify that the function returns ready.
  • Make one storage call raise and verify that the caller receives the exception.

Put your reasoning into practice

What would you change, and why?

Choose a scenario and explain your own decision. Guest practice offers a selection of scenarios, subject to a daily limit. Sign in for detailed feedback and saved history.