Skip to content

cURL: Defer redirects in request_multiple() until all transfers finished - #1117

Draft
swissspidy wants to merge 1 commit into
WordPress:developfrom
swissspidy:fix/curl-multi-redirects
Draft

swissspidy wants to merge 1 commit into
WordPress:developfrom
swissspidy:fix/curl-multi-redirects

Conversation

@swissspidy

Copy link
Copy Markdown
Member

Pull Request Type

  • I have checked there is no other PR open for the same change.

This is a:

  • Bug fix
  • New feature
  • Documentation improvement
  • Code quality improvement

Context

In Transport\Curl::request_multiple(), a response is parsed as soon as its transfer finishes. Parsing a redirect follows it with a new, blocking request, from inside the multi loop. While that request runs, the other transfers of the batch are not serviced, but their timeouts keep running. They can then time out although their server answered in time.

In a local test, a request to /delay/1 with a 2-second timeout failed with "Operation timed out after 4001 milliseconds with 0 bytes received", because the other request in the batch redirected to /delay/3. With this change, it succeeds.

Found while reviewing concurrent requests for WordPress core: https://core.trac.wordpress.org/ticket/37459 and WordPress/wordpress-develop#13657. This is independent of the busy-loop fix in #1116, and the two branches merge cleanly.

Changelog: a redirect in a Requests::request_multiple() batch no longer makes the other requests of the batch time out when using cURL.

Detailed Description

  • Responses that will be redirected are now set aside and parsed once all transfers of the batch have finished. "Will be redirected" uses the same conditions as Requests::parse_response(): follow_redirects is enabled, the request is blocking, and the status is 3xx other than 304, 305 and 306. Their multiple.request.complete hook fires at that point too.
  • Trade-off: a redirect is now followed only after all transfers of the batch have finished. In the test above, the batch took 4.0 s instead of 3.0 s. Redirects are still followed one at a time, as before.
  • The loop now stops when curl_multi_exec() returns an error status, instead of looping forever. Each unfinished transfer gets a CurlException (type CurlException::MULTI), and the transport.internal.parse_error and multiple.request.complete hooks fire for it.

Quality assurance

  • This change does NOT contain a breaking change (fix or feature that would cause existing functionality to change).
    • No API changes, but redirects in a batch are now followed later, as described above.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added unit tests to accompany this PR.
    • testMultipleRedirectDoesNotStallOtherRequests (in BaseTestCase, so it runs for both transports) uses the test server's /redirect-to and /delay/n endpoints. It fails for cURL without the fix and passes with it.
  • The (new/existing) tests cover this PR 100%.
    • The error-status branch is not covered. I couldn't find a way to make curl_multi_exec() return an error from a test on current PHP.
  • I have (manually) tested this code to the best of my abilities.
    • Full suite run locally on PHP 8.3 with libcurl 8.5 against the local test server. The only failures were ones that also fail on develop in that environment: it runs as root, and HTTPS goes through an intercepting proxy.
  • My code follows the style guidelines of this project.

Documentation

For new features:

  • I have added a code example showing how to use this feature to the examples directory.
  • I have added documentation about this feature to the docs directory.

AI disclosure: this change was prepared with Claude Code.

Responses were parsed as soon as their transfer finished, and parsing a
redirect follows it with a new, blocking request. While that request
ran, the other transfers of the batch were not serviced, but their
timeouts kept running, so they could time out although their server had
answered in time. Parse the responses that will be redirected once all
transfers have finished instead. The redirects themselves are still
followed one after the other, as before.

Also stop instead of looping forever when `curl_multi_exec()` reports an
error, and report the error for each unfinished transfer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5orPkSjWrCmXNcVAbH

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant