FastAPI Async Guard
Catch blocking calls and async/sync mistakes in FastAPI code, in the
editor, and learn why they freeze your server.
In FastAPI, async def endpoints run on the event loop, a single thread
shared by every request, while def endpoints run in a threadpool. One
time.sleep, requests.get or sync database query inside an async def
freezes the whole server until it returns. It's easy to miss: the code
works, tests pass, and production falls over under load.
@router.get("/users/{id}/profile")
async def profile(id: int):
return fetch_profile(id)
# ~~~~~~~~~~~~~ FAG004: `fetch_profile()` blocks the event loop inside
# async endpoint 'profile' (GET /users/{id}/profile):
# profile → fetch_profile → get_json → requests.get
Features
Six rules for the mistakes that actually happen:
| Rule |
Severity |
Detects |
| FAG001 |
Warning |
blocking call (time.sleep, requests, sync SQLAlchemy, file I/O, subprocess...) in async def |
| FAG002 |
Error |
coroutine called without await (data = request.json()) |
| FAG003 |
Warning |
async def endpoint that never awaits but blocks: should be def |
| FAG004 |
Warning |
blocking through your own helpers, across files, with the full call chain |
| FAG005 |
Information |
CPU-heavy work (bcrypt, Pillow, pandas) in async def |
| FAG006 |
Hint, off |
def endpoint that could be async def |
Hovers that explain the why, with a short example and, for FAG004,
links to every function in the chain.
Quick fixes where a safe one exists: await asyncio.sleep(...), add
await, wrap in await run_in_threadpool(...) (adding the import),
convert an endpoint between def and async def, or ignore one line.
Understands real code: import aliases, Annotated[Session, Depends()],
APIRouter prefixes, routers imported from other modules, re-exports in
__init__.py, methods, nested functions, recursion.
Fast: files are parsed once and cached; analysis is debounced while you type.
Nothing to install besides Python 3.10+: the analysis server's
dependencies are bundled, and your environment is never modified.
Installation
Search for FastAPI Async Guard in the Extensions view (Ctrl+Shift+X), or:
code --install-extension branly0.fastapi-async-guard
Editors based on Open VSX (VSCodium, Cursor, ...) get it from
open-vsx.org.
You can also download the .vsix from the
GitHub releases and use
Extensions → ⋯ → Install from VSIX….
Requirements
- VS Code 1.90+
- A Python 3.10+ interpreter. The extension uses the one selected in the
Python extension
if installed, otherwise
python3/python on PATH, or the
fastapiAsyncGuard.interpreter setting.
Settings
| Setting |
Default |
Description |
fastapiAsyncGuard.rules |
see below |
Severity per rule: error, warning, information, hint, off or default |
fastapiAsyncGuard.blockingFunctions |
[] |
Extra blocking functions, e.g. ["mylib.client.fetch", "mylib.Client.get"] |
fastapiAsyncGuard.asyncFunctions |
[] |
Extra coroutine functions for FAG002 |
fastapiAsyncGuard.cpuHeavyFunctions |
[] |
Extra CPU-heavy functions for FAG005 |
fastapiAsyncGuard.exclude |
[] |
Globs to skip: ["migrations", "tests/**", "**/*_pb2.py"] |
fastapiAsyncGuard.interpreter |
"" |
Python used to run the analysis server |
fastapiAsyncGuard.logLevel |
info |
Server log level (Output → FastAPI Async Guard) |
"fastapiAsyncGuard.rules": {
"FAG001": "warning", "FAG002": "error", "FAG003": "warning",
"FAG004": "warning", "FAG005": "information", "FAG006": "off"
}
Silence a single line with # noqa: FAG001.
How it works
A thin TypeScript client starts a Python language server (pygls) that parses
your code with Python's own ast module, resolves names across the
workspace, and follows calls through your helpers. Everything stays local.
Read docs/ARCHITECTURE.md for the full picture, and
docs/decisions/ for why it's built this way.
Static analysis has limits (dynamic calls, untyped injected objects...):
see docs/LIMITATIONS.md. The analyser prefers staying
silent to being wrong.
Contributing
Bug reports, false positives and new blocking APIs are very welcome. See
CONTRIBUTING.md and the
developer guide.
License
MIT