mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-07-13 02:03:44 +00:00
fix race on disconnect
between the time we check for `is_open` and the actual disconnect, the socket could already have been disconnected by another thread (TOCTOU). Furthermore, the disconnects can race causing a segfault or similar issue in the asio's internals.
This commit is contained in:
+10
-1
@@ -76,7 +76,13 @@ std::string TClient::GetCarPositionRaw(int Ident) {
|
||||
}
|
||||
}
|
||||
|
||||
void TClient::Disconnect(std::string_view Reason) {
|
||||
bool TClient::Disconnect(std::string_view Reason) {
|
||||
// Do not remove this guard: concurrent close() on the same socket can crash in Asio internals.
|
||||
EDisconnectState Expected = EDisconnectState::Connected;
|
||||
if (!mDisconnectState.compare_exchange_strong(Expected, EDisconnectState::Disconnecting, std::memory_order_acq_rel, std::memory_order_acquire)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
beammp_debugf("Disconnecting client {} for reason: {}", GetID(), Reason);
|
||||
boost::system::error_code ec;
|
||||
if (mSocket.is_open()) {
|
||||
@@ -91,6 +97,9 @@ void TClient::Disconnect(std::string_view Reason) {
|
||||
} else {
|
||||
beammp_debug("Socket is already closed.");
|
||||
}
|
||||
// Terminal state: this client must not perform TCP IO after this point.
|
||||
mDisconnectState.store(EDisconnectState::Disconnected, std::memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
void TClient::SetCarPosition(int Ident, const std::string& Data) {
|
||||
|
||||
+3
-4
@@ -697,9 +697,8 @@ void TNetwork::DisconnectClient(const std::weak_ptr<TClient>& c, const std::stri
|
||||
}
|
||||
|
||||
void TNetwork::DisconnectClient(TClient& c, const std::string& R) {
|
||||
if (c.IsDisconnected())
|
||||
return;
|
||||
c.Disconnect(R);
|
||||
// Keep this unconditional; TClient::Disconnect() is the single-winner guard.
|
||||
(void)c.Disconnect(R);
|
||||
}
|
||||
|
||||
void TNetwork::Looper(const std::weak_ptr<TClient>& c) {
|
||||
@@ -740,7 +739,7 @@ void TNetwork::Looper(const std::weak_ptr<TClient>& c) {
|
||||
|
||||
void TNetwork::TCPClient(const std::weak_ptr<TClient>& c) {
|
||||
// TODO: the c.expired() might cause issues here, remove if you end up here with your debugger
|
||||
if (c.expired() || !c.lock()->GetTCPSock().is_open()) {
|
||||
if (c.expired() || c.lock()->IsDisconnected()) {
|
||||
mServer.RemoveClient(c);
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -76,7 +76,7 @@ void TPPSMonitor::operator()() {
|
||||
return true;
|
||||
});
|
||||
for (auto& ClientToKick : TimedOutClients) {
|
||||
ClientToKick->Disconnect("Timeout");
|
||||
Network().DisconnectClient(*ClientToKick, "Timeout");
|
||||
}
|
||||
TimedOutClients.clear();
|
||||
if (C == 0 || mInternalPPS == 0) {
|
||||
|
||||
+1
-1
@@ -224,7 +224,7 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
|
||||
case 'p':
|
||||
if (!Network.Respond(*LockedClient, StringToVector("p"), false)) {
|
||||
// failed to send
|
||||
LockedClient->Disconnect("Failed to send ping");
|
||||
Network.DisconnectClient(*LockedClient, "Failed to send ping");
|
||||
} else {
|
||||
Network.UpdatePlayer(*LockedClient);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user