Async Promises

OxPHP runs PHP closures in background threads, on a dedicated pool that is separate from the HTTP worker pool. Long-running work goes there instead of blocking request handling.

How it works

  1. You dispatch a task by calling oxphp_async() with a closure and optional arguments. OxPHP serializes the closure's use-variables and arguments, sends them to the async pool, and returns a promise ID immediately
  2. A dedicated async worker thread deserializes the data, runs the closure, and serializes the result
  3. You await the result by calling oxphp_async_await() with the promise ID. In worker mode with fibers, the current fiber suspends and other requests continue on the same thread. In traditional mode, the worker thread blocks until the result is ready
  4. Any promises not explicitly awaited are automatically cancelled and cleaned up at the end of the request or, for a promise that an async task dispatched, when that task ends. That includes a task still running PHP code that never suspends: it is interrupted at the next point where the engine checks for an interrupt, such as a loop's jump back, and unwound with OxPHP\Async\AsyncException, which its catch and finally blocks see. A task inside a single long call into C code (a curl request, say) is interrupted only once that call returns. Work that has to finish must be awaited

Configuration

Variable Default Description
ASYNC_WORKERS 0 (disabled) Number of dedicated async worker threads. Set to 0 to disable the async pool entirely
ASYNC_QUEUE_CAPACITY 0 (auto) Maximum pending async tasks. When 0, defaults to ASYNC_WORKERS × 64
ASYNC_MAX_FIBERS 256 Per-worker cap on concurrent async task fibers. The process-global in-flight limit (queued + running tasks) is ASYNC_MAX_FIBERS × ASYNC_WORKERS; a dispatch past it is rejected immediately (non-blocking) with OxPHP\Async\AsyncException, so fan-out composition cannot deadlock waiting on capacity it holds
Note

The async pool is disabled by default (ASYNC_WORKERS=0). With the pool disabled, all four async functions exist but throw OxPHP\Async\AsyncException when called. Set ASYNC_WORKERS to a value greater than 0 to enable background execution.

Dispatching tasks

Pass a closure and optional arguments to oxphp_async(). It returns a promise ID (integer) immediately:

php
<?php $promise = oxphp_async(function (string $url) { return file_get_contents($url); }, 'https://api.example.com/data'); // The closure is running in the background. // Do other work here... $result = oxphp_async_await($promise); echo $result;

Passing data to closures

Use use-variables or function arguments to pass data. Only scalar types and arrays are supported:

