cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

http_client sends out request sent *after* connection timeout ?!?

Open blavov opened this issue 7 years ago • 1 comments

Hello all, I've got a problem with the http_client (cpprestsdk 2.9.0, downloaded from the at the beginning of February, running on Ubuntu 14.04.) making http_requests that have already timed out. This results in the operation failing on the client end, but succeeding on the web service, which is a disaster.

I have a server where I want to add to a value to an account that can be reached via a REST/json call (PUT to a specific URL with the value in a json-formatted body); the connection timeout is set to 20 seconds.

This work well under normal conditions, but if the network connection is down (I test this by simply unplugging the ethernet connection to the router on the Linux pc side), then things start going awry. In my test, the timeout is set to 20 seconds (via http_client_config::set_timeout() ), which results in an error displayed on screen when reached (the http_exception I get is "failed to read HTTP status line").

I then replug the connection, wait for the net to come up again, and press "retry" on my display, which calls the same function again. The second try works fine, but upon closer inspection, I see that the account was incremented twice!

Using a packet logger, I can see that the first try is executed immediately and doesn't come very far a the network connection is down. As soon as I replug, however, that very same connection - the same source and destination port and address - retries sending out the packets, this time succeeding. But the http_client.request() already failed five seconds before I replugged the cable, and at the time when those packets go out, I have yet to press the "retry" button, so I haven't even started the manual retry!

The client call looks like this:

myClient::DoPOST()
    {
    json::value requestBody;
    http_request webRequest(methods::POST);
    webRequest.headers().add("Accept","application/json");
    webRequest.headers().add("Language","de");
    webRequest.set_request_uri(SOME_URL);
    // fill the request_body with the appropriate data
    webRequest.set_body(requestBody);
    try {
        pplx::task<http_response> webTask = webRequest.request(httpReq);
        webTask.wait();
        // now that request is over, look at response
        http_response myResponse = webTask.get();
        pplx::task<http_response> finishedResponse = myResponse.content_ready();
        finishedResponse.wait();
        // more result handling
        ... 
    } catch (http_exception &exc) {
        // some error logging
    }

As said above, this routine catches an http_exception "failed to read HTTP status line" on first try and then terminates - but still seems to execute the request a few seconds after the failure and termination.

Is this normal behaviour for the cpprest lib? How can I prevent this? Should I do anything differently? Any help is appreciated, as I don't know how to handle this very strange phenomenon.

Cheers, Pit.

blavov avatar Jun 08 '17 19:06 blavov

I have recently encountered this as well. How did you go about solving your problem?

hawkeyese avatar Dec 17 '21 10:12 hawkeyese