title: TIdHttpServer freezing; calls to "OnCommandGet" start but never finish
body: <p>I have a Delphi 2007 <strong>TNtService</strong> based web service utilizing the Indy <strong>TIdHttpServer</strong> component.  The server works fine and can be quite stable for hours.  But every so often (usually 3-6 hours), it freezes.  </p>
  
  <p>Based on logging I have built into the service, <strong>OnCommandGet</strong> calls are being started but not completed.  I have threadsafe counter variables at the beginning and end of the <strong>OnCommandGet</strong> handler.  And can see when the freezing starts the "start" variable increments while the "end" count remains the same.</p>
  
  <p>I let the server remain in a state like this for a couple minutes.  Sometimes everything "gets back to normal", and all threads complete (although taking many minutes).  If it goes more than a couple minutes I have the service <strong>Halt</strong> itself.  I then have another auxiliary service to restart it.</p>
  
  <p>Some of my OnCommandGet handlers spawn <strong>TThread</strong> threads.  Originally I was not setting the <strong>priority</strong>, instead taking the default value.  I was able to correlate the freezing to times when these threads were working.  Afterwards I set <strong>priority := tpLowest;</strong>, and this seemed to mitigate the problem somewhat.  </p>
  
  <p>But I still get freezing and have no idea what it is.  </p>
  
  <p>Could it be something else in Windows consuming resources?  (I have checked <strong>eventvwr</strong> logs and don't see anything).</p>
  
  <p>Is there some way I can detect which threads/contexts are using all the memory?</p>
  
  <p>Below is the code I am using to create the server.  Not sure that it will help, as the server seems to work fine on startup.</p>
  
  <p><strong>Update 1</strong>
  @jerrydodge and @remylebeau pointed out code within the <strong>OnCommandGet</strong> needs to be included.  Problem with this: there are a lot of <strong>if...then..else</strong> cases and calls to other functions.  I have functions to return <strong>js</strong> and <strong>css</strong> files, and there are about 50 calls to other handler procedures.  Not sure that it would provide much.   I really think if anything it might be something within one of these calls.  I guess the approach might be is to try and find out which handler is the culprit.  And part of my writing this question was possibly to be given suggestions on how you can see which processes are consuming resources.  Is there a way to get CPU of specific Contexts?</p>
  
  <pre><code>ServerIOHandler := TIdServerIOHandlerSSLOpenSSL.Create(self);
  ServerIOHandler.SSLOptions.CertFile := 'C:\web\vaut\embeddedanalytics.com.crt';
  ServerIOHandler.SSLOptions.KeyFile := 'C:\web\vaut\embeddedanalyticsout.key';
  ServerIOHandler.SSLOptions.RootCertFile := 'C:\web\vaut\gd_bundle.crt';
  ServerIOHandler.SSLOptions.Method := sslvSSLv23;
  ServerIOHandler.SSLOptions.Mode := sslmServer;
  
  
  IdHTTPServer1 := TIdHTTPServer.Create;
  IdHTTPServer1.MaxConnections := FMaxConnections;
  IdHTTPServer1.AutoStartSession := True;
  IdHTTPServer1.KeepAlive := FGlobalKeepAlive;
  IdHTTPServer1.SessionState := True;
  IdHTTPServer1.OnCommandGet := MainGet;
  IdHTTPServer1.onexception := IdHttpServerexception;
  IdHTTPServer1.onlistenexception := IdHttpServerlistenexception;
  with idHttpServer1 do begin
    onconnect := IdHttpServerConnect;
    oncreatesession := IdHttpServerCreateSession;
    ondisconnect := IdHttpServerDisconnect;
    onsessionend := IdHttpServersessionend;
    onsessionstart := IdHttpServersessoinstart;
  end;
  idHttpServer1.ParseParams := True;
  
  try
    CoInitialize(nil);
    // Connect to database...  Do other initialization stuff
  finally
    CoUninitialize;
  end;
  idHttpServer1.OnQuerySSLPort := QuerySSLPort;
  idHttpServer1.IOHandler := ServerIOHandler;
  idHttpServer1.Bindings.Add.Port := FPort;
  idHttpServer1.Bindings.Add.Port := 443;
  IdHTTPServer1.Active := True;
  </code></pre>
  
  <p><strong>UPDATE 2</strong>
  I have a procedure in my code which reports the current memory usage using the function below.  I know this could be a separate question itself.  But is <strong>GetProcessMemoryInfo</strong> threadsafe?  Because I am NOT wrapping it in Critical Section.</p>
  
  <pre><code>function CurrentMemoryUsage: Cardinal;
  var
   pmc: TProcessMemoryCounters;
  begin
   pmc.cb := SizeOf(pmc) ;
   result := 0;
   if GetProcessMemoryInfo(GetCurrentProcess, @pmc, SizeOf(pmc)) then
     Result := pmc.WorkingSetSize
   else
     RaiseLastOSError;
  end;
  </code></pre>