I use Indy for TCP communication (D2009, Indy 10).
After evaluating the client’s request, I want to send a response to the client. Therefore, I save the TIdContext as it is (pseudocode)
procedure ConnectionManager.OnIncomingRequest (Context : TIdContext);
begin
Task := TTask.Create;
Task.Context := Context;
ThreadPool.AddTask (Task);
end;
procedure ThreadPool.Execute (Task : TTask);
begin
// Perform some computation
Context.Connection.IOHandler.Write ('Response');
end;
But what if the client terminates the connection somewhere between the request and the response, ready to be sent? How to check if context is saved? I tried
if Assigned (Context) and Assigned (Context.Connection) and Context.Connection.Connected then
Context.Connection.IOHandler.Write ('Response');
but it doesn’t help. In some cases, the program simply freezes, and if I pause execution, I see that the current line matches the if condition.
What's going on here? How can I avoid trying to send using dead connections?
source
share