Skip to content

Long-running PHP (H2) ​

Ready

The hub recognises the services this package fits and proposes the compose part of the change, if any; the source change stays by hand, with the steps and examples in the proposal's review. Nothing changes until a player approves it (Hook proposals). The cheat sheet has the steps by hand.

For PHP services that run as one long-lived process serving many requests: Swoole and OpenSwoole (coroutines), Workerman and ReactPHP (event loops), Amp (fibers on the Revolt loop), RoadRunner (a Go server with PHP workers) and FrankenPHP (a Go server with embedded PHP threads, in worker mode). Here the kernel usually cannot tell requests apart: one PHP thread serves many connections, or the connection belongs to a Go process and never reaches PHP at all. This hook tells the sensor which request is running.

It gets in in two parts:

  • Config-only: the ini file from H1 (a read-only mount of the hook directory plus one file in conf.d), which loads the hook's binding into every PHP process.
  • A one-line source change that wraps the server's request handler. PHP has no way to wrap a running server from outside without an extension of its own.

The wrap line is guarded by class_exists(…, false), so the service runs unchanged when the hook is reverted.

Covers ​

Versions (tested in a sample)Kernel links it without the hook?
SwooleSwoole\Http\Server, process and base mode, HTTP/1 and HTTP/2 (tested: 6.3, process mode, HTTP/1)no: coroutines share a thread; in process mode a reactor thread owns the sockets
OpenSwooleOpenSwoole\HTTP\Server (tested: 22.1)no, same as Swoole
WorkermanWorker::$onMessage, HTTP and raw protocols (tested: 5.2, HTTP)only while one connection is open per worker
ReactPHPreact/http 1.x (tested: 1.11)no: one event loop serves every connection
Ampamphp/http-server 3.x, Revolt, a fiber per request (tested: 3.4)no, same as ReactPHP
RoadRunnerPSR-7 workers via spiral/roadrunner-http 3.x (tested: rr 2025.1, roadrunner-http 3.6)no: the Go server rr owns the connection, PHP gets the request over a pipe
FrankenPHP1.x, worker mode and classic mode (tested: 1.13, both modes)no: Caddy (Go) owns the connection
PHP7.4–8.5 (fibers need 8.1), NTS and ZTS, glibc and musl, x86_64 and aarch64 (samples: 8.1, 8.3, 8.5 ZTS)

Untested combinations (Swoole base mode, HTTP/2, Workerman raw protocols, older majors) follow the same code paths. The Known gaps list what differs.

FrankenPHP's classic mode needs no source change: every request is a PHP request, and H1's ini file covers it on its own.

How it gets in ​

From a registry (local development, or a build with network; the same release as the hook directory): composer require vulnboxd/vbxtrace:1.0.0, then auto_prepend_file=vendor/vulnboxd/vbxtrace/php/prepend.php; the prebuilt extension is vbxtrace-php-ext-1.0.0.tar.gz on the release page; see Local development.

1. The ini file (config-only) ​

This is the same mount as for H1. The images these runtimes use (phpswoole/swoole, openswoole/swoole, dunglas/frankenphp, php:*-cli) have no ext-ffi, so the hub proposes the extension variant:

diff
 services:
   <service>:
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+      - /usr/local/lib/vulnboxd/trace/php/conf.d/zz-vbxtrace-ext.ini:/usr/local/etc/php/conf.d/zz-vbxtrace.ini:ro
+      - /usr/local/lib/vulnboxd/trace/php/ext/vbxtrace-php8.3-nts-x86_64.ini:/usr/local/etc/php/conf.d/zz-vbxtrace-ext.ini:ro

The second ini file loads the extension built for the PHP minor version, nts or zts, and the architecture, fixed in its name when the hub makes the proposal (FrankenPHP is always zts), so the environment a worker gets doesn't matter. A triple that no longer matches (the image was upgraded) leaves the hook off without printing anything.

Where the image has ext-ffi, the basic variant (zz-vbxtrace.ini, no extension) is enough: PHP allows FFI in the CLI SAPI by default, and Swoole, Workerman, ReactPHP, Amp and RoadRunner workers all run in the CLI SAPI. FrankenPHP isn't the CLI SAPI, so it needs the extension, preload or FFI variant. The details, and how an existing auto_prepend_file is kept, are in H1.

2. The wrap line (source change) ​

One call to \VbxTrace\Hook::wrap('<runtime>', $handler) where the service hands its handler to the server. The hub locates the line by pattern and shows the full diff.

Swoole / OpenSwoole:

