Reputation: 845
I want to send some https requests to the same endpoint concurrently using Poco (1.6.0), but I am constantly getting exceptions with the code I have done. (However, the same code using HTTP requests works perfectly...)
Here is the code I use:
Poco::Net::initializeSSL();
try {
Poco::URI ep("https://stackoverflow.com/");
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrHandler
= new Poco::Net::AcceptCertificateHandler(false);
Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE,
"", "", "", Poco::Net::Context::VERIFY_STRICT,
9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Poco::Net::SSLManager::instance().initializeClient(0, ptrHandler, context);
for (int i = 0; i < 20; ++i)
{
// Start an asynchronous HTTPS request
std::future<bool> f = std::async(std::launch::async, []() -> bool {
try {
Poco::URI ep("https://github.com/");
std::unique_ptr<Poco::Net::HTTPSClientSession> session(
new Poco::Net::HTTPSClientSession (ep.getHost(), ep.getPort()) );
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET,
ep.getPathAndQuery() ,
Poco::Net::HTTPMessage::HTTP_1_1);
request.write(std::cerr);
try {
session->sendRequest(request);
}
catch (const Poco::Exception& e) {
std::cerr << "Error A" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
Poco::Net::HTTPResponse response;
try {
session->receiveResponse(response);
}
catch (const Poco::Exception& e) {
std::cerr << "Error B" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
response.write(std::cerr);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cerr << "Async request ended" << std::endl;
}
catch (const Poco::Exception& e) {
std::cerr << "Error :-/" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
std::cerr << "Async request ended 2" << std::endl;
return true;
});
// Generate several HTTPS requests until the async request is completed
std::chrono::microseconds span (10);
while (f.wait_for(span) != std::future_status::ready)
{
std::unique_ptr<Poco::Net::HTTPSClientSession> session(
new Poco::Net::HTTPSClientSession (ep.getHost(), ep.getPort()) );
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET,
ep.getPathAndQuery() ,
Poco::Net::HTTPMessage::HTTP_1_1);
request.write(std::cerr);
try {
session->sendRequest(request);
}
catch (const Poco::Exception& e) {
std::cerr << "Error 1" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
Poco::Net::HTTPResponse response;
try {
session->receiveResponse(response);
}
catch (const Poco::Exception& e) {
std::cerr << "Error 2" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
throw;
}
response.write(std::cerr);
}
f.get();
}
}
catch (const Poco::Exception& e) {
std::cerr << "ERROR :-(" << std::endl;
std::cerr << e.code() << " - " << e.displayText() << std::endl;
}
Poco::Net::uninitializeSSL();
This is the issue I got:
When I run the code in QtCreator debugger, I got the following one: Once the async request ends, the current request in the main thread throw a Poco exception.
Error 1
4 - I/O error: Interrupted
However, this seems not to appear outside the QtCreator debugger. This is a bit strange.
This is probably a concurrency issue, but I could not find what I am doing wrong and I didn't see any similar issue on the Internet. Could you please help me to find what is my mistake?
Thank you.
EDIT: Simplified the minimal code and added a few details in the description of the issue.
EDIT2: It seems I forgot to mention the most important information, the environment under which I get the issue. It is the following: Ubuntu 14.04 LTS 64bit with gcc 4.8.4, gdb 7.7.1, openssl 1.0.1f
Upvotes: 1
Views: 1204
Reputation: 845
The issue comes from a too old version of GDB. In its 7.9 version, GDB comes with numerous improvements related to multithread debugging and can be used to debug this code without any issue. An older version of GDB will fail with this code.
Upvotes: 1