Shared Connections Under Runtime Hooks
With runtime hooks on, a worker-mode request can be suspended inside code you did not write, and other requests on the same worker run meanwhile. This page covers what they find there: the objects the application keeps for the life of the worker, and database or Redis connections that several requests share.
Hooked waits and shared application state
Every hooked wait that suspends a request is a point where the worker starts or resumes other requests: a cache read in a service constructor, a connect in a container factory, a sleep() in a retry loop. The per-request state listed under What each fiber keeps is saved and restored around the wait. The objects the application keeps for the life of the worker are not, so overlapping requests see the container, singletons and static properties as they are at that moment.
A worker's first request runs alone
With any hook category enabled, a worker admits no other request until its first request ends, or until 250 ms after that request first waits, whichever comes first. A container or router built in the first request is therefore complete before a second request reaches it, as long as the build finishes within those 250 ms. This happens each time a worker starts: at server start, after a recycle, and when a dynamic pool adds a worker.
The requests held back stay in the shared queue, where any other free worker can take them, and their QUEUE_WAIT_TIMEOUT_MS budget runs from the moment they arrive. On a pool with no other worker free, a request whose budget ends before the hold does gets a 529. That takes a budget shorter than the hold (configured that way, or shortened by the server under overload), or a first request that works for most of the budget before it first waits. At the default 1000 ms that is about 750 ms of work, and less for requests that queued while the worker was starting.
Without runtime hooks there is no such window: a request suspends only at the suspension points the application calls.
What stays with the application
- A build that happens later. A build still running after the hold, or one in a later request (a service only some routes use), can be seen half done by another request. Build such services in the worker script before
oxphp_worker()is called, where no request can overlap them. - Per-request state in a shared service. A "current route" or "current request" singleton that is cleared after each request breaks on any overlap, however warm the worker is: the second request finds the first one's value, and the first one's reset wipes the second's. Building it ahead does not help. Run such an application in worker mode without
RUNTIME_HOOKS.
Sharing one connection between fibers
WordPress, Laravel and Symfony open their database and cache clients when the worker boots and hand the same ones to every request. For PDO, mysqli and phpredis this is safe under runtime hooks, and gains nothing: the connection's commands still run one after another, as they do with the hooks off. The shared connection does give the worker thread back while it waits, so other requests run the work that is not on that connection. The real gain is on connections a fiber opens for itself, such as an async task making its own HTTP or database calls.
A command landing in the middle of another fiber's exchange breaks the protocol. Unguarded, mysqlnd refuses it before sending anything, and phpredis returns one request's reply to another with no error. So a fiber claims a connection before it uses it, and another fiber's call on that connection waits. The claim is on the connection, not the PHP object: a persistent connection reached through several PDO objects counts as one, and a connection from PDO::connect() is covered like any other.
phpredis with connection pooling off
With redis.pconnect.pooling_enabled=0, phpredis hands every object opened persistently with the same arguments one connection, and those objects count as one. In a ZTS build that is the constructor's persistent option; a build from phpredis's development branch at commit b37b231 or later does it for pconnect() and popen() as well. With pooling on (the default), each object has a connection of its own.
- Arguments must have the declared types, or be numeric strings (a port read from the environment, written as a whole number). An object opened with a host that is an object, or a port of
"6379.0", counts on its own. - The setting that counts is the one in force at
pconnect()orpopen(), or at the first command for an object the constructor opens. A getter such asgetHost()called before any command connects the object too, so change the setting between the two and the object either waits needlessly or is unguarded. - phpredis 6.3.0 in a ZTS build opens a plain socket for
pconnect(). With pooling off, apconnect()given no persistent id, or anauthcontext entry, can wait for the holder of a constructor's connection with the same arguments as if it were on it.
A fiber keeps the connection until its request ends
A fiber that queried once keeps the connection to the end of its request, also while it is parked on something else. A request that has queried and then waits on its own work that needs the same connection is waiting for itself: the two come apart on the bound instead of proceeding. Give that work a connection of its own.
Every wait is bounded
A call waits for a shared connection for at most the smaller of max_execution_time and default_socket_timeout. With neither set that is 30 seconds, and 60 seconds where max_execution_time is 0. max_execution_time is read as the request has it now, so set_time_limit() counts. default_socket_timeout is read as the process started with it, so a library that narrows it for one call does not shorten the bound for later requests.
Past the bound, the server log says why, and then:
- PDO and mysqli hand the call to the client, which refuses a command issued mid-exchange with its own error, the one your application already handles;
- phpredis throws a
RedisExceptionand sends nothing; PDO::rollBack()on a persistent connection another fiber holds, with a transaction open, sends nothing and returnsfalse, because the transaction is the holder's. The error it reports, in the way the error mode says, is the one the connection records, which can be the holder's. This also applies to code outside any fiber.
Two fibers that each hold what the other waits for come apart on the bound. One such pair comes apart at once, with a log line: a request waiting in session_start() for the session another request on its worker has open (see Worker State Between Requests), while it holds a connection that request waits for.
Transactions and dropped PDO handles
On a persistent connection a request or task holds, dropping a PDO handle rolls back the open transaction only if the holder was the last to use that handle (it built the handle, or called a guarded PDO method through it). That holds wherever the handle is destroyed, including PHP's cycle collector running in another request. Since PHP 8.4.27 and 8.5.12, PDO itself rolls back only for the last handle on a persistent connection. OxPHP sends the rollback anyway, unless the dropping request also holds the connection and keeps another handle it built or used; then the transaction stays open for that handle, past the end of the request if that handle lives in a static property or a container.
In practice, finish a transaction through a handle the request itself built or used. A transaction opened through a local handle and meant to be finished through a handle an earlier request left in a static property is rolled back when the local handle goes. Code outside any request, such as a worker's boot code, holds no connection, and its handles count only one another.
Predis, raw sockets and the socket level
Below the client, OxPHP also guards the socket. A read on a connection another fiber is parked on is refused at once. An fwrite() or fputs() from PHP code (on a stream with no write filter) waits for the holder's exchange, for at most the smaller of the stream's own timeout (stream_set_timeout()) and the bound. Under streams without net, that write is refused too wherever the OpenSSL extension is loaded, which includes the official images.
A refused operation fails the way a timeout does: stream_get_meta_data() reports timed_out, and a refused write sends nothing. Clients can read that as a lost connection:
- mysqlnd closes the connection on a refused write and marks it gone on a refused read, and answers every later command from that state;
- phpredis closes it on a refused read of a reply line. A build from its development branch closes it on every refused read from commit
7ad2f79, and on a refused write from commit48a46c7.
PDO, mysqli and phpredis never reach that refusal in normal use, because their client-level claim comes first. Predis and hand-written protocols have only the socket level, which guards one reply at a time, and Predis closes the connection on a refused operation, which ends the request parked on it. Give such a client a connection of its own per request, or use phpredis.
What is not covered
- Statements kept across requests.
PDOStatement::execute()on a statement prepared in an earlier request runs unclaimed. If it reaches the socket while another fiber is parked on the connection, it is refused there, and mysqlnd loses the connection (also on the read that fetches the next row of an unbuffered result). - Exchanges split by a wait. A hand-written protocol that writes a command, suspends on something else and reads the reply later, or a Predis
MULTIandEXECwith a wait in between, can have another fiber's command take the connection over.
Code outside any fiber
In worker mode this is mainly a destructor that PHP's cycle collector runs between requests while other requests are parked. It cannot wait for a claim. A socket operation it makes on a connection whose holder is parked waiting for its reply fails at once, the way a timeout does, and sends nothing. A phpredis call it makes while the holder is inside a phpredis call on that connection returns false without sending anything. With mysqlnd the refusal can close the connection under the holder, ending its request. Do not use a shared client in a destructor.
Persistent PDO connections
With PDO::ATTR_PERSISTENT, the worker keeps the driver's connection in a pool. Two constructors that overlap would both miss the pool and the second would replace the first's entry, closing that connection under the request reading on it. static $pdo ??= new PDO(...) overlaps routinely, so persistent constructors to one data source run one at a time per worker, and the second finds the connection the first registered.
The data source is the one PDO resolves, so an alias from pdo.dsn.* in php.ini counts as the DSN it names. Constructors to different data sources do not wait for each other, so one stuck on a server that accepts and never answers does not hold up constructors to a healthy one. Two cases wait more:
- DSNs that differ only after a colon. A DSN is compared only up to the first colon after its driver prefix, which an IPv6 host has. Two IPv6 hosts in the same leading group of digits share one wait, a stuck one included.
uri:DSNs and DSNs that are not strings. Such a constructor waits until no persistent constructor to any data source runs on its worker, and the others wait while it runs. Behind a constructor stuck on a server that never answers, its wait runs out and it goes ahead unserialised.
This wait is bounded like the others. A constructor whose budget runs out goes ahead unserialised and the server log says so. Two constructors overlapping that way replace each other's pool entry, which ends the request reading on the replaced connection and can end the worker process when one of those PDO objects is destroyed later. Inside a fiber a userland scheduler started, the budget runs out at once if another constructor is already running.
Constructor options on a pooled connection
A pooled connection another fiber holds is reported alive to PDO's liveness check without the usual ping, which would be refused and make PDO open a second connection. This holds for every driver, including those with no liveness check of their own, such as pdo_sqlite. It is offered only to a constructor that would write nothing new onto the connection:
- every option it passes is one PDO stores on the handle itself:
PDO::ATTR_ERRMODE,PDO::ATTR_CASE,PDO::ATTR_ORACLE_NULLSorPDO::ATTR_DEFAULT_FETCH_MODE(thePDO::ATTR_PERSISTENTentry is not counted); - each has the value the connection already has.
An option the driver has to be told about is refused whatever its value, PDO::ATTR_AUTOCOMMIT, PDO::ATTR_STRINGIFY_FETCHES and PDO::ATTR_EMULATE_PREPARES included, on any driver. A constructor that fails either condition is told the connection is dead and gets a connection of its own, and the server log says so at most once a second.
Laravel's connection options carry PDO::ATTR_EMULATE_PREPARES and PDO::ATTR_STRINGIFY_FETCHES. Its persistent connection is still shared between requests, and between fibers that construct before they query, but a constructor that arrives while another request holds the connection opens its own. A request holds it from its first query to its end.
A connection nobody is using is pinged as usual, so one that has really died is still replaced. That ping is claimed like a query: a query that arrives while it is in flight waits for it.
Closing a shared connection ends the parked request
Closing a shared connection while another request is parked reading it ends that request with a 500. The log names what happened: oxphp: the connection this request was reading from was closed by another fiber while this one waited for its reply (or, for a wait that is not a read, … was using was closed by another fiber while this one waited on it). The request is told as soon as the close happens, rather than at its read deadline, which for mysqlnd is mysqlnd.net_read_timeout, a day by default.
Which calls do this depends on the client:
mysqli::close()andmysqli_close()wait for the holder, within the bound, then close;Redis::close()throws aRedisExceptioninstead of closing;fclose()on a raw stream, and PDO, close at once.
PDO has no close() method: dropping the last reference (unset($pdo), reassigning it, or letting it go out of scope) closes the connection. So a reconnect helper that reassigns a shared PDO handle ends the requests parked on the old connection. A request whose write waits at the socket level for the parked one ends the same way, whoever closes the connection; Predis closes it itself when its own read times out. Applications that close a shared connection to force a reconnect, such as WordPress's $wpdb->check_connection(), should expect the requests parked on it at that moment to fail, rather than return wrong data.
Prompts for your agent
These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.
Find the code in this repository that breaks when two requests overlap on one OxPHP worker with `RUNTIME_HOOKS` on. Read the `features/runtime-hooks-shared-state` page through the OxPHP documentation MCP server.
Look for per-request state kept in shared services, services built lazily after boot, PDO or Redis reconnect helpers, statements kept across requests, persistent PDO connections opened with driver options, and Predis or hand-written protocols on a shared connection. For each one, show me the file and the fix, or tell me to leave `RUNTIME_HOOKS` off if there is none.See also
- Runtime Hooks: the hook categories and where they do not suspend
- cURL Under Runtime Hooks: the same claim applied to cURL handles
- Worker State Between Requests: what a worker resets and what it keeps