diff
+if (class_exists(\VbxTrace\Hook::class, false)) {
+    $handler = \VbxTrace\Hook::wrap('swoole', $handler, ['server' => $server]);
+}
 $server->on('request', $handler);

Passing the server is optional. It gives exact request indexes on keep-alive connections; without it the index is unknown. OpenSwoole uses 'openswoole' (the same adapter).

Workerman:

diff
+if (class_exists(\VbxTrace\Hook::class, false)) {
+    $onMessage = \VbxTrace\Hook::wrap('workerman', $onMessage);
+}
 $worker->onMessage = $onMessage;

ReactPHP:

diff
+if (class_exists(\VbxTrace\Hook::class, false)) {
+    $handler = \VbxTrace\Hook::wrap('react', $handler);
+}
 $http = new React\Http\HttpServer($handler);

Amp: the result is a RequestHandler:

diff
+if (class_exists(\VbxTrace\Hook::class, false)) {
+    $handler = \VbxTrace\Hook::wrap('amp', $handler);
+}
 $server->start($handler, new DefaultErrorHandler());

RoadRunner: move the per-request code into a callable and call it from the loop:

diff
+if (class_exists(\VbxTrace\Hook::class, false)) {
+    $handler = \VbxTrace\Hook::wrap('roadrunner', $handler);
+}
 while ($request = $psr7->waitRequest()) {
     $psr7->respond($handler($request));
 }

FrankenPHP worker mode:

