Unexpected end of request body stream encountered before Content-Length satisfied.
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
Second try....
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).
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
}
});
});
}
Bump for @ras0219-msft
The same issue.
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.
The same issue on Ubuntu 18.04.1 Desktop (64-bit).
PING: The same issue on Ubuntu 18.04.1 Desktop (64-bit).
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.
- A HTTP request is sent using http_client object. The HTTP request has Connection: Keep-Alive header set.
- HTTP Server sends 200 OK response without Connection header and immediately closes the TCP connection by sending a FIN
- 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.
- 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 ?