TCPServer: fixed more leaks on shutdown

This commit is contained in:
SChernykh 2021-08-26 19:14:04 +02:00
parent 4ba32277c1
commit 27e85a922b

View file

@ -15,6 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <thread>
static thread_local bool server_event_loop_thread = false; static thread_local bool server_event_loop_thread = false;
namespace p2pool { namespace p2pool {
@ -61,6 +63,7 @@ TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::~TCPServer()
LOGERR(1, "TCP wasn't shutdown properly"); LOGERR(1, "TCP wasn't shutdown properly");
shutdown_tcp(); shutdown_tcp();
} }
delete m_connectedClientsList;
} }
@ -384,8 +387,6 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::shutdown_tcp()
return; return;
} }
drop_connections();
for (uv_tcp_t* s : m_listenSockets6) { for (uv_tcp_t* s : m_listenSockets6) {
uv_close(reinterpret_cast<uv_handle_t*>(s), [](uv_handle_t* h) { delete reinterpret_cast<uv_tcp_t*>(h); }); uv_close(reinterpret_cast<uv_handle_t*>(s), [](uv_handle_t* h) { delete reinterpret_cast<uv_tcp_t*>(h); });
} }
@ -394,6 +395,21 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::shutdown_tcp()
uv_close(reinterpret_cast<uv_handle_t*>(s), [](uv_handle_t* h) { delete reinterpret_cast<uv_tcp_t*>(h); }); uv_close(reinterpret_cast<uv_handle_t*>(s), [](uv_handle_t* h) { delete reinterpret_cast<uv_tcp_t*>(h); });
} }
drop_connections();
// Give it 1 second to gracefully close connections
using namespace std::chrono;
const system_clock::time_point start_time = system_clock::now();
volatile uint32_t* n = &m_numConnections;
while (*n > 0) {
if (duration_cast<milliseconds>(system_clock::now() - start_time).count() >= 1000) {
break;
}
std::this_thread::sleep_for(milliseconds(1));
}
uv_async_t asy; uv_async_t asy;
uv_async_init(&m_loop, &asy, NULL); uv_async_init(&m_loop, &asy, NULL);
uv_stop(&m_loop); uv_stop(&m_loop);
@ -542,9 +558,7 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connection_close(uv_handle_t*
} }
} }
else { else {
LOGERR(5, "internal error: can't find TCPServer instance for peer " << log::Gray() << static_cast<char*>(client->m_addrString) << ", deallocating it"); LOGERR(5, "internal error: can't find TCPServer instance for peer " << log::Gray() << static_cast<char*>(client->m_addrString) << ", this will leak memory");
client->reset();
delete client;
} }
} }
@ -730,6 +744,7 @@ TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::~Client()
} }
} }
uv_mutex_destroy(&m_writeBuffersLock); uv_mutex_destroy(&m_writeBuffersLock);
uv_mutex_destroy(&m_sendLock);
} }
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE> template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>