From eaab5eaf301416b68d99664a5e61a57f1c2c7dfa Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 14 Nov 2022 23:37:54 +0100 Subject: [PATCH] fix crash which could happen when closing an invalid socket for some reason boost::asio doesn't account for this and lets us simply die when/if this happens. --- include/Client.h | 1 + src/Client.cpp | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/include/Client.h b/include/Client.h index ba09541..fd239a1 100644 --- a/include/Client.h +++ b/include/Client.h @@ -127,6 +127,7 @@ private: std::string mDID; int mID = -1; std::chrono::time_point mLastPingTime; + std::mutex mDisconnectMtx; }; // Returns a valid client, or nullptr if no such client exists diff --git a/src/Client.cpp b/src/Client.cpp index 02f8eb5..be36117 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -59,7 +59,13 @@ std::string TClient::GetCarPositionRaw(int Ident) { } void TClient::Disconnect(std::string_view Reason) { + // we need exclusivity here in case we can't + std::unique_lock Lock(mDisconnectMtx); beammp_debugf("Disconnecting client {} for reason: {}", GetID(), Reason); + if (mSocket.is_open()) { + beammp_debug("Somehow Disconnect() was called twice, ignoring"); + return; + } boost::system::error_code ec; mSocket.shutdown(socket_base::shutdown_both, ec); if (ec) {