Recent

Author Topic: Segmentation fault when stopping daemon in Debian 6 (Lazarus 0.9.30, FPC 2.4.2).  (Read 12704 times)

touchring

  • Full Member
  • ***
  • Posts: 174
Hi, this happens when i created the daemon using the "New Project wizard" which adds LazDaemonApp instead of LazDaemon and interfaces (for earlier versions).

joshua@debian:~/Desktop/Data/testapps/basicdaemon$ ./project1 -r
Segmentation fault
joshua@debian:~/Desktop/Data/testapps/basicdaemon$ gdb ./project1

I've tried issuing kill pid and Ctrl C, both will result in Segmentation fault.  Heap trace don't work so i can't debug my application.

I've uploaded the project file with all settings to - http://www.2shared.com/file/DQKFuP0u/basicdaemon.html

Appreciate any ideas.  Thanks.  :)


Here's the trace from GDB.

joshua@debian:~/Desktop/Data/testapps/basicdaemon$ gdb ./project1
.............
Reading symbols from /home/joshua/Desktop/Data/testapps/basicdaemon/project1...done.
(gdb) run -r
Starting program: /home/joshua/Desktop/Data/testapps/basicdaemon/project1 -r
^C
Program received signal SIGINT, Interrupt.
0xb7fe2424 in __kernel_vsyscall ()
(gdb) bt
#0  0xb7fe2424 in __kernel_vsyscall ()
#1  0x08048732 in SYSTEM_FPSYSCALL$LONGINT$LONGINT$LONGINT$$LONGINT ()
#2  0x0809f481 in BASEUNIX_FPSIGSUSPEND$SIGSET_T$$LONGINT ()
#3  0x0809f56a in BASEUNIX_SIGPAUSE$LONGINT$$LONGINT ()
#4  0x00000000 in ?? ()
(gdb)


Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
I didn't study the code. I only compiled, started and stopped by Ctrl+C. Here is output:
Code: [Select]
[v1@msi_NB BasicDaemon]$ ./project1 -r
^CHeap dump by heaptrc unit
96 memory blocks allocated : 12295/12552
90 memory blocks freed     : 11969/12224
6 unfreed memory blocks : 326
True heap size : 196608
True free heap : 195328
Should be : 195512
Call trace for block $00007F1A4A0CF140 size 30
  $000000000049ED80 line 3977 of lresources.pp
  $000000000046009C
  $000000000049B361 line 3106 of lresources.pp
  $000000000049B0FB line 3126 of lresources.pp
  $00000000004573DF
  $000000000043BE02
  $000000000043F448
  $00000000004142C8 line 14 of project1.lpr
Call trace for block $00007F1A4A0CEE40 size 32
  $000000000042A2AA
  $000000000043D7AE
  $000000000043EB5F
  $000000000043E910
  $000000000043BE02
  $000000000043F448
  $00000000004142C8 line 14 of project1.lpr
  $000000000041426D
Call trace for block $00007F1A4A0CED80 size 24
  $0000000000452120
  $000000000043D7AE
  $000000000043EB5F
  $000000000043E910
  $000000000043BE02
  $000000000043F448
  $00000000004142C8 line 14 of project1.lpr
  $000000000041426D
Call trace for block $00007F1A4A0CECC0 size 24
  $00000000004477AE
  $000000000043D079
  $000000000043D603
  $000000000043EB5F
  $000000000043E910
  $000000000043BE02
  $000000000043F448
  $00000000004142C8 line 14 of project1.lpr
Call trace for block $00007F1A4A0CEC00 size 48
  $000000000043D603
  $000000000043EB5F
  $000000000043E910
  $000000000043BE02
  $000000000043F448
  $00000000004142C8 line 14 of project1.lpr
  $000000000041426D
Call trace for block $00007F1A4A05C0C0 size 168
  $000000000043BE02
  $000000000043F448
  $00000000004142C8 line 14 of project1.lpr
  $000000000041426D
[v1@msi_NB BasicDaemon]$

EDIT: Lazarus 0.9.31 r30853M FPC 2.5.1 x86_64-linux-qt (Chakra-Project = Arch based KDE4 distro)
« Last Edit: June 04, 2011, 10:58:50 pm by Blaazen »
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

touchring

  • Full Member
  • ***
  • Posts: 174
I setup DaemonMapper1 and defined OnStart for Daemon1, no more segmentation fault, at least for the last built, the daemon still stops with exception and leaks.

One thing is I still can't figure out when Segmentation Fault appears - i am beginning to suspect it is a random error which depends on some random event during compilation stage.

