I'm using boost 1.54.0 with OpenSSL 1.0.1e.
When closing SSL connection object from time to time I see that completion handler of async_shutdown() is not called.
After debugging I found out, that this happenes when there's outstaing async_write().
SSL async_shutdown() should send SSL Alert(Closing), thus we have here 2 writes. I know that multiple async_write()'s are forbidden.
How I should handle the situation? Should I wait for async_write() completion before calling SSL async_shutdown()?
EDIT: According to this I probably need cancel() on underlying TCP socket to cancel all outstanding async-operations. Is it correct?
EDIT If I'm using async_ API all the time, can I call shutdown()
or I must call async_shutdown()
?
This is how I handle shutting down. Before this, I was having problems shutting down. This code shuts everything down cleanly:
void SSLSocket::Stop()
{
// This method calls the shutdown method on the socket in order to stop reads or writes that might be going on. If this is not done, then an exception will be thrown
// when it comes time to delete this object.
//
boost::system::error_code EC;
try
{
// This method can be called from the handler as well. So once the ShuttingDown flag is set, don't go throught the same code again.
//
LockCode->Acquire(); // Single thread the code.
if (ShuttingDown)
return;
ShuttingDown = true;
pSocket->next_layer().cancel();
pSocket->shutdown(EC);
if (EC)
{
stringstream ss;
ss << "SSLSocket::Stop: socket shutdown error - " << EC.message() << ".\n";
// Log.LogString(ss.str(), LogError); // Usually get this - probably not an error.
}
else
{
pSocket->next_layer().close();
}
delete pSocket;
pSocket = 0;
ReqAlive = false;
SetEvent(hEvent);
IOService->stop();
LobbySocketOpen = false;
WorkerThreads.join_all();
LockCode->Release();
delete LockCode;
LockCode = 0;
}
catch (std::exception& e)
{
stringstream ss;
ss << "SSLSocket::Stop: threw an error - " << e.what() << ".\n";
Log.LogString(ss.str(), LogError);
Stop();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With