cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

Unexpected end of request body stream encountered before Content-Length satisfied.

Open glukacsy opened this issue 10 years ago • 9 comments

Hi,

Noticed that on OS X if we reuse an HTTP client for multiple requests in a quick succession we got the above error message and the request fails.

The way I am handling this today is that if this happens we blow away the shared http_client object, recreate and retry again (in which case there is not issue).

Any idea? It's pretty reproducible.

Regards, Gergely PS: Earlier I opened an issue about http_client reuse on OS X and proxies (which seems to be completely broken), the above is happening without proxies

glukacsy avatar Jun 11 '16 14:06 glukacsy

Second try....

glukacsy avatar Jun 26 '16 15:06 glukacsy

Could you post a minimal repro? I want to know exactly how you are reusing the client (is it launching two parallel requests, is it launching one request then launching a second in the handler for the first, etc).

ras0219-msft avatar Jun 27 '16 23:06 ras0219-msft

Apologies for the late reply, I just picked this up again.

So here is a minimal(ist) report which shows that something is wrong when reusing clients:

    http_client_config httpConfig;
    std::shared_ptr<http_client> downloadClient = std::make_shared<http_client>("http://www.example.org", httpConfig);

    for (unsigned int i = 0; i < 100; ++i)
    {
        pplx::create_task([this, i, downloadClient]()
        {
            // sleep a bit to simulate a production environment where requests come in randomly
            std::random_device rd;
            std::mt19937 generator(rd());
            std::uniform_int_distribution<> dis(1, 10);
            auto offset = dis(generator);
            std::this_thread::sleep_for(std::chrono::milliseconds(offset));

            // make request
            http_request httpRequest(methods::GET);
            httpRequest.set_request_uri("/index.html");

            http_client_config httpConfig1;
            std::shared_ptr<http_client> downloadClient1 = std::make_shared<http_client>("http://www.example.org", httpConfig1);

            downloadClient->request(httpRequest).then([](pplx::task<http_response> responseTask) // change to downloadClient1 and you are good
            {
                try
                {
                    http_response response = responseTask.get();
                }
                catch (const http_exception& e)
                {
                    SPARK_LOG_ERROR("Failed to make request, error: " << e.what()); // whoops: various errors
                }
            });
        });
    }

glukacsy avatar Aug 30 '16 00:08 glukacsy

Bump for @ras0219-msft

glukacsy avatar Sep 12 '16 21:09 glukacsy

The same issue.

3Hren avatar Mar 22 '17 10:03 3Hren

The same issue by using http_client singleton in multi-threading. Code to send request:

void UmbrellaApiClient::SendRestRequest(http_request& request, ReportCallback callback)
{
    status_code code;
    std::string requestString = std::string(request.to_string().c_str());

    try
    {
        m_client.request(request).then([&code](http_response response) {
            code = response.status_code();
            return response.extract_json();
        }).then([requestString, &code, callback](pplx::task<json::value> previousTask) {
            try
            {
                json::value jvalue = previousTask.get();

                if (callback)
                    callback(jvalue, code);

                utility::stringstream_t ss;
                jvalue.serialize(ss);

                sLog.outInfo("UmbrellaApiClient::SendRestRequest %s => result: %s", requestString.c_str(), ss.str().c_str());
            }
            catch (http_exception const & e)
            {
                if (callback)
                    callback(json::value::null(), 0);
                sLog.outError("UmbrellaApiClient::SendRestRequest %s => occur error: %s", requestString.c_str(), e.what());
            }
        }).wait();
    }
    catch (const std::exception &e)
    {
        sLog.outError("UmbrellaApiClient::SendRestRequest %s => occur error with(std::exception): %s", requestString.c_str(), e.what());
    }
}

Bump for @ras0219-msft

The log:

UmbrellaApiClient::SendRestRequest POST /iot/xx/borrow_successful HTTP/1.1
Accept: application/json
Content-Length: 70
Content-Type: application/json

{"dock_device_id":"8x26x02250x33x2","umbrella_device_id":"18x50x3x91"} => occur error: Unexpected end of request body stream encountered before Content-Length satisfied.

ryancheung avatar Sep 06 '17 07:09 ryancheung

The same issue on Ubuntu 18.04.1 Desktop (64-bit).

emptyVoid avatar Feb 01 '19 05:02 emptyVoid

PING: The same issue on Ubuntu 18.04.1 Desktop (64-bit).

xann16 avatar Feb 21 '19 12:02 xann16

I'm using v2.9.0.

I observed this exception all the time in the following scenario. Posting it here so that it may help in debugging the issue.

  1. A HTTP request is sent using http_client object. The HTTP request has Connection: Keep-Alive header set.
  2. HTTP Server sends 200 OK response without Connection header and immediately closes the TCP connection by sending a FIN
  3. http_client acknowledges FIN with ACK, but doesn't close the TCP connection from it's end. By this time, the TCP connection is closed from server's end and is only half duplex.
  4. Another HTTP request is sent using the http_client object. Rather than opening a new TCP connection, client reuses the existing half duplex connection to send the HTTP request. Server sends RST to force close the connection. An exception "Unexpected end of request body stream encountered before Content-Length satisfied" is seen on the client side.

Somehow reusing a half duplex connection from client side seems wrong to me. Server cannot anyway send the response as the connection is closed from its end. Why does client reuse the existing TCP connection ?

srini-ar avatar Apr 26 '19 06:04 srini-ar