Source can be downloaded from here - http://www.2shared.com/file/3Ji7ACBg/basicdaemon.html

Quote
unit DaemonUnit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, DaemonApp;

type

  { TDaemon1 }

  TDaemon1 = class(TDaemon)
    procedure DataModuleStart(Sender: TCustomDaemon; var OK: boolean);
    procedure DataModuleStop(Sender: TCustomDaemon; var OK: boolean);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Daemon1: TDaemon1;

implementation

procedure RegisterDaemon;
begin
  RegisterDaemonClass(TDaemon1);
end;

{ TDaemon1 }

procedure TDaemon1.DataModuleStart(Sender: TCustomDaemon; var OK: boolean);
begin
  OK := True;
end;


procedure TDaemon1.DataModuleStop(Sender: TCustomDaemon; var OK: boolean);
begin
  OK := True;
end;


{$R *.lfm}


initialization
  RegisterDaemon;
end.

Quote
root@debian:/home/joshua/Desktop/Data/testapps/basicdaemon# ./project1 -r
An unhandled exception occurred at $080E057A :
EAccessViolation :
  $080E057A

An unhandled exception occurred at $08063C28 :
EAccessViolation :
  $08063C28
  $08063D17
  $08057964
  $0809EC05
  $0805A9AE

Heap dump by heaptrc unit
9 memory blocks allocated : 280/296
2 memory blocks freed     : 53/64
7 unfreed memory blocks : 227
True heap size : 131072
True free heap : 130304
Should be : 130392
Call trace for block $B76080F0 size 64
  $0805B3E8
  $08057941
  $0809EC05
  $0805A9AE
  $08063D17
  $08057964
  $0809EC05
  $0805A9AE
Call trace for block $B7600120 size 24
  $08057941
  $0809EC05
  $0805A9AE
  $08063D17
  $08057964
  $0809EC05
  $0805A9AE
Call trace for block $B76000C0 size 12
  $0809EA17
  $0805A9AE
  $08063D17
  $08057964
  $0809EC05
  $0805A9AE
Call trace for block $B7600060 size 23
  $0805B3E8
  $0806E594
  $B7627C76
  $080D91A2
Call trace for block $B7608060 size 64
  $0805B3E8
  $0806E594
  $B7627C76
  $080D91A2
Call trace for block $B7775060 size 8
  $0805B3E8
  $0806E594
  $B7627C76
  $080D91A2
Call trace for block $B777D060 size 32
  $0805B3E8
  $0806E594
  $B7627C76
  $080D91A2

touchring

  • Full Member
  • ***
  • Posts: 174
I didn't study the code. I only compiled, started and stopped by Ctrl+C. Here is output:

EDIT: Lazarus 0.9.31 r30853M FPC 2.5.1 x86_64-linux-qt (Chakra-Project = Arch based KDE4 distro)


Thanks for trying, pls try the latest version.

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
With new version, after pressing Ctrl+C
Code: [Select]
An unhandled exception occurred at $0000000000439D82 :
EAccessViolation :
  $0000000000439D82

Heap dump by heaptrc unit
8 memory blocks allocated : 891/920
2 memory blocks freed     : 69/80
6 unfreed memory blocks : 822
True heap size : 360448
True free heap : 358688
Should be : 358840
Call trace for block $00007F8477916240 size 40
  $00000000004297CC
  $00000000004183BD
Call trace for block $00007F8477916180 size 20
  $000000000048A177
  $00000000004183BD
Call trace for block $00007F847790E0C0 size 9
  $000000000042E3FA
  $00000000004183BD
Call trace for block $00007F84779160C0 size 48
  $000000000042E3FA
  $00000000004183BD
Call trace for block $00007F847791E0C0 size 141
  $000000000042E3FA
  $00000000004183BD
Call trace for block $00007F84778C40D0 size 564
  $000000000042E3FA
  $00000000004183BD
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

touchring

  • Full Member
  • ***
  • Posts: 174
Yes, I'm getting unhandled exception as well.  Any thoughts? 

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
No idea. I just tried some example from another thread and it also gives memory leak or SIGSEV.
http://forum.lazarus.freepascal.org/index.php?topic=8576.0 - the last post
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

touchring

  • Full Member
  • ***
  • Posts: 174
No idea. I just tried some example from another thread and it also gives memory leak or SIGSEV.
http://forum.lazarus.freepascal.org/index.php?topic=8576.0 - the last post



Thanks, please try this example, let me know if you get segmentation fault.  Also, try the binary attached.  Wanna know if it's my compiler or OS problem.  :o

