Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Disconnected TCPServer thread won't terminate
#11
(04-19-2022, 06:36 PM)kbriggs Wrote: I moved the disconnect to OnConnect like this:
...
The thread stayed alive and just kept calling OnExecute every 15 msec because I commented out the terminate:

Is there unread data left behind in AContext.Connection.IOHandler.InputBuffer? In between each call to OnExecute, the server checks the connection's Connected() method, and it should stop the thread once Connected() no longer returns True. However, Connected() will return True if there is unread data in the InputBuffer, even if the underlying socket is closed. Sometimes, when closing down a socket manually, you also need to clear the InputBuffer too, because of that reason.

Try this:

Code:
if Key = '' then // invalid websocket header
begin
  MadExcept.NameThread(GetCurrentThreadID, 'WebSocket No Key (' + IP + ')');
  LogData.Debug('WebSocket No Key (' + IP + ') ' + IntToStr(GetCurrentThreadID));
  AContext.Binding.CloseSocket;  // alternative to AContext.Connection.Disconnect
  AContext.Connection.IOHandler.InputBuffer.Clear;
  Exit;
end;

Reply
#12
(04-25-2022, 06:11 PM)rlebeau Wrote:
Code:
  AContext.Binding.CloseSocket;
  AContext.Connection.IOHandler.InputBuffer.Clear;
  Exit;

I'll give that a try and report back. I didn't even check to see to what they were sending (if anything). Meanwhile, manually terminating the thread has been working great but if clearing the buffer works then I'll switch to that.
Reply
#13
I can confirm that clearing the buffer after disconnecting does indeed terminate the thread. I had dozens of rogue connection attempts overnight and none of their threads were still active.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)