#1 2018-02-15 21:51:00

vlad
Member
Registered: 2017-03-20
Posts: 16

Random exceptions in FPC/Lazarus on server closure

Hello,

I just built a small test mORMot server on latest Lazarus, and observed random exceptions on exit. Server source below:

program cbserver;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp,
  SynCommons, SynLog, mORMot, mORMotHttpServer, mORMotSQLite3, SynSQLite3Static, SynDB;

type

  { TCBServer }

  TCBServer = class(TCustomApplication)
  protected
    procedure DoRun; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
  end;

  { ICBService }

  ICBService = interface(IInvokable)
    ['{2061324A-51C8-48FC-82CC-D21ECC3D16E1}']
    function Test2: RawUTF8;
  end;

  { TCBService }

  TCBService = class(TInjectableObjectRest, ICBService)
    function Test2: RawUTF8;
  end;

const
  ROOT_NAME = 'root';
  DEFAULT_PORT = '1044';

var
  aServer: TSQLRestServer;
  aHTTPServer: TSQLHttpServer;
  aModel: TSQLModel;

{ TCBService }

function TCBService.Test2: RawUTF8;
begin
  Result := 'sss';
end;

{ TCBServer }

procedure TCBServer.DoRun;
begin
  TInterfaceFactory.RegisterInterfaces([TypeInfo(ICBService)]);

  with TSQLLog.Family do begin
    Level := LOG_VERBOSE;
    EchoToConsole := LOG_VERBOSE; // log all events to the console
  end;
  aModel := TSQLModel.Create([], ROOT_NAME);
  try
    aServer := TSQLRestServerDB.Create(aModel, 'cbs.db3', false);
    try
      aServer.CreateMissingTables;
      aServer.ServiceDefine(TCBService, [ICBService], sicShared);
      aHTTPServer := TSQLHttpServer.Create(AnsiString(DEFAULT_PORT), [aServer], '+', useHttpApiRegisteringURI);
      try
        writeln(#10'Background server is running.'#10);
        writeln('Press [Enter] to close the server.'#10);
        readln;
      finally
        aHTTPServer.Free;
      end;
    finally
      aServer.Free;
    end;
  finally
    aModel.Free;
  end;


  Terminate;
end;

constructor TCBServer.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException := True;
end;

destructor TCBServer.Destroy;
begin
  inherited Destroy;
end;

var
  Application: TCBServer;
begin
  Application := TCBServer.Create(nil);
  Application.Title := 'Test';
  Application.Run;
  Application.Free;
end.

Sometimes, it exits gracefully:

Heap dump by heaptrc unit of D:\dev\lazarus\projects\CBS\cbserver.exe
2770 memory blocks allocated : 1427827/1436584
2770 memory blocks freed     : 1427827/1436584
0 unfreed memory blocks : 0
True heap size : 720896 (160 used in System startup)
True free heap : 720736

But often, various memory leaks and exceptions are thrown:

488 memory blocks allocated : 545751/546896
480 memory blocks freed     : 544727/545872
8 unfreed memory blocks : 1024
True heap size : 229376 (160 used in System startup)
True free heap : 227552
Should be : 227168
20180215 21353104  +            SynSQLite3.TSQLDatabase(035B6210).Destroy cbs.db3
Call trace for block $00000000095184D0 size 272
  $000000010000E582
  $000000010000AF70
...
20180215 21353104 EXCOS                         EExternalException (C0000005) at 00000001001637A8
20180215 21353104 DB                            SynSQLite3.TSQLDatabase(035B6210) closing "cbs.db3" 0 B
20180215 21353104 EXC                           ESQLite3Exception {"ErrorCode":0,"SQLite3ErrorCode":"secUnknown","Messag
e":"TSQLDatabase.DBClose called with no sqlite3 global"} at 0000000100160F08
20180215 21353104  -                    00.002.401
20180215 21353104  -            00.002.576
20180215 21353104  -    00.003.032
20180215 21353104 EXCOS EExternalException (C0000005) at 000000010000F38C
:
.
20180215 21353104 EXCOS EExternalException (C0000005) at 000000010000F38C
An unhandled exception occurred at $000000010000F38C:
EAccessViolation:
  $000000010000F38C
  $000000010000F489

or this:

20180215 21401560 EXCOS EExternalException (C0000005) at   $000000010003B3D6  VARRECTOTEMPUTF8,  line 20970 of ../../com
ponents/mORMot/SynCommons.pas
An unhandled exception occurred at $000000010003B3D6:
EAccessViolation: Access violation
20180215 21401600  -    00.032.184
20180215 21401600  +    mORMotSQLite3.TSQLRestServerDB(00167630).Destroy root
20180215 21401600  +            mORMotSQLite3.TSQLRestServerDB(00167630).Shutdown() root CurrentRequestCount=0
  $000000010003B3D6  VARRECTOTEMPUTF8,  line 20970 of ../../components/mORMot/SynCommons.pas
  $00000001000405AC  FORMATUTF8,  line 25512 of ../../components/mORMot/SynCommons.pas
  $000000010004032E  FORMATUTF8,  line 25471 of ../../components/mORMot/SynCommons.pas
20180215 21401601  -            00.012.421
20180215 21401601 info          mORMotSQLite3.TSQLRestServerDB(00167630) Destroy root
20180215 21401601  +            SynSQLite3.TSQLDatabase(001392F0).Destroy cbs.db3
20180215 21401601  +
20180215 21401601 DB                            SynSQLite3.TSQLDatabase(001392F0) closing "cbs.db3" 40 KB
20180215 21401601  -                    00.000.478
20180215 21401601  -            00.000.601
20180215 21401601  -    00.014.396
  $0000000100081658  CREATEUTF8,  line 62437 of ../../components/mORMot/SynCommons.pas
Heap dump by heaptrc unit of D:\dev\lazarus\projects\CBS\cbserver.exe
2751 memory blocks allocated : 1405971/1414704
An unhandled exception occurred at $00000001000175FD:
2751 memory blocks freed     : 1405971/1414704
0 unfreed memory blocks : 0
True heap size : 720896 (160 used in System startup)
True free heap : 720736
EInOutError: Invalid file handle
  $00000001000175FD
  $0000000100018BD1
  $0000000100018D57
  $00000001000346C0

My setup: Windows 10 64bit, Lazarus 1.9 from trunk (fpcupdeluxery), latest mORMot. Compiler - same target platform.

Any ideas?

Offline

#2 2018-02-15 23:42:27

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

Re: Random exceptions in FPC/Lazarus on server closure

Without the custapp unit, and with SynUsesDpr.inc clause?

Offline

#3 2018-02-16 08:11:42

vlad
Member
Registered: 2017-03-20
Posts: 16

Re: Random exceptions in FPC/Lazarus on server closure

Same random exceptions. The code below:

program cbserver;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  {$I SynDprUses.inc}
  Classes, SysUtils,
  SynCommons, SynLog, mORMot, mORMotHttpServer, mORMotSQLite3, SynSQLite3Static, SynDB;

type

  { ICBService }

  ICBService = interface(IInvokable)
    ['{2061324A-51C8-48FC-82CC-D21ECC3D16E1}']
    function Test2: RawUTF8;
  end;

  { TCBService }

  TCBService = class(TInjectableObjectRest, ICBService)
    function Test2: RawUTF8;
  end;

const
  ROOT_NAME = 'root';
  DEFAULT_PORT = '1044';

var
  aServer: TSQLRestServer;
  aHTTPServer: TSQLHttpServer;
  aModel: TSQLModel;

  { TCBService }

function TCBService.Test2: RawUTF8;
begin
  Result := 'sss';
end;

begin
TInterfaceFactory.RegisterInterfaces([TypeInfo(ICBService)]);

with TSQLLog.Family do begin
  Level := LOG_VERBOSE;
  EchoToConsole := LOG_VERBOSE; // log all events to the console
end;
aModel := TSQLModel.Create([], ROOT_NAME);
try
  aServer := TSQLRestServerDB.Create(aModel, 'cbs.db3', false);
  try
    aServer.CreateMissingTables;
    aServer.ServiceDefine(TCBService, [ICBService], sicShared);
    aHTTPServer := TSQLHttpServer.Create(AnsiString(DEFAULT_PORT), [aServer], '+', useHttpApiRegisteringURI);
    try
      writeln(#10'Background server is running.'#10);
      writeln('Press [Enter] to close the server.'#10);
      readln;
    finally
      aHTTPServer.Free;
    end;
  finally
    aServer.Free;
  end;
finally
  aModel.Free;
end;


end.

Offline

#4 2018-02-16 08:58:39

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

Re: Random exceptions in FPC/Lazarus on server closure

Please do not post huge set of source code in the forum, as stated by the rules.

With the following code:
https://pastebin.com/Jznw08ph

I don't have any problem.

Note the modifications I've made:

...
{$I Synopse.inc}

uses
  {$I SynDprUses.inc}
  Classes, SysUtils,
...

    finally
      aHTTPServer.Free;
    end;
    sleep(100);
  finally
    aServer.Free;
  end;

So just ensure the right .inc files - and a simple sleep() to let the HTTP server shutdown properly.

Offline

#5 2018-02-16 11:08:42

vlad
Member
Registered: 2017-03-20
Posts: 16

Re: Random exceptions in FPC/Lazarus on server closure

Sorry for large blocks of code, my fault.

Picked your example, but keep getting random exceptions occasionally. Played also with Sleep() by setting various timings, but the crash happens on aHTTPServer.Free call, so the pause does not have impact as such.

In case it helps, TestSQL3 test went smoothly except this one:

 1.5. Compression:
!  - GZIP format: 1 / 19 FAILED  708.40ms

Offline

#6 2018-02-16 11:22:20

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Random exceptions in FPC/Lazarus on server closure

About GZIP - see this topic

Offline

#7 2018-02-16 12:24:58

vlad
Member
Registered: 2017-03-20
Posts: 16

Re: Random exceptions in FPC/Lazarus on server closure

mpv wrote:

About GZIP - see this topic

Applied it, but compilation ended with SynZipFiles.pas(928,53) Error: Identifier not found "ZLIB_VERSION".

Offline

#8 2018-02-16 13:38:38

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Random exceptions in FPC/Lazarus on server closure

I return back a SynZip.ZLIB_VERSION constant to allow compilation of SynZipFiles. But I think in your case compression is not a root of problem

Offline

Board footer

Powered by FluxBB