Fix: always check before closing TCP handles

pull/27/head
SChernykh 2021-09-13 19:11:59 +02:00
parent 4c5144b37f
commit 14e26f5c17
2 changed files with 16 additions and 5 deletions

View File

@ -216,8 +216,10 @@ void JSONRPCRequest::on_read(const char* data, size_t size)
void JSONRPCRequest::close()
{
uv_tcp_t* s = &m_socket;
uv_close(reinterpret_cast<uv_handle_t*>(s), on_close);
uv_handle_t* h = reinterpret_cast<uv_handle_t*>(&m_socket);
if (!uv_is_closing(h)) {
uv_close(h, on_close);
}
}
void JSONRPCRequest::on_close(uv_handle_t* handle)

View File

@ -385,10 +385,16 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::close_sockets(bool listen_sockets
if (listen_sockets) {
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_handle_t* h = reinterpret_cast<uv_handle_t*>(s);
if (!uv_is_closing(h)) {
uv_close(h, [](uv_handle_t* h) { delete reinterpret_cast<uv_tcp_t*>(h); });
}
}
for (uv_tcp_t* s : m_listenSockets) {
uv_close(reinterpret_cast<uv_handle_t*>(s), [](uv_handle_t* h) { delete reinterpret_cast<uv_tcp_t*>(h); });
uv_handle_t* h = reinterpret_cast<uv_handle_t*>(s);
if (!uv_is_closing(h)) {
uv_close(h, [](uv_handle_t* h) { delete reinterpret_cast<uv_tcp_t*>(h); });
}
}
}
@ -621,7 +627,10 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connect(uv_connect_t* req, int
LOGWARN(5, "failed to connect to " << static_cast<char*>(client->m_addrString) << ", error " << uv_err_name(status));
}
server->on_connect_failed(client->m_isV6, client->m_addr, client->m_port);
uv_close(reinterpret_cast<uv_handle_t*>(&client->m_socket), nullptr);
uv_handle_t* h = reinterpret_cast<uv_handle_t*>(&client->m_socket);
if (!uv_is_closing(h)) {
uv_close(h, nullptr);
}
server->m_preallocatedClients.push_back(client);
return;
}