#1 2016-01-21 09:58:20

hnb
Member
Registered: 2015-06-15
Posts: 291

FPC server destroy problem

In my app, server is released in this way:

  with TSQLite3HttpService.CreateAsConsole do
  try
    DoStart(nil);

    writeln(#10'Background server is running.'#10);
    writeln('Press [Enter] to close the server.'#10);
    ConsoleWaitForEnterKey; // ReadLn if you do not use main thread execution
    exit;
  finally
    Free;
  end

During server closing (only when the client is still trying to communicate with server), I have very often strange error in 3 ways (i think it is all related to some mORMot logic bug):

1 is raised in TSQLHttpServer.HttpThreadTerminate > TSQLRestServer.EndCurrentThread, line 38290 :

  if Sender.ThreadID<>CurrentThreadId then
    raise ECommunicationException.CreateUTF8(
      '%.EndCurrentThread(%.ID=%) should match CurrentThreadID=%',
      [self,Sender,Sender.ThreadID,CurrentThreadId]); 

2 VarRecToTempUTF8 in SynCommons - SIGSEGV for this:

    vtObject: begin
      if V.VObject<>nil then begin
        Res.Text := PUTF8Char(PPointer(PPtrInt(V.VObject)^+vmtClassName)^)+1;  

3 for procedure in RTL - TThread.SysDestroy; / TThread.Synchronize; (classes module)

always is raised randomly one of above errors. In Delphi all works fine...

This time I have no idea what is wrong :\


best regards,
Maciej Izak

Offline

#2 2016-01-21 10:41:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: FPC server destroy problem

Sounds like if a class instance is released twice, or not released in the expected order.

Try to run the server with FastMM4 FullDebugMode, which would help identify the problem.
See http://wiert.me/2009/07/29/delphi-fastm … roduction/

Offline

#3 2016-01-21 10:50:52

hnb
Member
Registered: 2015-06-15
Posts: 291

Re: FPC server destroy problem

ab wrote:

Sounds like if a class instance is released twice, or not released in the expected order.

Try to run the server with FastMM4 FullDebugMode, which would help identify the problem.
See http://wiert.me/2009/07/29/delphi-fastm … roduction/

That would be nice but FastMM4 don't work for FPC Windows target smile. I can't test it for Linux, the linux version of my server is in progress :\


best regards,
Maciej Izak

Offline

#4 2016-01-21 10:54:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: FPC server destroy problem

Try the FullDebugMode in Delphi.
Perhaps you would find an error, which is tolerated by Delphi, but not accepted by FPC...

Offline

#5 2016-01-21 11:30:39

hnb
Member
Registered: 2015-06-15
Posts: 291

Re: FPC server destroy problem

All is fine in Delphi. One small detail - in Delphi I need to press sometimes few times [enter] to close server, and even with this, closing is a little delayed (some threads locks?) during in fpc I need to press only one [enter] and server is released immediatly.


best regards,
Maciej Izak

Offline

#6 2016-01-21 12:12:13

hnb
Member
Registered: 2015-06-15
Posts: 291

Re: FPC server destroy problem

  fHttpServer.Terminate;
  fHttpServer.WaitFor;

in destructor TSQLHttpServer.Destroy; in mORMotHttpServer before

FreeAndNil(fHttpServer); 

fixes the problem for FPC.

Last edited by hnb (2016-01-21 12:12:26)


best regards,
Maciej Izak

Offline

#7 2016-01-21 12:25:03

hnb
Member
Registered: 2015-06-15
Posts: 291

Re: FPC server destroy problem

Alternatively can be used this patch (SynCrtSock.pas):

destructor THttpApiServer.Destroy;
var i: integer;
begin
  {.$ifdef LVCL}
  Terminate; // for Execute to be notified about end of process
  WaitFor;
  {.$endif} 

best regards,
Maciej Izak

Offline

#8 2016-01-21 15:19:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: FPC server destroy problem

I've overriden the TSynThread.Destroy method so that it calls Terminate and WaitFor, as TThread.Destroy in Delphi RTL.

It should fix the problem at higher level.
See http://synopse.info/fossil/info/265f6a043b

Offline

#9 2016-01-22 08:08:28

hnb
Member
Registered: 2015-06-15
Posts: 291

Re: FPC server destroy problem

Yup it helps. I've created related bug report: http://bugs.freepascal.org/view.php?id=29470 (could you add some comment on bugtracker?)

In FPC is called Terminate and WaitFor but in other conditions (SysDestroy method):

  if FHandle<>0 then
    begin
      { Don't check Suspended. If the thread has been externally suspended (which is
        deprecated and strongly discouraged), it's better to deadlock here than
        to silently free the object and leave OS resources leaked. }
      if not FFinished {and not Suspended} then
        begin
          Terminate;
          { Allow the thread function to perform the necessary cleanup. Since
            we've just set Terminated flag, it won't call Execute. }
          if FInitialSuspended then
            Start;
          WaitFor;
        end;
    end; 

best regards,
Maciej Izak

Offline

#10 2016-01-22 08:32:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: FPC server destroy problem

Yes, I added a comment to the FPC tracker.

Does our TSynThread patch fix the issue you were talking about?

Offline

#11 2016-01-22 08:37:07

hnb
Member
Registered: 2015-06-15
Posts: 291

Re: FPC server destroy problem

Yes, works like a charm smile.


best regards,
Maciej Izak

Offline

#12 2016-01-22 09:08:54

hnb
Member
Registered: 2015-06-15
Posts: 291

Re: FPC server destroy problem

Other good news: even "[Enter]" action to close server in console is now delayed as in Delphi wink Same behavior, both for Delphi and FPC.


best regards,
Maciej Izak

Offline

Board footer

Powered by FluxBB