Skip to content

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 ​

VersionsServer modelKernel alone
gunicorn sync20–23process, one request per connectionexact (one client socket per worker)
gunicorn gthread20–23thread pool, keep-aliveexact per thread; the hook adds keep-alive request boundaries
gunicorn gevent / eventlet20–23greenletshook needed (with P5)
waitress2.x–3.xthread pool fed by one async loophook needed (the loop thread reads all connections)
cheroot / CherryPycheroot 8–10, CherryPy 18thread pool, keep-aliveexact per thread; hook adds boundaries
Werkzeug dev server (Flask)2.x–3.xthread per connection (HTTP/1.0 by default)exact; hook adds boundaries
wsgiref.simple_server (Bottle's default)Python 3.8–3.14one request at a timeexact; hook adds boundaries
web.py's built-in server0.62cherootas cheroot
FrameworksFlask, 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):

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/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, ...).

Serverrequest_beginrequest_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)
waitresswaitress.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.pycheroot.server.HTTPRequest.respond (the connection's socket)respond returns (HTTPRequest.status)counted per HTTPConnection
Werkzeug, wsgirefP6: a successful parse_requesthandle_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.

EventEmitted fromJoin key
request_begin / request_endthe server hooks abovepeer address and port, request index
spawned, connected, opened, context_enter/exitP1child 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's http.server methods). 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=0 installs 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.so lists 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=1 prints installed fw_wsgi and any internal error.
  • Which links did a flow get? The hub shows each link's method; this package's are hook:P2.
  • gunicorn with --preload works the same (the hook is installed before the app is imported).
  • A custom gunicorn worker class that overrides handle_request without calling the base class still gets boundaries from wsgi.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).

SampleServer / frameworkPythoneventschaos / baseline
gunicorn-syncgunicorn sync + Flask3.12 (Debian)16 events matchbyte-identical
gunicorn-gthreadgunicorn gthread + Flask3.916 matchbyte-identical
waitresswaitress + BottlePyPy 3.1016 matchbyte-identical
werkzeugFlask dev server3.13 Alpine (musl)16 matchbyte-identical
wsgirefBottle on wsgiref3.816 matchbyte-identical
cherrypyCherryPy (cheroot)3.1116 matchbyte-identical
djangoDjango on gunicorn gthread3.1216 matchbyte-identical
falconFalcon on waitress3.1016 matchbyte-identical
pyramidPyramid on gunicorn sync3.1416 matchbyte-identical
webpyweb.py (cheroot)3.1116 matchbyte-identical