From b0f192abc4e8bdd9f5f46f6932b8308e32b7b4b6 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 6 May 2020 23:41:25 +0100 Subject: Fix: Racy use of flags in TCPConnecter::CheckCallbacks conected and aborted flags are used concurrently from multiple threads. --- src/network/core/tcp.h | 6 ++++-- src/network/core/tcp_connect.cpp | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index 08a09ca1a..ffa231497 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -15,6 +15,8 @@ #include "address.h" #include "packet.h" +#include + /** The states of sending the packets. */ enum SendPacketsState { SPS_CLOSED, ///< The connection got closed. @@ -61,8 +63,8 @@ public: */ class TCPConnecter { private: - bool connected; ///< Whether we succeeded in making the connection - bool aborted; ///< Whether we bailed out (i.e. connection making failed) + std::atomic connected;///< Whether we succeeded in making the connection + std::atomic aborted; ///< Whether we bailed out (i.e. connection making failed) bool killed; ///< Whether we got killed SOCKET sock; ///< The socket we're connecting with diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp index 9448acb83..b4485cfe9 100644 --- a/src/network/core/tcp_connect.cpp +++ b/src/network/core/tcp_connect.cpp @@ -66,19 +66,21 @@ void TCPConnecter::Connect() { for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) { TCPConnecter *cur = *iter; - if ((cur->connected || cur->aborted) && cur->killed) { + const bool connected = cur->connected.load(); + const bool aborted = cur->aborted.load(); + if ((connected || aborted) && cur->killed) { iter = _tcp_connecters.erase(iter); if (cur->sock != INVALID_SOCKET) closesocket(cur->sock); delete cur; continue; } - if (cur->connected) { + if (connected) { iter = _tcp_connecters.erase(iter); cur->OnConnect(cur->sock); delete cur; continue; } - if (cur->aborted) { + if (aborted) { iter = _tcp_connecters.erase(iter); cur->OnFailure(); delete cur; -- cgit v1.2.3-54-g00ecf