Skip to content

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 ​

VersionsKernel alone
gevent20.12+ (greenlet ≥ 0.4.17, so contextvars are per greenlet)hook needed: one thread serves every connection
eventlet0.30+hook needed
gevent.pywsgi, gevent.server.StreamServer
eventlet.wsgi
gunicorn -k gevent / -k eventletgunicorn 20–23requests marked by P2
Python3.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:

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

EventEmitted fromJoin key
request_begin / request_endgevent: 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: P2peer address and port, request index
lazy raw sessiongevent StreamServer handlers (gevent.baseserver._handle_and_close_when_done), TCP onlyas P4
spawnedgevent.subprocess.Popen, gevent's os.fork (after patch_os), eventlet's green Popenchild pid
connectedgevent's socket class (gevent._socket3.socket: connect, connect_ex, sendto), eventlet's GreenSocketlocal port
openedP1 (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() and gevent.Greenlet.spawn called on the class (it is a compiled type) do not carry the id; gevent.spawn does.
  • eventlet: no HTTP status (0).
  • A child reported by both gevent's Popen and its fork is reported once (same pid in a row).
  • gevent's thread pool (gevent.threadpool) and gevent.get_hub().threadpool.spawn do 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=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? VBXTRACE_DEBUG=1 prints installed 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.

SampleWhatPythoneventschaos / baseline
geventpywsgi (keep-alive, spawn, file, connect, stream, a spawned greenlet) + StreamServer session3.1222 matchbyte-identical
eventleteventlet.wsgi, the same scenario3.1119 matchbyte-identical
gunicorn-geventgunicorn -k gevent3.1216 matchbyte-identical
gunicorn-eventletgunicorn -k eventlet3.1116 matchbyte-identical