Other asyncio frameworks and raw asyncio (P4)
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 that run their own event-loop server instead of WSGI/ASGI: aiohttp, Sanic's own server, Tornado, Twisted (twisted.web and raw protocols), and — very common in attack/defense services — raw asyncio servers (asyncio.start_server line protocols, asyncio.Protocol classes), trio and anyio servers. One thread serves every connection, so the kernel cannot tell which connection a child process, outgoing connection or file belongs to; P4 marks requests (HTTP frameworks) or sessions (raw servers), and P1 reports what happens inside. It gets in with the same compose-only change as P1.
Covers
| Versions | Marks | |
|---|---|---|
| aiohttp (server) | 3.8+ | HTTP requests and websockets |
| Sanic (own server) | 22+ | HTTP requests and websockets (ASGI mode: P3) |
Tornado (tornado.web) | 6.x | HTTP requests and websockets |
Twisted twisted.web | 20+ | HTTP requests (also deferred responses) |
Twisted protocols (tcp.Server) | 20+ | raw sessions, one per connection |
asyncio.start_server | Python 3.8–3.14, also on uvloop | raw sessions |
loop.create_server (Protocol classes) | selector event loop | raw sessions |
trio.serve_tcp / serve_listeners | trio 0.20+ | raw sessions |
anyio listeners (Listener.serve) | anyio 3–4 | raw sessions |
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/p4/proposals/compose.toml) when it detects aiohttp, sanic, tornado, twisted, trio or anyio among the dependencies, or twistd / sanic in the command. Raw asyncio services without dependencies are covered by P1's proposal (any Python service) — P4's modules are installed by the same change.
What it records
HTTP frameworks — exact request boundaries:
| Framework | request_begin | request_end (status) | Request index counted on |
|---|---|---|---|
| aiohttp | web_protocol.RequestHandler._handle_request | RequestHandler.finish_response returns (resp.status) | the connection's start() task (aiohttp may run each request in a task of its own; they share the connection's counter) |
| Sanic | Sanic.handle_request on the native HTTP/1.1 server | the next Http.init_for_request, or Http.http1 returning (response status; websocket 101) | the connection task |
| Tornado | web.RequestHandler._execute (the request's own task) | _execute returns (get_status()) | the connection's IOStream |
| twisted.web | http.Request.process / server.Request.process | Request.finish (Request.code) — also when it is called later for a NOT_DONE_YET response | the HTTPChannel |
Protocol websocket for requests with Upgrade: websocket, otherwise http1.
Raw servers — a lazy session per connection (protocol raw, index 0, status 0): nothing is emitted until the handler spawns a process, connects or opens a file; then request_begin is emitted first, and request_end when the connection ends. A connection that does nothing hooked emits nothing. An HTTP framework running on top of the same server (aiohttp, uvicorn and hypercorn sit on loop.create_server/start_server, daphne on Twisted) marks its own requests, which drops the session silently — no double counting.
| Server | Hook |
|---|---|
asyncio.start_server | the client_connected_cb: a task per connection (works on uvloop too, since it wraps the callback) |
loop.create_server | the protocol factory: every callback of each protocol instance (connection_made, data_received, eof_received, connection_lost, ...) runs with its connection's session — asyncio would otherwise run each callback in a different copied context |
| trio | trio._highlevel_serve_listeners._run_handler |
| anyio | anyio.abc.SocketListener.serve's handler |
| Twisted | twisted.internet.tcp.Server._dataReceived / connectionLost |
Inside requests and sessions, P1 reports spawned, connected, opened.
Context following
- asyncio, trio, anyio: tasks copy the context they are created in, so a task a request (or session) starts carries its id for as long as it runs, also after the response (the aiohttp and Tornado samples check such a task).
- Twisted runs on its reactor without tasks: P4 sets the id for the duration of
Request.processand of eachdataReceivedcall. Work deferred to Twisted's thread pool (deferToThread,callInThread) carries the id into the pool thread betweencontext_enterandcontext_exit. Callbacks fired later from the reactor (aDeferredresolved by another event) run without the id; the request still ends correctly atfinish().
Known gaps
loop.create_serveron uvloop (C) is not hooked (start_serveris); Protocol classes with__slots__and no__dict__cannot have their callbacks rebound and are skipped.- Twisted: reactor callbacks outside
process/dataReceived(timers, deferreds fired by other connections) carry no id. Twisted'sinlineCallbackscode after anawait/yieldon another event likewise. - aiohttp older than 3.8 (different internals): no request boundaries.
- Tornado's bare
HTTPServer(callback)withouttornado.webis not hooked (onlyRequestHandler). - UDP servers (
create_datagram_endpoint) are not sessions.
Safety
- Wrapped: aiohttp
RequestHandler.start/_handle_request/finish_response; SanicSanic.handle_request,Http.init_for_request/http1, andTouchUp.run(Sanic recompileshandle_requestfrom source at startup through__wrapped__, which silently drops a wrapper; P4 wraps the recompiled method again); TornadoRequestHandler._execute; TwistedRequest.process/finish,tcp.Server._dataReceived/connectionLost,ThreadPool.callInThreadWithCallback;asyncio.start_server,BaseEventLoop.create_server; trio_run_handler; anyioSocketListener.serve. - Every wrapper calls the original first with the same arguments and returns / raises what it does; the binding's work is guarded, counted, and switched off after 5 internal errors.
VBXTRACE=0installs nothing. Without a tracer, sessions are not even opened. - 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_aio;grep vbxtrace /proc/<pid>/maps. - Which links did a flow get? The hub shows each link's method; this package's are
hook:P4. - A raw service shows no sessions: sessions only appear when the connection spawns, connects or opens a file — a connection that only computes and answers has nothing to link.
Sample results
hooks/run-samples.sh python --package p4.
| Sample | What | Python | events | chaos / baseline |
|---|---|---|---|---|
| aiohttp | keep-alive, streaming, a task outliving the response, websocket | 3.12 | 22 match | byte-identical |
| tornado | the same on tornado.web | 3.11 | 22 match | byte-identical |
| sanic | Sanic's own server, websocket | 3.12 | 19 match | byte-identical |
| twisted | twisted.web incl. deferToThread + a raw protocol | 3.10 | 21 match | byte-identical |
| asyncio-raw | start_server line protocol + a Protocol class | 3.13 Alpine (musl) | 8 match | byte-identical |
| trio | trio.serve_tcp + anyio listener | 3.12 | 6 match | byte-identical |