gevent / eventlet (P5)
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 on green threads: gevent (gevent.pywsgi, StreamServer, gunicorn's gevent worker) and eventlet (eventlet.wsgi, gunicorn's eventlet worker). Monkey patching swaps out the very classes P1 wraps (socket.socket, subprocess.Popen, os.fork); P5 puts the same wrappers on the replacements, carries the request's id into greenlets the request spawns, and marks the requests of gevent's and eventlet's own servers. It gets in with the same compose-only change as P1.
Covers
| Versions | Kernel alone | |
|---|---|---|
| gevent | 20.12+ (greenlet ≥ 0.4.17, so contextvars are per greenlet) | hook needed: one thread serves every connection |
| eventlet | 0.30+ | hook needed |
| gevent.pywsgi, gevent.server.StreamServer | ||
| eventlet.wsgi | ||
gunicorn -k gevent / -k eventlet | gunicorn 20–23 | requests marked by P2 |
| Python | 3.8–3.14 (as far as gevent/eventlet support 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:
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/p5/proposals/compose.toml) when it detects gevent or eventlet among the dependencies, or -k gevent|eventlet / --worker-class gevent|eventlet in the command.
Import order: gevent and eventlet want monkey.patch_all() / eventlet.monkey_patch() before anything else is imported. The hook imports nothing at startup (no threading, no socket; its own lock is a raw _thread lock), so it does not disturb that order. eventlet's warning "1 RLock(s) were not greened" appears with and without the hook (eventlet's own imports cause it).
What it records
| Event | Emitted from | Join key |
|---|---|---|
request_begin / request_end | gevent: pywsgi.WSGIHandler.handle_one_response (index counted per handler = per connection; status WSGIHandler.code). eventlet: wsgi.HttpProtocol.parse_request / handle_one_request (index per protocol instance = per connection; status 0: eventlet keeps it in a local variable). gunicorn's gevent/eventlet workers: P2 | peer address and port, request index |
| lazy raw session | gevent StreamServer handlers (gevent.baseserver._handle_and_close_when_done), TCP only | as P4 |
spawned | gevent.subprocess.Popen, gevent's os.fork (after patch_os), eventlet's green Popen | child pid |
connected | gevent's socket class (gevent._socket3.socket: connect, connect_ex, sendto), eventlet's GreenSocket | local port |
opened | P1 (builtins are not patched by gevent/eventlet) | pid + fd |
The wrappers are re-applied after each of gevent.monkey.patch_all / patch_socket / patch_subprocess / patch_os and eventlet.monkey_patch, and put on gevent's and eventlet's own classes when they are imported directly (code that uses gevent.socket without patching).
Context following
Every greenlet has its own contextvars context (greenlet ≥ 0.4.17), so the id set in the greenlet that serves a request stays there. A new greenlet starts with an empty context; P5 carries the id into greenlets started by gevent.spawn, gevent.spawn_later, gevent.pool.Group.spawn / Pool.spawn, eventlet.spawn, spawn_n and spawn_after while a request is current (the gevent and eventlet samples check a greenlet that writes a file).
Known gaps
Greenlet(fn).start()andgevent.Greenlet.spawncalled on the class (it is a compiled type) do not carry the id;gevent.spawndoes.- eventlet: no HTTP status (0).
- A child reported by both gevent's
Popenand itsforkis reported once (same pid in a row). - gevent's thread pool (
gevent.threadpool) andgevent.get_hub().threadpool.spawndo not carry the id.
Safety
- Wrapped:
gevent.monkey.patch_*,gevent.spawn/spawn_later,gevent.pool.Group.spawn,gevent._socket3.socket.connect/connect_ex/sendto,gevent.subprocess.Popen.__init__,gevent.pywsgi.WSGIHandler.handle_one_response,gevent.baseserver._handle_and_close_when_done;eventlet.patcher.monkey_patch,eventlet.spawn/spawn_n/spawn_after,eventlet.greenio.base.GreenSocket.connect/connect_ex/sendto,eventlet.wsgi.HttpProtocol(parse_request,handle_one_request). - Each 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. - 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_gevent;grep vbxtrace /proc/<pid>/maps. - Which links did a flow get? The hub shows each link's method; this package's are
hook:P5. - Connections not linked under gevent: check that monkey patching runs through
gevent.monkey(patching by hand,socket.socket = gevent.socket.socket, is still covered because gevent's socket class itself is wrapped).
Sample results
hooks/run-samples.sh python --package p5.
| Sample | What | Python | events | chaos / baseline |
|---|---|---|---|---|
| gevent | pywsgi (keep-alive, spawn, file, connect, stream, a spawned greenlet) + StreamServer session | 3.12 | 22 match | byte-identical |
| eventlet | eventlet.wsgi, the same scenario | 3.11 | 19 match | byte-identical |
| gunicorn-gevent | gunicorn -k gevent | 3.12 | 16 match | byte-identical |
| gunicorn-eventlet | gunicorn -k eventlet | 3.11 | 16 match | byte-identical |