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:
Lion Kortlepel
2026-04-10 18:09:57 +02:00
parent e9ce71d39a
commit 7089e5da9a
5 changed files with 29 additions and 9 deletions
+3 -4
View File
@@ -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;
}