cURL Under Runtime Hooks
The curl category of RUNTIME_HOOKS makes the waits of a cURL transfer suspend the current fiber. That covers every HTTP client built on ext/curl: Guzzle with its default handler, Symfony HttpClient's CurlHttpClient, and the SDKs built on either. It is included in 1, true and all, needs neither streams nor net, and works on Linux only, with libcurl loaded as a shared library as in the official images. When net or curl stays off lists the cases where it does not install.
cURL transfers
With curl listed, curl_exec() suspends the fiber for everything it waits on: connecting, the TLS handshake, sending the request and waiting for the answer. curl_multi_select() suspends it for its wait. Resolving the host name is included where libcurl resolves in the background, as the official images' libcurl does with c-ares; a libcurl that resolves on the calling thread holds the thread for it.
curl_multi_exec() never waits: it moves every transfer as far as it can and returns. A loop of curl_multi_exec() and curl_multi_select(), the usual way to run transfers side by side, suspends in the second call. A wait libcurl makes outside the calls listed on this page, such as while a handle is destroyed, keeps the thread.
Cancelling a task in a transfer
Cancelling an async task suspended in a transfer throws OxPHP\Async\AsyncException from the cURL call it is in. The transfer ends, and the handle can run the next one:
- an HTTP transfer in
curl_exec()leavescurl_errno()atCURLE_BAD_FUNCTION_ARGUMENT(43); - a
curl_multi_select()leavescurl_multi_errno()atCURLM_UNRECOVERABLE_POLL(12); - a protocol that waits inside its own command exchange, such as FTP, can report an error of its own.
A cancellation that arrives while a callback of a curl_exec() transfer waits (in a hooked usleep(), say) is thrown there, and the transfer ends with the same error the next time it waits. In a callback that curl_multi_exec() runs it is thrown too, but the transfer stays in the multi handle, and the loop's next curl_multi_select() waits on the thread. A transfer the task makes after the cancellation, in a finally block say, waits on the thread as it does without the hook.
Sharing a handle between fibers
In worker mode a handle kept for the life of the worker, in a service or a static property, can be reached by every request on the worker, and with the hook a request can be suspended in the middle of a call on it. A second call on that handle meanwhile would be refused by libcurl (CURLE_RECURSIVE_API_CALL, CURLM_RECURSIVE_API_CALL) or would corrupt the transfer: curl_exec() empties the CURLOPT_RETURNTRANSFER buffer the answer is being written to, and curl_setopt() frees a CURLOPT_POSTFIELDS body that may still be being sent.
So these calls claim the handles they are given until they return, and the same calls from another fiber wait:
curl_exec(),curl_setopt(),curl_setopt_array(),curl_reset(),curl_pause()andcurl_upkeep();curl_multi_exec(),curl_multi_select(),curl_multi_setopt()andcurl_multi_close();curl_multi_add_handle()andcurl_multi_remove_handle(), which claim the easy handle and the multi handle together.
A call from one of the transfer's own callbacks, on a handle that transfer holds, goes ahead. Calls that only read a handle never wait: curl_getinfo(), curl_errno(), curl_error(), curl_multi_getcontent() and curl_multi_info_read(). Neither does a call made outside any fiber, such as a destructor the cycle collector runs between requests; it ignores other fibers' claims, so it should not touch a handle the worker's requests share.
The wait is bounded like the wait for a shared database connection: by the smaller of max_execution_time and default_socket_timeout, 30 seconds with neither set. A call whose handle is not free within that bound is not made. Neither is a call where the fiber cannot be suspended (inside a fiber a userland scheduler started, or anywhere the sleep hooks would not suspend). Such a call throws OxPHP\Async\AsyncException with a message naming the reason, and the server log records it at most once a second per worker thread. It throws instead of returning false, because an HTTP client reads false as a failed transfer and may retry it on the same handle.
A handle one fiber uses on its own is never held up: an easy handle created for the transfer, or taken from a pool and put back afterwards. Where a handle is shared, its calls run one after another, as they do without the hook.
Multi handles shared between requests
A loop of curl_multi_exec() and curl_multi_select() holds the multi handle while it is suspended in curl_multi_select(). A call from another request is let in between two of the loop's calls, and the loop's next call waits for it.
- A waiting call other than
curl_multi_exec()andcurl_multi_select()(curl_multi_add_handle(), say) ends the loop'scurl_multi_select()early, which then returns0as if its timeout had run out. A loop that counts its zero returns as elapsed time, instead of reading the clock, is already wrong, since a signal does the same. Guzzle and Symfony HttpClient read the clock. - Another request's
curl_multi_exec()orcurl_multi_select()waits until its timeout or until any transfer in the handle has data. The nextcurl_multi_exec(), whichever request makes it, moves them all.
Waiting calls on one handle go in the order they started to wait. One that also waits for a handle held elsewhere is passed by those that can go.
Guzzle keeps one multi handle per client for its asynchronous requests (requestAsync(), Pool), and Symfony's CurlHttpClient for all of them. Both pass curl_multi_select() a timeout of at most a second, unless Guzzle's select_timeout option (or the deprecated GUZZLE_CURL_SELECT_TIMEOUT environment variable, read when the option is absent) is set higher. In worker mode, where such a client is usually a shared service, each request adds its transfers while the others' are under way, and their transfers run side by side.
Symfony's CurlHttpClient breaks under this. While one call drives its transfers (CurlResponse::perform()), the client keeps a flag up, and a call that arrives meanwhile is taken for a callback reentering the client: the response it was made for fails with TransportException: Userland callback cannot use the client nor the response while processing "…". Without the hook a request never suspends inside perform(). With it, the curl_multi_exec() there can wait its turn on the multi handle and suspend the request with the flag still up, and another request on the worker that reads a response from the same client then fails. Where requests share a CurlHttpClient (in a Symfony app, the http_client service when it runs on ext/curl), leave curl out of the categories: RUNTIME_HOOKS=sleep,streams,net, not all, 1 or true.
Two things to watch:
- The loop's calls wait too, for a call another request is in. A loop that passes
curl_multi_select()a timeout longer than the bound can make another request'scurl_multi_exec()orcurl_multi_select()throw. - A loop inside a fiber a userland scheduler started cannot stop to let a call in, so the waiting call waits until the loop ends.
curl_pause() cannot release a transfer another fiber runs with curl_exec(): it waits for curl_exec() to return, which a paused transfer never does, until the bound runs out. Unpause from the transfer's own callbacks, or run the transfer in a multi handle, where curl_pause() waits only for a call in progress on it.
Easy handles in a multi handle
An easy handle added to a multi handle is held by the request or async task that added it until it is out again: removed with curl_multi_remove_handle() by any request, or out because the multi handle was closed or freed, or because the request or task that added it ended. A call on the handle from anywhere else waits for that, within the same bound.
Some calls do not wait for that hold, though they still wait for a call in progress on the multi handle:
curl_multi_remove_handle();curl_pause();curl_setopt()ofCURLOPT_PRIVATEorCURLOPT_VERBOSE, which Symfony HttpClient makes on other requests' transfers.
The transfer's callbacks run in whichever request drives the multi handle, or in the request whose curl_pause() releases data held back for a paused transfer. Their calls on the transfer's handle go ahead. A handle a server push adds (CURLMOPT_PUSHFUNCTION) is not held, since it arrives without a curl_multi_add_handle().
Share handles
A share handle (curl_share_init(), and curl_share_init_persistent() on PHP 8.5) is not claimed. Transfers on handles that share one take turns on the worker thread, which is how libcurl expects a share handle to be used when no lock callbacks are set, and ext/curl sets none.
Prompts for your agent
These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.
Check this repository's HTTP client code for problems under OxPHP's `RUNTIME_HOOKS=curl`. Read the `features/runtime-hooks-curl` page through the OxPHP documentation MCP server.
Find every Guzzle, Symfony HttpClient or raw `curl_*` handle that lives longer than one request, and tell me which requests would share it, whether a call on it can now throw `OxPHP\Async\AsyncException`, and whether any `curl_multi_select()` timeout or `curl_pause()` use needs changing. Flag first any Symfony `CurlHttpClient` that requests share: under the `curl` category one of two requests that use it at once fails. Show me the fixes.See also
- Runtime Hooks: all hook categories and where they do not suspend
- Shared Connections Under Runtime Hooks: the same rules for a database or Redis connection
- Async Promises: cancelling a task suspended in a transfer