04-17-2022, 02:37 AM
I'm having an issue trying to terminate Indy threads from rogue connections on my game server's TCP port which I use for a WebSocket connection. In the OnConnect event I read headers required to establish a valid websocket and I have ReadTimeout set to 10000 so it doesn't get stuck there from bots that are just probing the port. If I get valid headers I assign an object to AContext.Data, otherwise I just Exit and disconnect them in the OnExecute event. I use madExcept's NameThread command along to way to track the progress of the connection. Here is my OnExecute event:
Notice I update the thread name right before disconnecting. And they do disconnect because when I run TCPView on my server, no one is connected to me. But those rogue threads never terminate and they build up over time and never stop unless I take the game server offline. And I know they don't terminate because my app has a function that retrieves all the live threads assigned to my app. I then use madExcept's GetThreadName command to get the names I assigned to them. Here's what that looks like:
[attachment=481]
Most of those IPs are from Russia. Why are these threads not terminating after the disconnect?
By the way, this is my OnDisconnect event, which should just Exit immediately for these rogue connections:
Code:
class procedure WSServerEvents.OnExecute(AContext: TIdContext);
var st: string;
begin
Sleep(10); // prevent CPU max
try
if Assigned(AContext.Data) = False then
begin
MadExcept.NameThread(GetCurrentThreadID, 'WebSocket Rogue (' + AContext.Binding.PeerIP + ')');
AContext.Connection.Disconnect; // rogue connection
end else
begin
if TConnectRec(AContext.Data).isAdmin then AdminSessionExecute(AContext)
else PlayerSessionExecute(AContext);
end;
except
on E: Exception do
begin
if E is EIdException then raise else
begin
LogData.AddError('WSServerExecute error: ' + E.Message);
st := StringReplace(madExcept.GetCrashStackTrace, #13#10, ' - ', [rfReplaceAll]);
LogData.AddError('WSServerExecute stack trace: ' + st);
raise;
end;
end;
end;
end;
Notice I update the thread name right before disconnecting. And they do disconnect because when I run TCPView on my server, no one is connected to me. But those rogue threads never terminate and they build up over time and never stop unless I take the game server offline. And I know they don't terminate because my app has a function that retrieves all the live threads assigned to my app. I then use madExcept's GetThreadName command to get the names I assigned to them. Here's what that looks like:
[attachment=481]
Most of those IPs are from Russia. Why are these threads not terminating after the disconnect?
By the way, this is my OnDisconnect event, which should just Exit immediately for these rogue connections:
Code:
class procedure WSServerEvents.OnDisconnect(AContext: TIdContext);
begin
if Assigned(AContext.Data) = False then Exit;
try
if TConnectRec(AContext.Data).isAdmin then AdminSessionDisconnect(AContext)
else PlayerSessionDisconnect(AContext);
except
on E: Exception do
begin
if E is EIdException then raise else
begin
LogData.AddError('WSServerDisconnect error: ' + E.Message);
raise;
end;
end;
end;
end;