Fix the blocking call audit for an async handler

Review the AI's codeEasy10 min

Your task

An AI built a FastAPI async def upload handler that reads, hashes and stores avatars. Throughput is low and the event loop lags, because sync calls inside an async handler block the one thread that serves every request. Your team audits each call in such a handler and labels what it needs.

An assistant wrote classify_async_calls(calls). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • calls: the calls in the handler, in order, as written, such as time.sleep or await file.read.
  • Return one verdict per call, in the same order: "ok", "missing_await", "use_await" or "use_threadpool".

Rules the tests check:

  • Trim whitespace from both ends of each call first. Names then match exactly, case included.
  • The verdict is the first of these that applies:
    1. "ok" when the call starts with await (with the space). It is already awaited.
    2. "missing_await" when the call is one of asyncio.sleep, file.read, run_in_threadpool and db.fetch. These are coroutines, which do nothing unless awaited.
    3. "use_await" when the call is one of time.sleep, file.file.read, requests.get and requests.post. Each has an async version to await instead.
    4. "use_threadpool" when the call is one of hashlib.sha256, hashlib.md5, open, f.write, bcrypt.hashpw and PIL.Image.open. These have no async version, so they belong on a worker thread.
    5. "ok" for anything else, including an empty entry.

Examples

classify_async_calls(calls: list[str]) -> list[str]
Example 1
Inputclassify_async_calls(["file.file.read", "hashlib.sha256", "time.sleep", "open", "f.write"])
Output["use_await", "use_threadpool", "use_await", "use_threadpool", "use_threadpool"]
Example 2
Inputclassify_async_calls(["await file.read", "await asyncio.sleep", "return digest"])
Output["ok", "ok", "ok"]
Example 3
Inputclassify_async_calls([])
Output[]