Mark Rogers wrote:
> I am however getting EAccessViolation exceptions when shutting down my 
> application if any web requests have been made, presumably because a 
> thread servicing web requests has not been terminated. (The exception 
> gets triggered in TExecLoop.Execute.)
>   

Looking into this in more detail, it seems that if I have my instance of 
TSynHttpServer (which is derived from TSynTcpServer) set with 
SpareThreads>0, then threads which do not die (eg by timing out) do not 
get freed and cause the access violation and a memory leak.

The relevant code seems to be this (below) code from SynSrv.pas 
(function TSynTcpServer.ExecuteConnThread).

If FSpareThreads > 0 then it appears that some management of existing 
threads takes place (via FThreadPool), although at this point there are 
no existing threads so nothing happens, and then a new thread is created 
(Loop:=TExecLoop.Create) but nowhere does it seem to be added into 
FThreadPool?

I know that Synapse HTTP is not part of Synapse but hopefully someone 
here is using it or can help me fix this.

    procedure TSynTcpServer.ExecuteConnThread(const Method:
    TNotifyEvent; var aThreadId: Cardinal);
    var Loop: TExecLoop;
        i: integer;
    begin
      if not Assigned(Method) then
        exit;

      if (FSpareThreads<=0) then begin
        // Do not use spare-threads...
        with TSynThread.Create(True,Method) do begin
          aThreadId:=ThreadId;
          Resume;
        end;
        exit;
      end;
      //
      // Get spare waiting thread:
      Loop:=nil;
      with FThreadPool.LockList do try
        i:=Count;
        while (i>0) do begin
          dec(i);
          Loop:=Items[i];
          if Loop.Thread.Suspended then begin
            Remove(Loop);
            break;
          end;
          // we could catch it between UnlockList and Suspend in
    DoThreadWaiting...
          Loop:=nil;
        end;
      finally
        FThreadPool.UnlockList;
      end;
      //
      if (Loop=nil) then begin
        // Start new thread:
        Loop:=TExecLoop.Create;
        Loop.OnThreadWaiting:=Self.DoThreadWaiting;
        Loop.FThread:=TSynThread.Create(True,Loop.Execute);
      end;
      Loop.CurrentExec:=Method;
      aThreadId:=Loop.Thread.ThreadId;
      Loop.Thread.Resume;
    end;

-- 
Mark Rogers // More Solutions Ltd (Peterborough Office) // 0845 45 89 555
Registered in England (0456 0902) at 13 Clarke Rd, Milton Keynes, MK1 1LG


-- 
Mark Rogers // More Solutions Ltd (Peterborough Office) // 0845 45 89 555
Registered in England (0456 0902) at 13 Clarke Rd, Milton Keynes, MK1 1LG


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
synalist-public mailing list
synalist-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/synalist-public

Reply via email to