http://www.2shared.com/file/y5ROO02j/basicdaemon.html
or
http://uploading.com/files/4eadca9b/basicdaemon.zip/


I'm getting:-

Quote
root@debian:/home/touchring/Desktop/Data/testapps/basicdaemon# ./project1 -r -debug
        0   Initializing service.
^C   0   Shutdown service.
Segmentation fault


Segmentation Fault is driving me nuts.  :(
« Last Edit: June 06, 2011, 05:19:28 am by touchring »

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
I cannot run your binary (are you on 32-bits ?).
Compiling your third version gives me various behaviour.
Code: [Select]
[v1@msi_NB BasicDaemon3]$ ./project1 -r
^C^Cexception at 000000000041856B:
Access violation.
An unhandled exception occurred at $0000000000439D92 :
EAccessViolation :
  $0000000000439D92

[v1@msi_NB BasicDaemon3]$ ./project1 -r
^CNeoprávněný přístup do paměti (SIGSEGV)
[v1@msi_NB BasicDaemon3]$ ./project1 -r
^C^C
Exception, SIGSEGV or nothing.
This is the log:
Code: [Select]
project1 [2011-06-06 10:37:52.711 Info] Daemon BasicDaemon current status: Start Pending
project1 [2011-06-06 10:37:52.718 Info] Daemon BasicDaemon current status: Running
project1 [2011-06-06 10:37:55.286 Info] Daemon BasicDaemon current status: Stop Pending
project1 [2011-06-06 10:37:55.291 Info] Daemon BasicDaemon current status: Stopped
I use freepascal 2.5.1, 64-bit (SVN).
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

touchring

  • Full Member
  • ***
  • Posts: 174
I don't even get the AV error and address, just Segmentation Fault.  If you see the code, it is quite straightforward.  btw, there should be a /log folder created.

Note: I'm using Lazarus 0.9.30 (32-bit), FPC 2.4.2 on a 64 bit machine, running Debian 6.0 guest on Virtualbox.

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Quote
there should be a /log folder created
Nothing special, only
Code: [Select]
v1.3 2011_06_06_09_50_34 sys Main 0 Shutdown service.
v1.3 2011_06_06_09_50_35 sys Main 0 Initializing service.
v1.3 2011_06_06_09_50_42 sys Main 0 Initializing service.
v1.3 2011_06_06_09_50_43 sys Main 0 Shutdown service.
v1.3 2011_06_06_09_50_44 sys Main 0 Initializing service. 
I am afraid I can't help you. Maybe there is problem in daemonapp.pas
And it has the same behaviour when I try:
Code: [Select]
./project1 -ror
Code: [Select]
sudo ./project1 -r
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

touchring

  • Full Member
  • ***
  • Posts: 174
Thanks, at least you assured me it's not my VM or OS problem.  Now, without isolating the problem, I don't even know if this is a bug of mine or daemonapp.pp has memory leaks.

I do know that FPC has its peculiarities and some stuff that work on Delphi cleanly won't work as well with FPC even, though the code can compile.

I've already thought of how to deal with memory leaks - create another watcher daemon to restart the primary daemon everyday!  :D


Quote
Latest update:

Just to confirm, segmentation fault also appears in cleandir example!

root@debian:/home/joshua/Desktop/examples/cleandir# ./cleandirs -r -c /home/joshua/Desktop/examples/cleandir/cleandir.cfg
Segmentation fault
root@debian:/home/joshua/Desktop/examples/cleandir#
« Last Edit: June 06, 2011, 06:31:17 pm by touchring »

eolandro

  • New Member
  • *
  • Posts: 12
Well it's been a long time but who knows !!!

I think i got a kind of answer. the problem with daemons and threads is the count of threads.

Let me show you.

with this example and put this command
$pstree -p | grep proyect1

you got something like this:

|-bash(6966)---project1(6978)---{project1}(6979)

if you see you have only Two pid the first is a child of bash, the second one is from that child.

but with another example of daemon that not crash, you got something like this:

|-xfce4-terminal(2530)-+-bash(6169)---pcfdidmon(7287)-+-{pcfdidmon}(7288)
           |                      |                              `-{pcfdidmon}(7289)

If you see you got 3 pids.

i don't know why this happends. but i think fpc do something like a double fork and if fpc can not do the second fork when you put a Ctrl-C or kill -15 <PID> you get a segmentation fault.

sometimes if you rewrite a Execute method from TDaemon, you got the problem of only 2 pids.


PD
"do the UNIX double-fork magic, see Stevens Advanced Programming in the UNIX Environment" for details (ISBN 0201563177)" extract from a python snippet http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/.