WSGI servers and frameworks (P2)
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 WSGI: gunicorn, waitress, CherryPy's cheroot, Werkzeug's development server (flask run, app.run()), wsgiref — and every framework running on them (Flask, Django, Bottle, Pyramid, Falcon, web.py, CherryPy apps). It marks where each request starts and ends, so the processes, connections and files P1 reports are tied to the exact request on the exact keep-alive connection. It gets in with the same compose-only change as P1; nothing in the application changes.
Covers
| Versions | Server model | Kernel alone | |
|---|---|---|---|
gunicorn sync | 20–23 | process, one request per connection | exact (one client socket per worker) |
gunicorn gthread | 20–23 | thread pool, keep-alive | exact per thread; the hook adds keep-alive request boundaries |
gunicorn gevent / eventlet | 20–23 | greenlets | hook needed (with P5) |
| waitress | 2.x–3.x | thread pool fed by one async loop | hook needed (the loop thread reads all connections) |
| cheroot / CherryPy | cheroot 8–10, CherryPy 18 | thread pool, keep-alive | exact per thread; hook adds boundaries |
| Werkzeug dev server (Flask) | 2.x–3.x | thread per connection (HTTP/1.0 by default) | exact; hook adds boundaries |
wsgiref.simple_server (Bottle's default) | Python 3.8–3.14 | one request at a time | exact; hook adds boundaries |
| web.py's built-in server | 0.62 | cheroot | as cheroot |
| Frameworks | Flask, Django, Bottle, Pyramid, Falcon, web.py, CherryPy | — | nothing framework-specific needed |
Werkzeug and wsgiref are handled by P6's http.server hooks (both are built on http.server.BaseHTTPRequestHandler); they are listed here because that is where you would look.
Not covered:
- uWSGI: its HTTP handling is C and the WSGI callable is fetched from C; uWSGI's process and thread workers are linked exactly by the kernel (one request per worker thread at a time), so only the keep-alive request boundaries are missing. Not sampled.
- bjoern, meinheld: C servers on libev with no Python-visible connection object. meinheld is unmaintained and does not build on current Python; bjoern serves one request at a time on one thread, which the kernel links only while one connection is open. Not sampled.
- mod_wsgi (Apache): Apache's own worker processes/threads; the kernel links them per thread. Not sampled (needs an Apache image; mod_wsgi-express would work the same way).
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 (one change installs all of them):
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/p2/proposals/compose.toml) when it detects gunicorn, waitress-serve, flask run, manage.py runserver or cherryd in the command, or gunicorn, waitress, werkzeug, flask, django, bottle, pyramid, falcon, web.py, cherrypy, cheroot among the dependencies (requirements*.txt, pyproject.toml, lockfiles), or a wsgi.py / manage.py.
What it records
Request boundaries come from each server's own per-request code path. P2 never wraps the WSGI application or its response iterable: that would change how servers treat the result (wsgiref adds Content-Length to one-element lists, gunicorn uses sendfile for wsgi.file_wrapper, ...).
| Server | request_begin | request_end (status) | Request index |
|---|---|---|---|
| gunicorn (all workers) | gunicorn.http.wsgi.create (the request's socket) | when the worker's handle_request returns, after the response iterable was consumed and closed (Response.status_code) | Request.req_number - 1 (gunicorn's own keep-alive count) |
| waitress | waitress.task.Task.service starts (the channel's socket) | service returns (Task.status) | counted per HTTPChannel (requests of a channel are serviced in order) |
| cheroot / CherryPy / web.py | cheroot.server.HTTPRequest.respond (the connection's socket) | respond returns (HTTPRequest.status) | counted per HTTPConnection |
| Werkzeug, wsgiref | P6: a successful parse_request | handle_one_request / wsgiref's handle returns (send_response / ServerHandler.status) | counted per handler (one per connection) |
All with protocol http1, the accepted socket's local port and peer. Everything in between — streaming bodies included, since the iterable is consumed inside these calls — runs with the request's context id, so P1's spawned, connected and opened carry it.
| Event | Emitted from | Join key |
|---|---|---|
request_begin / request_end | the server hooks above | peer address and port, request index |
spawned, connected, opened, context_enter/exit | P1 | child pid, local port, pid + fd, thread |
Context following
Each request runs on one thread (or greenlet) from begin to end, and the context variable is set on it in between and reset afterwards, so nothing leaks into the next request a pool thread serves. Threads the application starts and ThreadPoolExecutor work carry the id (P1). For gunicorn's gevent/eventlet workers, greenlets spawned by the request carry it through P5.
Known gaps
- uWSGI, bjoern, meinheld, mod_wsgi: see Covers — kernel-exact per worker thread, no request boundaries.
- A request rejected before the server's per-request path (a malformed request line answered with 400 by the server itself) gets no
request_begin; cheroot and waitress then count one request fewer on that connection, which only matters if the client keeps using the connection after a 400 (servers close it). - Unix-socket listeners (gunicorn
-b unix:...behind nginx): requests are reported without an address; the proxy in front is linked by the kernel.
Safety
- Wrapped:
gunicorn.http.wsgi.create,SyncWorker/ThreadWorker/AsyncWorker.handle_request,waitress.task.Task.service,cheroot.server.HTTPRequest.respond(and P6'shttp.servermethods). Each calls the original first with the same arguments, returns its result unchanged and lets its exceptions through; the binding's work is guarded, counted, and switched off after 5 internal errors. - Per-connection counters are kept whenever the binding is loaded (one attribute increment per request), so the request index stays exact if the sensor attaches mid-connection; everything else waits for a tracer (
vbx_v1_enabled_mask()). VBXTRACE=0installs nothing.- 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?
grep vbxtrace /proc/<worker pid>/maps(the library is loaded in the master and inherited by forked workers).VBXTRACE_DEBUG=1printsinstalled fw_wsgiand any internal error. - Which links did a flow get? The hub shows each link's method; this package's are
hook:P2. - gunicorn with
--preloadworks the same (the hook is installed before the app is imported). - A custom gunicorn worker class that overrides
handle_requestwithout calling the base class still gets boundaries fromwsgi.create; the end is then taken at the next request on the thread.
Sample results
hooks/run-samples.sh python --package p2 — every sample runs the same scenario: /hello and a 404 on one connection, then /spawn, /file, /connect and a streamed body that opens a file between chunks on a second one (servers that close after each response get a connection per request).
| Sample | Server / framework | Python | events | chaos / baseline |
|---|---|---|---|---|
| gunicorn-sync | gunicorn sync + Flask | 3.12 (Debian) | 16 events match | byte-identical |
| gunicorn-gthread | gunicorn gthread + Flask | 3.9 | 16 match | byte-identical |
| waitress | waitress + Bottle | PyPy 3.10 | 16 match | byte-identical |
| werkzeug | Flask dev server | 3.13 Alpine (musl) | 16 match | byte-identical |
| wsgiref | Bottle on wsgiref | 3.8 | 16 match | byte-identical |
| cherrypy | CherryPy (cheroot) | 3.11 | 16 match | byte-identical |
| django | Django on gunicorn gthread | 3.12 | 16 match | byte-identical |
| falcon | Falcon on waitress | 3.10 | 16 match | byte-identical |
| pyramid | Pyramid on gunicorn sync | 3.14 | 16 match | byte-identical |
| webpy | web.py (cheroot) | 3.11 | 16 match | byte-identical |