API Index — Namespaces, Packages, Reports, Indices
pool_worker.php
Persistent subprocess entry for ZealPHP's native FCGI-style worker pool.
Loops on stdin frames: read request payload → execute the PHP file → write
response frame to stdout → reset request state → next iteration. Exits
cleanly on EOF (parent closed the pipe) or after ZEALPHP_POOL_MAX_REQUESTS
requests (FPM pm.max_requests parity recycle).
Each iteration is approximately what cgi_worker.php does for ONE request,
but without the per-request boot cost — Composer autoloader, uopz overrides,
and the IPC class stay loaded across iterations. That's what the pool buys
over cgiMode('proc') (where every request pays ~30-50 ms cold-start PHP).
Caveat for unmodified WordPress / Drupal: PHP's global namespace (defined
classes, define() constants, ini_set) PERSISTS across iterations of the
SAME worker. Setting ZEALPHP_POOL_MAX_REQUESTS=1 recycles after each
request → true fresh-process semantics, identical to cgiMode('proc') but
with the parent pool managing spawn cost amortisation. K > 1 (default 500)
is for apps with idempotent boot — modernised legacy or framework code
that guards defined() ?: define(). WordPress itself emits E_NOTICE on
re-define but continues fine (notice ≠ fatal), matching FPM behavior.
uopz overrides: header(), header_remove(), setcookie(), setrawcookie(), http_response_code(), headers_list(), headers_sent() — same shape as src/cgi_worker.php's captures so the response frame can be threaded back through the parent worker's response builder unchanged.
Table of Contents
Classes
- ZealPHP_Pool_State
- Loop control and the response channel, kept out of the included script's reach (C1).
- ZealPHP_IPC_Sender
- Destructor-based metadata frame sender. PHP runs destructors even after
exit()is called from inside a shutdown function — phpMyAdmin'sResponseRenderer->response()does exactly that. The shutdown chain gets preempted, but a destructor on a static instance still fires, so the parent receives status/headers/cookies regardless of how the request ended. - ZealPHP_Pool_NullSessionHandler
- Save handler that stores nothing. Used only by pool_reset_session() (below) to run a throwaway session through session_destroy() without touching real data.
Functions
- pool_reset_session() : void
- Forget the previous request's session before the next one runs (H1).
-
pool_prepare_request()
: array{file: string, prevCwd: mixed}|array{__error: array
} - Prepare per-request state and return the file to include + the prior cwd.
- pool_finish_request() : array<string, mixed>
- Capture output and build the response AFTER the global-scope include.
- pool_reset_handlers() : void
- Drop every error and exception handler the request installed, so none of them runs in the next request (C9). The worker installs none of its own.
- pool_reset_env() : void
- Put the process environment back to its boot state (C9): putenv() from one request must not reach the next one, as FPM restores it at request end.
- pool_reset_request_state() : void
- Reset all per-request state between pool iterations.
Functions
pool_reset_session()
Forget the previous request's session before the next one runs (H1).
pool_reset_session(array<string, string> $baseline) : void
session_write_close() saves and closes the session but leaves the module's internal id set. The next request's session_start() then reuses it instead of reading its own cookie, so a visitor with no cookie (or a different one) was handed the previous user's session — data and, via the host-emitted Set-Cookie, the id itself. FPM gets this for free from per-request module shutdown; a reused pool subprocess has to do it by hand.
PHP has no call that only clears the id: session_id('') stores an EMPTY id, which makes session_start() skip the cookie and mint a new session, so the returning owner would lose theirs. session_destroy() is the one path that resets the module's request globals (id back to NULL). So when an id is left over, start a throwaway session on a no-op save handler and destroy that; the real session's data, already written by session_write_close(), is never touched.
Then restore the session INI settings the request may have changed (session_name(), session_save_path(), session_set_cookie_params(), the save handler, ini_set), so one app's session configuration does not carry into the next request.
Parameters
- $baseline : array<string, string>
-
session INI values at boot
pool_prepare_request()
Prepare per-request state and return the file to include + the prior cwd.
pool_prepare_request(array<string|int, mixed> $req) : array{file: string, prevCwd: mixed}|array{__error: array}
The include itself is performed by the CALLER at GLOBAL scope (see the
request loop) — NOT here — so a legacy app's top-level variables
($menu / $submenu in WP's wp-admin) become real $GLOBALS. Including from
inside a function made them function-locals, so WP's global $menu resolved
to null and uksort($menu, …) fataled. [Issue 1: include scope]
Parameters
- $req : array<string|int, mixed>
Return values
array{file: string, prevCwd: mixed}|array{__error: arraypool_finish_request()
Capture output and build the response AFTER the global-scope include.
pool_finish_request(mixed $result, Throwable|null $error, mixed $prevCwd) : array<string, mixed>
Parameters
- $result : mixed
-
the include's return value (null if it threw)
- $error : Throwable|null
-
exception thrown by the include, if any
- $prevCwd : mixed
-
cwd to restore
Return values
array<string, mixed>pool_reset_handlers()
Drop every error and exception handler the request installed, so none of them runs in the next request (C9). The worker installs none of its own.
pool_reset_handlers() : void
PHP has no call that clears the handler stack, and set_error_handler(null) pushes the old handler instead of dropping it. Each round below sets a null handler to read the current one, undoes that push, then pops one level; it stops once no handler is left.
pool_reset_env()
Put the process environment back to its boot state (C9): putenv() from one request must not reach the next one, as FPM restores it at request end.
pool_reset_env(array<string, string> $baseline) : void
Parameters
- $baseline : array<string, string>
-
getenv() at boot
pool_reset_request_state()
Reset all per-request state between pool iterations.
pool_reset_request_state() : void
Clears superglobals ($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES,
$_REQUEST, $_SESSION), the raw input buffer, response capture state
($__pw_headers, $__pw_cookies, $__pw_rawcookies, $__pw_status),
any queued shutdown functions, and all output buffers.
Performs FPM-style $GLOBALS cleanup (unsets request-scope keys not in
the boot snapshot) and, when ext-zealphp is loaded, calls
zealphp_process_state_clean() to roll back constants, classes, functions,
and included files to the boot baseline.
When ZEALPHP_POOL_FULL_RESET=1 is set, also resets op_array
run_time_cache, function-local statics, and class static properties
(mirrors CoSessionManager's per-request reset stack from ext-zealphp 0.3.25).
IMPORTANT: Must be called at GLOBAL SCOPE (not inside a function) so that
$_SESSION = null and the superglobal resets take effect process-wide.