ASGI servers and frameworks (P3)
Ready
The hub proposes this hook for the containers it fits, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.
For Python services served over ASGI: uvicorn, hypercorn, daphne and granian, and every framework on them — FastAPI, Starlette, Django (ASGI) and Channels, Quart, Litestar, BlackSheep, Sanic in ASGI mode. All of them serve many requests on one event-loop thread, so the kernel alone cannot tell which request a child process, connection or file belongs to; P3 marks each HTTP request and websocket, and P1 reports what happens inside. It gets in with the same compose-only change as P1.
Covers
| Versions | |
|---|---|
| uvicorn | 0.20+ (h11 and httptools; websockets via websockets, wsproto and the sans-io implementation; asyncio and uvloop loops) |
| hypercorn | 0.14+ (asyncio and trio workers; HTTP/1.1, HTTP/2, websockets) |
| daphne | 4.x (Twisted on the asyncio reactor) |
| granian | 2.x (tested 2.5.5), ASGI interface, asyncio task implementation (request index unknown, see below) |
| Frameworks | FastAPI, Starlette, Django ASGI / Channels, Quart, Litestar, BlackSheep, Sanic (ASGI mode) — nothing framework-specific needed |
| Python | 3.8–3.14 |
granian (Rust server) is covered with one limit: the Python application never sees the connection, so the request's number on a keep-alive connection cannot be known. Its requests are reported with request_index = VBX_REQUEST_INDEX_UNKNOWN (the sensor then links them only through other exact facts, never by guessing) and the endpoints from scope["server"] / scope["client"]. This needs granian's asyncio task implementation (the default; the opt-in rust one on Python < 3.12 runs every request's coroutine in one shared context, so nothing is reported under it).
How it gets in
From a registry (local development, or a build with network; the same release as the hook directory): pip install vbxtrace==1.0.0, then python -m vbxtrace app.py or import vbxtrace.auto first (no sitecustomize needed); see Local development.
Compose-only, identical for every Python package:
services:
<service>:
+ volumes:
+ - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+ environment:
+ PYTHONPATH: /opt/vbxtrace/pythonAppend with : to an existing PYTHONPATH. The hub proposes it (hooks/python/p3/proposals/compose.toml) when it detects uvicorn, hypercorn, daphne, granian, fastapi run|dev or uvicorn.workers.UvicornWorker in the command, the tiangolo/uvicorn-gunicorn* images, or uvicorn, hypercorn, daphne, granian, fastapi, starlette, quart, litestar, blacksheep, channels among the dependencies.
What it records
Two parts:
Which connection a request's task serves, set by a hook in each server:
Server Hook Connection object (request index counted on it) uvicorn HTTP (h11, httptools) RequestResponseCycle.run_asgi(runs in the request's own task)the transport uvicorn websockets (all three) the websocket protocol's run_asgithe transport (so an upgrade after keep-alive requests gets the next index) hypercorn asyncio / trio TCPServer.run(the connection's task; request tasks are its children)the TCPServerdaphne Server.create_application(the application task is created inside it)the Twisted transport granian granian.asgi._callback_wrapper(wraps the app before granian's server modules import it)none: index VBX_REQUEST_INDEX_UNKNOWN, endpoints from the scopeAn ASGI middleware at the outermost layer of the application (outside uvicorn's proxy-headers middleware, so it sees the real peer), installed where each server gets its app: uvicorn
Config.load, hypercornapp_wrappers.ASGIWrapper.__call__, daphneServer.__init__. For anhttporwebsocketscope on a known connection it emits:Event When Values request_beginthe app is called accepted socket's local port and peer; index = next request on that connection; protocol http1,http2(scope["http_version"]) orwebsocketrequest_endthe last body chunk ( more_bodyfalse) has been handed to the server; websockets and body-less responses: when the app returnsthe status from http.response.start; websocket: 101 once accepted, 403 if closed beforesendis passed through untouched (the middleware only looks at the message type and status);receiveis not wrapped. Lifespan scopes are passed straight through.
Inside, P1 reports spawned, connected, opened, and context_enter/exit for thread-pool work (Starlette/FastAPI run sync endpoints and sync background tasks through anyio's worker threads, which copy the context; Django's asgiref and Quart use run_in_executor).
Context following
The request's id is set in the request's own task (uvicorn creates one per request; hypercorn and daphne too), so it never leaks into another request. It stays set until the application returns: background tasks that run after the response (Starlette/FastAPI BackgroundTasks, Litestar background=, Quart add_background_task) still carry the id, after request_end — they are that request's work. Tasks the request creates (asyncio.create_task) copy the id and keep it for as long as they run.
Known gaps
- uvloop: P1's non-blocking connect hook is on asyncio's selector loop; with uvloop (
uvicorn[standard]'s default), connects made through the loop (asyncio.open_connection,httpxasync,asyncpg) are not reported. Blockingsocketconnects, processes and files are. Theuvicorn-uvloopsample checks the blocking path. - granian: request index unknown (see above); its WSGI and RSGI interfaces are not hooked.
- HTTP/2: the index is the order in which streams reach the application, which is stream-id order for hypercorn; not sampled (the driver speaks HTTP/1.1).
- Requests a server answers itself without calling the app (400 for a malformed request, uvicorn's 503 when over
--limit-concurrency) get no event and are not counted.
Safety
- Wrapped: uvicorn
Config.loadand therun_asgimethods, hypercornASGIWrapper.__call__andTCPServer.run, daphneServer.__init__/create_application, granianasgi._callback_wrapper. Each calls the original first with the same arguments and returns / raises what it does; the middleware awaits the application with the samescopeandreceiveand asendthat forwards every message unchanged and returns its result. - Errors in the binding's own work are counted; 5 switch it off.
VBXTRACE=0installs nothing. Without a tracer, the middleware checks one flag and calls the app directly. - Chaos runs (every event call failing) give byte-identical responses on every sample.
- Overhead has not been measured yet; it will be measured on the sample services in the dedicated performance phase.
Turning it off
Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook; vulnboxd's rollback covers a hook that breaks a service. Removing the PYTHONPATH entry and the mount removes all Python packages at once.
Troubleshooting
- Are the probes there?
readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.solists them;sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*'too. - Is the hook loaded?
VBXTRACE_DEBUG=1printsinstalled fw_asgi;grep vbxtrace /proc/<pid>/maps. - Which links did a flow get? The hub shows each link's method; this package's are
hook:P3. - uvicorn started from Python (
uvicorn.run(app)) is the same as the CLI:Config.loadruns either way. - gunicorn with
uvicorn.workers.UvicornWorker: the worker runs uvicorn's server, so P3 applies (P2's gunicorn hooks do not fire for it).
Sample results
hooks/run-samples.sh python --package p3 — the P2 scenario (keep-alive pair, spawn, file, connect, streamed body) plus a background task after the response and a websocket that spawns a process.
| Sample | Server / framework | Python | events | chaos / baseline |
|---|---|---|---|---|
| uvicorn-fastapi | uvicorn (h11, websockets) + FastAPI | 3.11 | 22 match | byte-identical |
| uvicorn-uvloop | uvicorn (uvloop, httptools, wsproto) + Starlette | 3.8 | 22 match | byte-identical |
| uvicorn-alpine | uvicorn + FastAPI | 3.12 Alpine (musl) | 22 match | byte-identical |
| hypercorn | hypercorn asyncio + Starlette | 3.12 | 22 match | byte-identical |
| hypercorn-trio | hypercorn trio + Starlette | 3.13 | 22 match | byte-identical |
| daphne | daphne + Starlette | 3.10 | 22 match | byte-identical |
| django-asgi | Django ASGI on uvicorn | 3.12 | 57 recorded, 17 compared (context_enter/exit of asgiref's pool ignored: racy order) | byte-identical |
| quart | Quart on hypercorn | 3.12 | 24 match | byte-identical |
| litestar | Litestar on uvicorn (sync stream steps and background task in its thread pool) | 3.12 | 30 match | byte-identical |
| blacksheep | BlackSheep on uvicorn | 3.11 | 19 match | byte-identical |
| sanic-asgi | Sanic (ASGI) on uvicorn | 3.12 | 19 match | byte-identical |
| granian | Starlette on granian (request_index=unknown) | 3.12 | 22 match | byte-identical |