Fixed crash from trying to close a socket that isn't open in the first place.

This commit is contained in:
Eelke Klein 2017-09-02 11:55:47 +02:00
parent aa50d3097e
commit 90d0a14b63

View file

@ -1,4 +1,4 @@
#include "ASyncDBConnection.h" #include "ASyncDBConnection.h"
#include "ScopeGuard.h" #include "ScopeGuard.h"
#include <chrono> #include <chrono>
@ -23,7 +23,7 @@ void ASyncDBConnection::setupConnection(const ConnectionConfig &config)
bool ok = m_connection.connectStart(keywords, values); bool ok = m_connection.connectStart(keywords, values);
// auto start = std::chrono::steady_clock::now(); // auto start = std::chrono::steady_clock::now();
if (ok && m_connection.status() != CONNECTION_BAD) { if (ok && m_connection.status() != CONNECTION_BAD) {
auto sock_handle = m_connection.socket(); auto sock_handle = m_connection.socket();
m_asioSock.assign(ip::tcp::v4(), sock_handle); m_asioSock.assign(ip::tcp::v4(), sock_handle);
m_asioSock.non_blocking(true); m_asioSock.non_blocking(true);
@ -66,15 +66,15 @@ void ASyncDBConnection::async_connect_handler(boost::system::error_code ec, std:
void ASyncDBConnection::doStateCallback(State state) void ASyncDBConnection::doStateCallback(State state)
{ {
m_state = state; m_state = state;
if (state == State::Connected) { if (state == State::Connected) {
m_canceller = m_connection.getCancel(); m_canceller = m_connection.getCancel();
m_connection.setNoticeReceiver( m_connection.setNoticeReceiver(
[this](const PGresult *result) { processNotice(result); }); [this](const PGresult *result) { processNotice(result); });
} }
std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex); std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex);
if (m_stateCallback.m_func) { if (m_stateCallback.m_func) {
m_stateCallback.m_func(state); m_stateCallback.m_func(state);
} }
@ -87,8 +87,10 @@ void ASyncDBConnection::closeConnection()
if (m_state == State::QuerySend) { if (m_state == State::QuerySend) {
m_canceller.cancel(nullptr); m_canceller.cancel(nullptr);
} }
m_asioSock.close(); if (m_state != State::NotConnected) {
m_connection.close(); m_asioSock.close();
m_connection.close();
}
doStateCallback(State::NotConnected); doStateCallback(State::NotConnected);
} }