Skip to content

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 ​

VersionsMarks
aiohttp (server)3.8+HTTP requests and websockets
Sanic (own server)22+HTTP requests and websockets (ASGI mode: P3)
Tornado (tornado.web)6.xHTTP requests and websockets
Twisted twisted.web20+HTTP requests (also deferred responses)
Twisted protocols (tcp.Server)20+raw sessions, one per connection
asyncio.start_serverPython 3.8–3.14, also on uvloopraw sessions
loop.create_server (Protocol classes)selector event loopraw sessions
trio.serve_tcp / serve_listenerstrio 0.20+raw sessions
anyio listeners (Listener.serve)anyio 3–4raw 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:

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

Frameworkrequest_beginrequest_end (status)Request index counted on
aiohttpweb_protocol.RequestHandler._handle_requestRequestHandler.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)
SanicSanic.handle_request on the native HTTP/1.1 serverthe next Http.init_for_request, or Http.http1 returning (response status; websocket 101)the connection task
Tornadoweb.RequestHandler._execute (the request's own task)_execute returns (get_status())the connection's IOStream
twisted.webhttp.Request.process / server.Request.processRequest.finish (Request.code) — also when it is called later for a NOT_DONE_YET responsethe 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.

ServerHook
asyncio.start_serverthe client_connected_cb: a task per connection (works on uvloop too, since it wraps the callback)
loop.create_serverthe 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
triotrio._highlevel_serve_listeners._run_handler
anyioanyio.abc.SocketListener.serve's handler
Twistedtwisted.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.process and of each dataReceived call. Work deferred to Twisted's thread pool (deferToThread, callInThread) carries the id into the pool thread between context_enter and context_exit. Callbacks fired later from the reactor (a Deferred resolved by another event) run without the id; the request still ends correctly at finish().

Known gaps ​

  • loop.create_server on uvloop (C) is not hooked (start_server is); 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's inlineCallbacks code after an await/yield on another event likewise.
  • aiohttp older than 3.8 (different internals): no request boundaries.
  • Tornado's bare HTTPServer(callback) without tornado.web is not hooked (only RequestHandler).
  • UDP servers (create_datagram_endpoint) are not sessions.

Safety ​

  • Wrapped: aiohttp RequestHandler.start / _handle_request / finish_response; Sanic Sanic.handle_request, Http.init_for_request / http1, and TouchUp.run (Sanic recompiles handle_request from source at startup through __wrapped__, which silently drops a wrapper; P4 wraps the recompiled method again); Tornado RequestHandler._execute; Twisted Request.process / finish, tcp.Server._dataReceived / connectionLost, ThreadPool.callInThreadWithCallback; asyncio.start_server, BaseEventLoop.create_server; trio _run_handler; anyio SocketListener.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=0 installs 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.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_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.

SampleWhatPythoneventschaos / baseline
aiohttpkeep-alive, streaming, a task outliving the response, websocket3.1222 matchbyte-identical
tornadothe same on tornado.web3.1122 matchbyte-identical
sanicSanic's own server, websocket3.1219 matchbyte-identical
twistedtwisted.web incl. deferToThread + a raw protocol3.1021 matchbyte-identical
asyncio-rawstart_server line protocol + a Protocol class3.13 Alpine (musl)8 matchbyte-identical
triotrio.serve_tcp + anyio listener3.126 matchbyte-identical