diff
+if (class_exists(\VbxTrace\Hook::class, false)) {
+    $handler = \VbxTrace\Hook::wrap('frankenphp', $handler);
+}
 while (frankenphp_handle_request($handler)) {

wrap() returns the handler unchanged when the hook is off, unavailable or fails to wrap. The wrapper passes every argument through and returns or throws exactly what the handler does.

Laravel Octane, Hyperf, webman and Symfony Runtime run on these servers, but their handler sits inside the framework, where the hub's patterns may not find it. No sample covers them yet. Wrap the framework's request entry point by hand the same way.

The hub proposes these changes when it detects any of:

  • a phpswoole/swoole, openswoole/swoole or dunglas/frankenphp base image;
  • a .rr.yaml file;
  • workerman/workerman, react/http, amphp/http-server or spiral/roadrunner-http in composer.lock;
  • the handler patterns listed in the templates, in hooks/php/h2/proposals/.

What it records ​

Runtimerequest_begin fromPeer and portRequest indexrequest_end status
Swoole / OpenSwoolethe wrapped on('request') callback$request->server: remote_addr, remote_port, server_portHTTP/1: counted per connection (fd + connect time) in the worker, exact in base mode and dispatch_mode 2/4, else unknown; HTTP/2: (streamId − 1) / 20 (Swoole can't read a status back)
Workermanthe wrapped onMessagegetRemoteIp/Port, getLocalPortcounted per TcpConnection (exact)0 (the response leaves through send())
ReactPHPthe wrapped request handlerserver params REMOTE_ADDR/PORT, SERVER_PORT (taken from the socket)unknown (React doesn't count)from the response, also asynchronously
Ampthe RequestHandler decoratorClient::getRemoteAddress(), getLocalAddress()counted per client (connection)from the response
RoadRunnerthe wrapped handlerREMOTE_ADDR only (rr doesn't pass the port)unknownfrom the response
FrankenPHP workerthe wrapped frankenphp_handle_request() callbackREMOTE_ADDR, REMOTE_PORT; local port 0 (Caddy's SERVER_PORT comes from the Host header)unknownhttp_response_code()

The protocol is http1, http2 or http3. Workerman's non-HTTP protocols report raw, one request per message, indexed per connection.

Helpers ​

PHP can't intercept proc_open, exec, fopen or stream_socket_client from outside. In a coroutine or event-loop server the kernel can't attribute what they do either, so the binding offers explicit calls for the places that matter. Each is a no-op without a tracer, and each returns its argument unchanged where it takes one:

php
\VbxTrace\Hook::spawnedProc($proc);              // after proc_open(): spawned with the child's pid
\VbxTrace\Hook::spawned($pid);                   // after pcntl_fork() or anything else that yields a pid
\VbxTrace\Hook::connectedStream($socket);        // after stream_socket_client()/fsockopen(): connected with the local port
\VbxTrace\Hook::connected($localPort, $ip, $port, \VbxTrace\Hook::TRANSPORT_TCP);
\VbxTrace\Hook::opened($fd, $flags);             // when you have a file descriptor
$ctx = \VbxTrace\Hook::current();                // capture the context for a callback that runs later…
\VbxTrace\Hook::connected($lp, $ip, $port, \VbxTrace\Hook::TRANSPORT_TCP, $ctx); // …and pass it explicitly

The samples use them, inside a child coroutine (Swoole), in a resumed fiber (Amp) and in a promise callback with a captured context (ReactPHP).

Context following ​

RuntimeMechanismFollows automaticallyNeeds explicit context
Swoole / OpenSwooleCoroutine::getContext() of the request's coroutine; child coroutines look it up through getPcid()go() / Coroutine::create() children, across every yieldwork handed to task workers ($server->task()) or other processes: Hook::handoff()
Amp, ReactPHP with react/asynca WeakMap keyed by the running Fibereverything inside the request's fiber, across suspensionsnew fibers started with async(): capture Hook::current() and pass it
ReactPHP (promises), Workermanthe process's current context, set for the synchronous part of the handlerthe handler's own synchronous codepromise callbacks and timers: capture Hook::current(). The adapter emits context_exit when the handler returns a pending promise
RoadRunner, FrankenPHP workerper process (RoadRunner) or per thread (FrankenPHP, ZTS)everything the worker does for the requestnothing: one request at a time

Known gaps ​

  • No automatic spawned / connected / opened. Without the helpers above, a coroutine or event-loop server's child processes and outgoing connections are linked only when the kernel can prove it: while exactly one request is open in the process.
  • Fiber switches aren't reported, so syscalls made between suspensions are attributed through the helpers only. A Revolt driver decorator that emits context_enter/context_exit at every switch is a possible follow-up.
  • RoadRunner, FrankenPHP: the client connection is linked on the Go side (package G1 on rr or the frankenphp binary). The PHP side contributes request boundaries and the client address.
  • Swoole status is always 0. In Swoole process mode with dispatch_mode 1, 3 or 7, the request index is unknown (requests of one connection can reach different workers).
  • WebSocket servers (Swoole on('message'), Workerman WebSocket) aren't wrapped yet. Wrap the handshake handler by hand if needed.
  • Task workers / $server->task() don't carry the context. Use Hook::handoff() and pass the id.

Safety ​

  • Every hook call is guarded. The wrappers pass arguments through untouched, return or rethrow exactly what the handler does, and add no output, headers or status.

  • While no tracer is attached, the wrapper costs one vbx_v1_enabled() call per request.

  • After 5 internal errors the binding switches itself off for the process. VBXTRACE=0 switches it off at start.

  • The adapters are loaded only by wrap(). Nothing is registered globally: no autoloader, no error handler, no stream wrapper. In Swoole the context lives under the key vbxtrace.ctx of the coroutine's context object.

  • Every sample was run through the harness on x86_64 in three modes: events (the recorded events match the expected ones), chaos (the core forced to fail on every call) and baseline (without the hook). In both chaos and baseline mode the responses were byte-identical to the events run. The samples, all on the extension variant:

    • Swoole (phpswoole/swoole:php8.3-alpine);
    • OpenSwoole 22 (openswoole/swoole, PHP 8.1 on Debian 11);
    • Workerman 5, ReactPHP (react/http 1.x), Amp v3 and RoadRunner (each on php:8.3-cli-alpine);
    • FrankenPHP in classic mode and in worker mode (dunglas/frankenphp, PHP 8.5 ZTS).

    They also cover context following: a child coroutine (Swoole, OpenSwoole), a resumed fiber (Amp) and a promise callback with a captured context (ReactPHP).

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.

Reverting only the ini mounts (both, for the extension variant) is enough to switch the hook off. The wrap line then skips itself, and can be left in place or reverted separately.

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? Inside the container, php -r 'var_dump(class_exists("VbxTrace\\Hook", false));' prints true when the ini file is in place. With the extension variant, php -m lists vbxtrace. Once a tracer is attached, the server's /proc/<pid>/maps contains libvbxtrace.
  • Which links did a flow get? The hub shows each link's method; this package's are hook:H2.
  • The wrap line does nothing: the ini files aren't loaded in that process. Check php --ini. They do not depend on the environment, so a supervisor that clears it changes nothing. With the extension variant, if php -m does not list vbxtrace, the triple in zz-vbxtrace-ext.ini no longer matches php -v (an upgraded image): ask the hub for a new proposal.
  • FrankenPHP shows no events: FrankenPHP isn't the CLI SAPI, so it needs the extension, preload or FFI variant, not the basic one.