Skip to content

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
uvicorn0.20+ (h11 and httptools; websockets via websockets, wsproto and the sans-io implementation; asyncio and uvloop loops)
hypercorn0.14+ (asyncio and trio workers; HTTP/1.1, HTTP/2, websockets)
daphne4.x (Twisted on the asyncio reactor)
granian2.x (tested 2.5.5), ASGI interface, asyncio task implementation (request index unknown, see below)
FrameworksFastAPI, Starlette, Django ASGI / Channels, Quart, Litestar, BlackSheep, Sanic (ASGI mode) — nothing framework-specific needed
Python3.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:

diff
 services:
   <service>:
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+    environment:
+      PYTHONPATH: /opt/vbxtrace/python

Append 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:

  1. Which connection a request's task serves, set by a hook in each server:

    ServerHookConnection 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 / trioTCPServer.run (the connection's task; request tasks are its children)the TCPServer
    daphneServer.create_application (the application task is created inside it)the Twisted transport
    graniangranian.asgi._callback_wrapper (wraps the app before granian's server modules import it)none: index VBX_REQUEST_INDEX_UNKNOWN, endpoints from the scope
  2. An 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, hypercorn app_wrappers.ASGIWrapper.__call__, daphne Server.__init__. For an http or websocket scope on a known connection it emits:

    EventWhenValues
    request_beginthe app is calledaccepted socket's local port and peer; index = next request on that connection; protocol http1, http2 (scope["http_version"]) or websocket
    request_endthe last body chunk (more_body false) 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 before

    send is passed through untouched (the middleware only looks at the message type and status); receive is 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, httpx async, asyncpg) are not reported. Blocking socket connects, processes and files are. The uvicorn-uvloop sample 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.load and the run_asgi methods, hypercorn ASGIWrapper.__call__ and TCPServer.run, daphne Server.__init__ / create_application, granian asgi._callback_wrapper. Each calls the original first with the same arguments and returns / raises what it does; the middleware awaits the application with the same scope and receive and a send that forwards every message unchanged and returns its result.
  • Errors in the binding's own work are counted; 5 switch it off. VBXTRACE=0 installs 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.so lists them; sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*' too.
  • Is the hook loaded? VBXTRACE_DEBUG=1 prints installed 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.load runs 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.

SampleServer / frameworkPythoneventschaos / baseline
uvicorn-fastapiuvicorn (h11, websockets) + FastAPI3.1122 matchbyte-identical
uvicorn-uvloopuvicorn (uvloop, httptools, wsproto) + Starlette3.822 matchbyte-identical
uvicorn-alpineuvicorn + FastAPI3.12 Alpine (musl)22 matchbyte-identical
hypercornhypercorn asyncio + Starlette3.1222 matchbyte-identical
hypercorn-triohypercorn trio + Starlette3.1322 matchbyte-identical
daphnedaphne + Starlette3.1022 matchbyte-identical
django-asgiDjango ASGI on uvicorn3.1257 recorded, 17 compared (context_enter/exit of asgiref's pool ignored: racy order)byte-identical
quartQuart on hypercorn3.1224 matchbyte-identical
litestarLitestar on uvicorn (sync stream steps and background task in its thread pool)3.1230 matchbyte-identical
blacksheepBlackSheep on uvicorn3.1119 matchbyte-identical
sanic-asgiSanic (ASGI) on uvicorn3.1219 matchbyte-identical
granianStarlette on granian (request_index=unknown)3.1222 matchbyte-identical