Skip to main content

SKY-Q401 · Quality, structure and performance

Async blocking call

High

The catalog gives no severity for this rule; the detector always reports HIGH.

A blocking call, such as requests.get(), time.sleep() or subprocess.run(), made directly inside an async def. An async server runs many requests on one thread, and a blocking call holds that thread until it returns.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
defect
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

While one request waits five seconds for a slow rates API, every other request on that event loop waits too, including health checks. Under load the service looks hung and gets restarted, although nothing crashed.

What Skylos flags

app/rates.py · PythonReported on line 13
"""Exchange-rate endpoint.""" import requestsfrom fastapi import FastAPI app = FastAPI()RATES_URL = "https://rates.example.com"  @app.get("/rates/{currency}")async def get_rate(currency: str):    url = f"{RATES_URL}/{currency}"    response = requests.get(url, timeout=5)    return response.json()

What Skylos leaves alone

app/rates.py · Python
"""Exchange-rate endpoint.""" import asyncio import requestsfrom fastapi import FastAPI app = FastAPI()RATES_URL = "https://rates.example.com"  @app.get("/rates/{currency}")async def get_rate(currency: str):    url = f"{RATES_URL}/{currency}"    response = await asyncio.to_thread(requests.get, url, timeout=5)    return response.json()

Why Skylos leaves it alone

requests.get is not called on the event loop here: it is handed to asyncio.to_thread(), which runs it on a worker thread while the loop keeps serving other requests.

How Skylos decides

  1. Runs with --quality on Python files. Looks at calls whose innermost enclosing function is an async def; a call inside a nested def or lambda is not counted, since that code may run elsewhere.
  2. Reports a call to time.sleep, requests.get, post, put, delete, patch, head or request, urllib.request.urlopen, subprocess.run, call, check_output or check_call, or os.system.
  3. Resolves import x as y and from x import f aliases in the file, so from time import sleep followed by sleep(1) is found. A name written module.func is matched even without an import.
  4. Reports HIGH on the line of the call; the message names the async replacement.

How to fix it

Use an async HTTP client and await it. (For a call that has no async version, await asyncio.to_thread(...) as in the example above.)

app/rates.py · Python · the fix, against the flagged file
"""Exchange-rate endpoint.""" Removed: import requestsAdded: import httpxfrom fastapi import FastAPI app = FastAPI()RATES_URL = "https://rates.example.com"  @app.get("/rates/{currency}")async def get_rate(currency: str):    url = f"{RATES_URL}/{currency}"Removed:     response = requests.get(url, timeout=5)Added:     async with httpx.AsyncClient(timeout=5) as client:Added:         response = await client.get(url)    return response.json()

When it can be wrong

  • A blocking call in an async command-line script that does nothing else at the same time is reported, although nobody waits on the loop.
  • It misses urllib.request.urlopen() written with its full module path, requests.Session().get(), blocking database drivers and file reads, which are not on the list.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the line of the call.

    Python
    time.sleep(0.1)  # skylos: ignore[SKY-Q401] startup only, before serving
  • [tool.skylos] ignore = ["SKY-Q401"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.