php
<?php $apiKey = 'sk-abc123'; $ids = [1, 2, 3]; $promise = oxphp_async(function () use ($apiKey, $ids) { // $apiKey and $ids are available here return count($ids); });

Awaiting results

Single promise

php
<?php $result = oxphp_async_await($promise); // Wait indefinitely $result = oxphp_async_await($promise, 5.0); // Wait up to 5 seconds

A timeout of 0.0 (the default) waits indefinitely. So do a negative timeout, NAN, INF, and anything above 1e9 seconds (about 31.7 years): a timeout sets a limit only when it is above 0 and at most 1e9 seconds. The same holds for the timeout of oxphp_async_await_all(), oxphp_async_await_race() and oxphp_async_await_any(). On timeout, OxPHP\Async\TimeoutException is thrown.

All promises

oxphp_async_await_all() waits for every promise and returns an associative array keyed by promise ID:

php
<?php $p1 = oxphp_async(fn() => file_get_contents('https://api.example.com/users')); $p2 = oxphp_async(fn() => file_get_contents('https://api.example.com/orders')); $results = oxphp_async_await_all([$p1, $p2], 10.0); $users = $results[$p1]; $orders = $results[$p2];
Note

oxphp_async_await_all() awaits promises sequentially in array order. All closures run concurrently on the async pool, but the calling thread collects results one at a time.

First settled promise (race)

oxphp_async_await_race() returns as soon as one promise settles, whether it fulfilled or rejected:

php
<?php // fetch_from_primary_db() and fetch_from_replica_db() are loaded by opcache.preload: // see "Your own functions and classes in tasks" below. $p1 = oxphp_async(fn() => fetch_from_primary_db()); $p2 = oxphp_async(fn() => fetch_from_replica_db()); $winner = oxphp_async_await_race([$p1, $p2], 5.0); // $winner = ['id' => int, 'value' => mixed] echo "Promise {$winner['id']} won: {$winner['value']}";

Non-winning promises remain awaitable individually after oxphp_async_await_race() returns. If the winning promise rejected, OxPHP\Async\AsyncException is thrown, and the losers are still awaitable. This is the JavaScript Promise.race analog.

First fulfilled promise

oxphp_async_await_any() returns as soon as one promise FULFILLS. Rejections are accumulated and only become observable if every promise rejects. This is the JavaScript Promise.any analog, useful for fallback / redundancy patterns ("any mirror that responds").

php
<?php $mirror_a = oxphp_async(fn() => file_get_contents('https://mirror-a.example.com/data')); $mirror_b = oxphp_async(fn() => file_get_contents('https://mirror-b.example.com/data')); $mirror_c = oxphp_async(fn() => file_get_contents('https://mirror-c.example.com/data')); try { $winner = oxphp_async_await_any([$mirror_a, $mirror_b, $mirror_c], 5.0); // ['id' => one of the input ids, 'value' => its result] } catch (\OxPHP\Async\AggregateAsyncException $e) { foreach ($e->getErrors() as $i => $err) { // $err keyed by input position 0..N-1 } foreach ($e->getErrorMap() as $promise_id => $err) { // $err keyed by promise id } } catch (\OxPHP\Async\TimeoutException $e) { foreach ($e->getPartialErrors() as $promise_id => $err) { // promises that already rejected before the deadline } $cancelled = $e->getCancelledPromiseIds(); // Promise ids that had not settled at the deadline. The cancel flag // is set on each AND their receivers were dropped — passing any of // these ids to oxphp_async_await*() afterwards throws "unknown or // already-awaited promise id". Treat the list as an audit trail, not // a resumable queue. }

Non-winning promises that were still pending at the moment of victory remain awaitable individually. Promises that already rejected before the winner do not, because their results were consumed when accumulated as candidate errors.

Exception types

Class Thrown by Notes
OxPHP\Async\AsyncException oxphp_async_await(), oxphp_async_await_all(), oxphp_async_await_race() Single error with message and optional original-exception details.
OxPHP\Async\TimeoutException All four await-* on deadline Extends AsyncException. For oxphp_async_await_any() timeouts, getPartialErrors() and getCancelledPromiseIds() are populated; for the other call sites both return [].
OxPHP\Async\AggregateAsyncException oxphp_async_await_any() when every promise rejects Extends AsyncException. Provides getErrors() (positional, keyed 0..N-1), getErrorMap() (id-keyed), getPromiseIds().

Error handling

Exceptions thrown inside an async closure are captured and re-thrown at await time as OxPHP\Async\AsyncException:

php
<?php $promise = oxphp_async(function () { throw new \RuntimeException('Something failed'); }); try { $result = oxphp_async_await($promise); } catch (\OxPHP\Async\AsyncException $e) { // "Async task failed: [RuntimeException] Something failed" echo $e->getMessage(); }

exit() and die() inside an async closure are also caught and converted to OxPHP\Async\AsyncException. The async worker survives and continues processing new tasks.

Exception hierarchy

text
\Exception └── OxPHP\Async\AsyncException # All async errors ├── OxPHP\Async\TimeoutException # Timeout-specific └── OxPHP\Async\AggregateAsyncException # Every promise rejected (await_any)

Fiber integration

In worker mode, oxphp_async_await() cooperates with OxPHP's fiber scheduler. Instead of blocking the worker thread, the current fiber suspends while it waits for the result. The scheduler resumes it once the result is ready, so other requests keep moving on the same thread.

In traditional mode (no worker file), oxphp_async_await() blocks the worker thread synchronously. The worker cannot handle other requests while it waits.

For best performance, combine async promises with worker mode:

worker.php
<?php // worker.php require __DIR__ . '/../vendor/autoload.php'; oxphp_worker(function () { // These two API calls run concurrently on the async pool // while the fiber suspends — the worker thread is free for other requests $p1 = oxphp_async(fn() => file_get_contents('https://api.example.com/users')); $p2 = oxphp_async(fn() => file_get_contents('https://api.example.com/orders')); $results = oxphp_async_await_all([$p1, $p2]); echo json_encode($results); });

Composition (nested async)

An async task may itself call oxphp_async() and await the result. Because each task runs inside a scheduler fiber, awaiting a nested promise suspends the task's fiber and frees its worker to run the nested task. This lets a task fan out to children without holding a worker while it waits.

php
<?php $p = oxphp_async(function (): int { // Dispatched and awaited from inside an async task $inner = oxphp_async(fn () => 21); return oxphp_async_await($inner) * 2; }); $result = oxphp_async_await($p); // 42

oxphp_async_await_all(), oxphp_async_await_race(), and oxphp_async_await_any() may likewise be called from inside a task fiber. The number of concurrent task fibers (queued + running) is bounded by ASYNC_MAX_FIBERS × ASYNC_WORKERS; a dispatch that would exceed the cap is rejected immediately with OxPHP\Async\AsyncException instead of blocking, so a fan-out cannot deadlock by waiting on capacity it is itself holding.

A task that ends without awaiting a promise it dispatched cancels that child task, the same way the end of a request cancels the tasks the request did not await. The child cannot outlive its parent, so work that has to finish must be awaited inside the task. When the child's code, and the code of every task above it, is in OPcache's shared memory, the parent's result is delivered at once and the child finishes its cancellation on its own. Otherwise the parent's result is held until the child has stopped (see Timeouts cancel the abandoned task). A child that no async worker had started yet when its parent ended is never run, and holds nothing.

Limitations

Async closures run on separate threads. This imposes restrictions on what data can cross the thread boundary:

Allowed Not allowed
null, bool, int, float, string Any other object. OxPHP\Shared\Shareable is reserved for the Shared\* types: a class written in PHP can neither implement it nor extend a class that does
Arrays of scalar types Resources (file handles, DB connections, streams)
Nested scalar arrays Closures whose use captures non-Shareable objects
Shared\* instances (Counter, Map, Channel, Atomic, Flag, Mutex, Once, Pool, Registry)

Additional constraints:

  • The closure must be user-defined, not a wrapper around a built-in function
  • Functions and classes your script declares do not exist in a task. Only built-in ones exist on every thread, and those loaded by opcache.preload, which also lists what fails and when
  • Arguments and return values are serialized across the thread boundary, so large arrays or strings add latency
  • Plain PHP values are not shared. Each async worker has its own PHP environment, and plain variables, arrays, and class instances are copied (or rejected) across the boundary. Use the shared-state primitives (Shared\Counter, Shared\Map, Shared\Channel, …) to pass references that are visible to both threads
  • A Shared\Pool created outside worker mode, by an HTTP request or by a script under oxphp run, can be passed to a task but not used there. Every acquire on it in the task throws ClosedException, and Registry::pool() or Registry::global() there throws SharedException rather than bind it to a name. Only the request that created such a pool can use it. See Shared\Pool
  • A task has no end of script. It is not a request, so register_shutdown_function() called from an async closure registers a callback that never runs: there is no response for it to write into and no request for it to be the end of, and it is discarded when the task finishes. Do that work at the end of the closure itself
  • Pool threads are replaced for an OPcache restart, each at a moment it runs no task. A busy thread stops taking tasks for at most 5 seconds to reach one, and with ASYNC_WORKERS=1 tasks stay queued meanwhile (a dispatch to a full queue is refused as usual). What a task left on its thread (static variables, ini values) goes with the thread; OPcache restarts says which tasks hold the restart
  • Pool threads are replaced after a fatal error stops their cycle collector. A fatal error raised while a task's cycle collection runs (in a destructor, or in a generator's finally the collection reached) logs a fatal interrupted a cycle collection, then Async worker thread retiring: the extension scheduled an exit. The thread keeps running tasks without the cycle collector until a moment it runs none, and is replaced then; it does not stop taking tasks to reach that moment
  • A task has no execution deadline. max_execution_time and set_time_limit() inside a task still succeed (set_time_limit() returns true) and read back through ini_get(), but nothing counts them down, since a pool thread runs every task inside one long request. Bound a task from the side that waits for it, with the timeout argument of oxphp_async_await() and its variants, which cancels the task it gives up on
  • With runtime hooks on, max_execution_time still bounds a wait for a connection or cURL handle another fiber is using (see Every wait is bounded). A task that lowers it shortens that wait for later tasks on its thread as well, because an ini value a task sets stays until its thread is replaced (for an OPcache restart, or after a fatal error stops its cycle collector) or the server stops

Your own functions and classes in tasks

An async worker thread never runs your script or the files it includes, so the functions and classes they declare do not exist there:

  • A task that calls a function declared at the top level of the closure's own file, or declares a class (anonymous or named) inside the closure, fails before its first statement runs. oxphp_async_await() throws OxPHP\Async\AsyncException with a message such as Async task failed: [Error] Call to undefined function helper() at /var/www/html/worker.php:12: it belongs to a script this PHP thread has not loaded; …. The whole closure is checked, including the closures it creates and the branches it never takes.
  • Every other function of your script that the task's thread enters (through a callable built at run time, say) is checked as the thread enters it, and does not start if it fails. The caller gets the Error, which it can catch, and a generator gets it when it first resumes.
  • A function the file declares inside an if or inside another function is missing too, but the task runs until it reaches the call and fails with PHP's own Call to undefined function.
  • A named class the task instantiates fails with PHP's own Class "…" not found when the task reaches it.
  • The same goes for a Shared\Pool factory or destroy callback that a task's acquire or evict() runs. Shared\Pool covers what such a callback may capture there, what becomes of its static variables, and when the check on entering a function does not run.

The checks before the task starts apply with OPcache, which is on by default, and only to a closure in a file OPcache caches. Without OPcache, and for a file OPcache compiles without caching (a blacklisted one, say, or one changed too recently under opcache.file_update_protection), a call to a function of a file included before the closure's file is also refused before the task starts. OPcache may replace a call to a function that only returns a constant with that constant, so such a call works in a task, but that is not a way around this rule.

Functions and classes loaded by opcache.preload exist on every PHP thread, the async worker threads included. Put what tasks need into files that the preload script loads:

zz-preload.ini
; /usr/local/etc/php/conf.d/zz-preload.ini opcache.preload=/var/www/html/preload.php
preload.php
<?php // /var/www/html/preload.php require __DIR__ . '/src/tasks.php';
src/tasks.php
<?php // /var/www/html/src/tasks.php function thumbnail_path(string $path): string { return dirname($path) . '/thumb-' . basename($path); } final class Thumbnail { public function __construct(public readonly string $path) {} }

A task can then call thumbnail_path() and use Thumbnail, whether its closure is written in your script or in a preloaded file. A closure written in a preloaded file can also declare anonymous classes and call that file's functions. Preloaded code is fixed for the life of the server, so restart the server after changing those files. Constants are not preloaded: a constant that a preloaded file defines with const or define() does not exist in a task.

A class is preloaded only when its parent class, interfaces and traits are built in or preloaded as well, so the preload script has to load the files that declare them too. If it does not, a class loaded with require stops the server from starting with a fatal error. A class compiled with opcache_compile_file() is left out instead, with a Can't preload unlinked class warning at startup, and a task that uses it gets Class "…" not found. Your script may still require a preloaded file. Its functions and classes are not declared a second time, but its other top-level statements run in your script.

When PHP runs as root, it refuses to preload unless opcache.preload_user is set. OxPHP started as root switches to www-data before PHP starts, so the published image needs no opcache.preload_user unless the server keeps root (--user=root).

Docker example

compose.yaml
services: app: image: ghcr.io/oxphp/oxphp:0.16.0 ports: - "80:80" environment: - DOCUMENT_ROOT=/var/www/html/public - WORKER_MODE_ENABLED=true - ENTRY_FILE=worker.php - ASYNC_WORKERS=4 - ASYNC_QUEUE_CAPACITY=256

Troubleshooting

"Async pool is disabled. Set ASYNC_WORKERS > 0 to enable."

The async pool is not configured. When ASYNC_WORKERS=0 (the default), the async functions are registered but throw OxPHP\Async\AsyncException on every call.

Fix: Set ASYNC_WORKERS to a positive value:

bash
ASYNC_WORKERS=4
"Failed to dispatch async task (pool full)"

The async pool is running but all queue slots are occupied, or the process-global in-flight cap (ASYNC_MAX_FIBERS × ASYNC_WORKERS, covering queued + running tasks) has been reached. Both raise OxPHP\Async\AsyncException on dispatch and increment oxphp_async_tasks_rejected_total.

Check: Verify the pool is accepting tasks:

bash
curl -s http://localhost:9090/config | jq '.async_workers'

Fix: Increase ASYNC_WORKERS or ASYNC_QUEUE_CAPACITY.

"Cannot pass object values in use-vars to async closure"

Objects cannot be serialized across thread boundaries.

Fix: Extract the scalar data you need before dispatching:

php
<?php // Wrong: passing an object $promise = oxphp_async(function () use ($user) { ... }); // Correct: passing scalar data extracted from the object $userId = $user->getId(); $userName = $user->getName(); $promise = oxphp_async(function () use ($userId, $userName) { ... });
Await hangs in traditional mode

In traditional mode, oxphp_async_await() blocks the worker thread. If all PHP workers are blocked waiting for async results, the server stops processing requests.

Fix: Enable worker mode (WORKER_MODE_ENABLED=true) so that oxphp_async_await() suspends the fiber instead of blocking the thread.

Timeouts cancel the abandoned task

OxPHP\Async\TimeoutException is raised on the await side the moment the deadline passes. The background task is no longer left running unobserved: a task parked in oxphp_sleep() or suspended awaiting a child promise is resumed and unwinds (its finally blocks run), and a CPU-bound task that never yields is interrupted at an opcode boundary. Cancellation is therefore best-effort: it is not instantaneous, but its latency has a short bound. The same cancellation applies to the promises oxphp_async_await_all() abandons and to the losers of oxphp_async_await_race() / oxphp_async_await_any(). A task that still cannot be interrupted in time is stranded: cancelled, but drained at request end, which can extend RSHUTDOWN by up to a few seconds. Watch oxphp_async_tasks_stranded_total for these.

The few-second bound holds only while the task's code is in OPcache's shared memory. Without OPcache, the closure's code lives in the memory of the request that dispatched the task, and so does the code of a closure that OPcache does not keep in shared memory:

  • code passed to eval()
  • a file OPcache compiled without caching (a blacklisted file, say, or one changed too recently for OPcache to cache it, under opcache.file_update_protection)
  • a file OPcache holds for opcache.file_cache in the request's own memory, read from that cache or compiled for it (with opcache.file_cache_only=1, or while the shared memory is full or restarting)

The request can free that code while the task is still running it, when the request lets go of the closure or when it ends. The end of the request therefore waits for the task to stop, however long that takes, and logs a warning once the few-second budget has run out. With traditional, Framework or SPA routing the response waits with it, unless the script already sent it with oxphp_finish_request(), and a streamed response stays open until the task stops. In Worker mode the response is not held, and what the task captured is released once the task stops, but a worker that is recycled or shut down waits for the task before it exits.

A task that never stops (one that catches the cancellation and keeps going, or one inside a call into C code that does not return) then keeps that PHP worker from finishing until the server is restarted. With traditional, Framework or SPA routing it holds up the end of the request, and in Worker mode the worker's exit.

A task that another task dispatched and did not await is cancelled when that task ends. If the child had started, the parent's result does not reach whoever awaits it until the child has stopped, so the request waits for both. A child that never stops holds its parent's result, and the request with it, the same way. A closure the task creates while it runs still has the reference counts of its code and name changed from the async worker thread, unsynchronised with the request that owns that memory.

Best practices

  • Always set timeouts on oxphp_async_await() calls in production to prevent indefinite waits
  • Use worker mode to get non-blocking fiber-based awaiting instead of blocking the worker thread
  • Keep closures small. Dispatch focused units of work, not entire request handlers
  • Extract scalars before dispatch: pull IDs, strings, and config values out of objects before passing them to the closure
  • Monitor the async pool by checking oxphp_async_tasks_rejected_total in Prometheus metrics. If rejections are rising, increase ASYNC_WORKERS or ASYNC_QUEUE_CAPACITY

Prompts for your agent

These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.

prompt
Move the slow work in my request handlers onto OxPHP's async pool. Read the `features/async-promises` page through the OxPHP documentation MCP server. Pick the calls in this repository that are worth dispatching, rewrite them as closures that carry nothing across the thread boundary but data, and give me the pool size and queue capacity to run them under.

See also

Found a mistake? Report it