diff --git a/src/tcp_server.h b/src/tcp_server.h index 4dc8231..82fd624 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -83,6 +83,7 @@ public: bool m_isV6; bool m_isIncoming; bool m_readBufInUse; + bool m_isClosing; uint32_t m_numRead; raw_ip m_addr; diff --git a/src/tcp_server.inl b/src/tcp_server.inl index a2c730d..7c6e1c3 100644 --- a/src/tcp_server.inl +++ b/src/tcp_server.inl @@ -556,6 +556,11 @@ bool TCPServer::send_internal(Client* client, Sen LOGERR(1, "sending data from another thread, this is not thread safe"); } + if (client->m_isClosing) { + LOGWARN(5, "client " << static_cast(client->m_addrString) << " is being disconnected, can't send any more data"); + return true; + } + WriteBuf* buf = nullptr; { @@ -845,6 +850,7 @@ TCPServer::Client::Client() , m_isV6(false) , m_isIncoming(false) , m_readBufInUse(false) + , m_isClosing(false) , m_numRead(0) , m_addr{} , m_port(0) @@ -867,6 +873,7 @@ void TCPServer::Client::reset() m_isV6 = false; m_isIncoming = false; m_readBufInUse = false; + m_isClosing = false; m_numRead = 0; m_addr = {}; m_port = -1; @@ -903,6 +910,11 @@ void TCPServer::Client::on_read(uv_stream_t* stre Client* pThis = static_cast(stream->data); pThis->m_readBufInUse = false; + if (pThis->m_isClosing) { + LOGWARN(5, "client " << static_cast(pThis->m_addrString) << " is being disconnected but data received from it, nread = " << nread << ". Ignoring it."); + return; + } + if (nread > 0) { if (pThis->m_owner && !pThis->m_owner->m_finished.load()) { if (!pThis->on_read(buf->base, static_cast(nread))) { @@ -944,11 +956,13 @@ void TCPServer::Client::on_write(uv_write_t* req, template void TCPServer::Client::close() { - if (!m_owner) { + if (m_isClosing || !m_owner) { // Already closed return; } + m_isClosing = true; + uv_read_stop(reinterpret_cast(&m_socket)); uv_tcp_t* s = &m_socket;