Skip to content

blocking => false not working #826

@peixotorms

Description

@peixotorms

Summary

When I set blocking => false, the request is still blocking.

Given the following code sample

# make options
    $options = array(
        'timeout' => 60,
        'connect_timeout' => 10,
        'blocking' => false
    );

# request
$response = WpOrg\Requests\Requests::request($url, $headers, $data, POST, $options);

Unfortunately, if I set a short timeout like 1 second, the remote API call will not complete.
I don't need the response, only for it to run in the background while the script does something else.

I'd expect the following behaviour

I would expect it to return immediately, so the rest of the script can run.

Instead this happened

With a long API call lasting over 1 minute, the script hangs until it returns a reply.

Additional context

The way around this for non blocking requests is to use curl with CURLOPT_NOSIGNAL set to 1.
This let's me timeout early and the api call will complete properly.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1); // Set timeout to 1 second
curl_setopt($ch, CURLOPT_NOSIGNAL, 1); // Ignore all cURL signals to ensure script continues 
curl_exec($ch);
curl_close($ch);

Your environment

Environment Answer
Operating system and version: Ubuntu 22.04
PHP version 8.2 lsapi, openlitespeed
Requests version v2.0.7

Tested against develop branch?

no

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions