View Issue Details

IDProjectCategoryView StatusLast Update
0032172FPCFCLpublic2017-11-07 07:52
ReporterCyraxAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
PlatformLinux x86_64OSArchOS Version3.17.4-1
Product Version3.1.1Product Buildr36747 
Target Version3.2.0Fixed in Version3.1.1 
Summary0032172: Running daemon application can't be terminated/killed from terminal.
DescriptionFor some odd reason you can't gracefully (send TERM signal to process by kill utility or by CTRL-C in terminal) shutdown a daemon application from terminal.
Steps To Reproduce0. Install LazDaemon package into Lazarus.
1. Open attached project in Lazarus.
2. Compile project.
3. Open terminal and goto project binary directory, run project with --run parameter.
4. Open another terminal and goto project root directory, run stop-project.sh script to send TERM signal to running project.
5. Observe that project doesn't shutdown properly.
Additional InformationOS is "Linux archdev 4.11.9-1-ARCH 0000001 SMP PREEMPT Wed Jul 5 18:23:08 CEST 2017 x86_64 GNU/Linux"

---
Free Pascal Compiler version 3.1.1-r36747

Lazarus 1.9.0 r55502 FPC 3.1.1 x86_64-linux-qt
Tagspatch
Fixed in Revision36752
FPCOldBugId
FPCTarget
Attached Files
  • project1.tar.gz (2,094 bytes)
  • fcl_daemon_fix.diff (804 bytes)
    diff --git packages/fcl-extra/src/unix/daemonapp.inc packages/fcl-extra/src/unix/daemonapp.inc
    index 1cd53be543..9ea4b0e0cf 100644
    --- packages/fcl-extra/src/unix/daemonapp.inc
    +++ packages/fcl-extra/src/unix/daemonapp.inc
    @@ -216,16 +216,18 @@ begin
       Application.Terminate;
     end;
     
    -Procedure SysInitDaemonApp;
    -
     Var
    -  old,new : SigactionRec;
    +  old,new : array [0..2] of SigactionRec;
    +
    +Procedure SysInitDaemonApp;
     
     begin
    -  New.sa_handler:=@DoShutDown;
    -  fpSigaction(SIGQUIT,@New,@Old);
    -  fpSigaction(SIGTERM,@New,@Old);
    -  fpSigaction(SIGINT,@New,@Old);
    +  New[0].sa_handler:=@DoShutDown;
    +  New[1].sa_handler:=@DoShutDown;
    +  New[2].sa_handler:=@DoShutDown;
    +  fpSigaction(SIGQUIT,@New[0],@Old[0]);
    +  fpSigaction(SIGTERM,@New[1],@Old[1]);
    +  fpSigaction(SIGINT,@New[2],@Old[2]);
     end;
     
     
    
    fcl_daemon_fix.diff (804 bytes)
  • fcl_daemon_fix.patch (1,147 bytes)
    From 9e5f28dfa6a0ca78f896e8c38cb960c0b91792b5 Mon Sep 17 00:00:00 2001
    From: john <no@email.com.invalid>
    Date: Thu, 20 Jul 2017 10:57:06 +0300
    Subject: [PATCH] Commit : Fix for UNIX daemon applications.
    
    ---
     packages/fcl-extra/src/unix/daemonapp.inc | 16 +++++++++-------
     1 file changed, 9 insertions(+), 7 deletions(-)
    
    diff --git a/packages/fcl-extra/src/unix/daemonapp.inc b/packages/fcl-extra/src/unix/daemonapp.inc
    index 1cd53be543..9ea4b0e0cf 100644
    --- a/packages/fcl-extra/src/unix/daemonapp.inc
    +++ b/packages/fcl-extra/src/unix/daemonapp.inc
    @@ -216,16 +216,18 @@ begin
       Application.Terminate;
     end;
     
    -Procedure SysInitDaemonApp;
    -
     Var
    -  old,new : SigactionRec;
    +  old,new : array [0..2] of SigactionRec;
    +
    +Procedure SysInitDaemonApp;
     
     begin
    -  New.sa_handler:=@DoShutDown;
    -  fpSigaction(SIGQUIT,@New,@Old);
    -  fpSigaction(SIGTERM,@New,@Old);
    -  fpSigaction(SIGINT,@New,@Old);
    +  New[0].sa_handler:=@DoShutDown;
    +  New[1].sa_handler:=@DoShutDown;
    +  New[2].sa_handler:=@DoShutDown;
    +  fpSigaction(SIGQUIT,@New[0],@Old[0]);
    +  fpSigaction(SIGTERM,@New[1],@Old[1]);
    +  fpSigaction(SIGINT,@New[2],@Old[2]);
     end;
     
     
    -- 
    2.13.3
    
    
    fcl_daemon_fix.patch (1,147 bytes)
  • output.txt (210 bytes)
    [john@arch_x86-64_devel x86_64-linux-qt-30101-Default]$ ./project1 --run
    Main program start
    Daemon Start
    Daemon Execute
    
    ^CException at 0000000000400D44: EAccessViolation:
    Access violation.
    Main program end
    ^C
    
    output.txt (210 bytes)
  • fcl_daemon_fix2.diff (811 bytes)
    diff --git packages/fcl-extra/src/unix/daemonapp.inc packages/fcl-extra/src/unix/daemonapp.inc
    index 1cd53be543..7eba52b11d 100644
    --- packages/fcl-extra/src/unix/daemonapp.inc
    +++ packages/fcl-extra/src/unix/daemonapp.inc
    @@ -218,14 +218,22 @@ end;
     
     Procedure SysInitDaemonApp;
     
    -Var
    -  old,new : SigactionRec;
    +  Procedure installhandler(aSig : Longint);
    +
    +  Var
    +    old,new : SigactionRec;
    +
    +  begin
    +    FillChar(New,SizeOf(Sigactionrec),#0);
    +    FillChar(Old,SizeOf(Sigactionrec),#0);
    +    New.sa_handler:=@DoShutDown;
    +    fpSigaction(aSig,@New,@Old);
    +  end;
     
     begin
    -  New.sa_handler:=@DoShutDown;
    -  fpSigaction(SIGQUIT,@New,@Old);
    -  fpSigaction(SIGTERM,@New,@Old);
    -  fpSigaction(SIGINT,@New,@Old);
    +  InstallHandler(SIGQUIT);
    +  InstallHandler(SIGTERM);
    +  InstallHandler(SIGINT);
     end;
     
     
    
    fcl_daemon_fix2.diff (811 bytes)

Activities

Cyrax

2017-07-20 09:52

reporter  

project1.tar.gz (2,094 bytes)

Cyrax

2017-07-20 10:10

reporter  

fcl_daemon_fix.diff (804 bytes)
diff --git packages/fcl-extra/src/unix/daemonapp.inc packages/fcl-extra/src/unix/daemonapp.inc
index 1cd53be543..9ea4b0e0cf 100644
--- packages/fcl-extra/src/unix/daemonapp.inc
+++ packages/fcl-extra/src/unix/daemonapp.inc
@@ -216,16 +216,18 @@ begin
   Application.Terminate;
 end;
 
-Procedure SysInitDaemonApp;
-
 Var
-  old,new : SigactionRec;
+  old,new : array [0..2] of SigactionRec;
+
+Procedure SysInitDaemonApp;
 
 begin
-  New.sa_handler:=@DoShutDown;
-  fpSigaction(SIGQUIT,@New,@Old);
-  fpSigaction(SIGTERM,@New,@Old);
-  fpSigaction(SIGINT,@New,@Old);
+  New[0].sa_handler:=@DoShutDown;
+  New[1].sa_handler:=@DoShutDown;
+  New[2].sa_handler:=@DoShutDown;
+  fpSigaction(SIGQUIT,@New[0],@Old[0]);
+  fpSigaction(SIGTERM,@New[1],@Old[1]);
+  fpSigaction(SIGINT,@New[2],@Old[2]);
 end;
 
 
fcl_daemon_fix.diff (804 bytes)

Cyrax

2017-07-20 10:10

reporter  

fcl_daemon_fix.patch (1,147 bytes)
From 9e5f28dfa6a0ca78f896e8c38cb960c0b91792b5 Mon Sep 17 00:00:00 2001
From: john <no@email.com.invalid>
Date: Thu, 20 Jul 2017 10:57:06 +0300
Subject: [PATCH] Commit : Fix for UNIX daemon applications.

---
 packages/fcl-extra/src/unix/daemonapp.inc | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/packages/fcl-extra/src/unix/daemonapp.inc b/packages/fcl-extra/src/unix/daemonapp.inc
index 1cd53be543..9ea4b0e0cf 100644
--- a/packages/fcl-extra/src/unix/daemonapp.inc
+++ b/packages/fcl-extra/src/unix/daemonapp.inc
@@ -216,16 +216,18 @@ begin
   Application.Terminate;
 end;
 
-Procedure SysInitDaemonApp;
-
 Var
-  old,new : SigactionRec;
+  old,new : array [0..2] of SigactionRec;
+
+Procedure SysInitDaemonApp;
 
 begin
-  New.sa_handler:=@DoShutDown;
-  fpSigaction(SIGQUIT,@New,@Old);
-  fpSigaction(SIGTERM,@New,@Old);
-  fpSigaction(SIGINT,@New,@Old);
+  New[0].sa_handler:=@DoShutDown;
+  New[1].sa_handler:=@DoShutDown;
+  New[2].sa_handler:=@DoShutDown;
+  fpSigaction(SIGQUIT,@New[0],@Old[0]);
+  fpSigaction(SIGTERM,@New[1],@Old[1]);
+  fpSigaction(SIGINT,@New[2],@Old[2]);
 end;
 
 
-- 
2.13.3

fcl_daemon_fix.patch (1,147 bytes)

Cyrax

2017-07-20 10:12

reporter   ~0101811

Attached patch(es) that will fix this bug.

Please use fcl_daemon_fix.diff patch for Subversion repository. Other is for Git.

Cyrax

2017-07-20 10:14

reporter  

output.txt (210 bytes)
[john@arch_x86-64_devel x86_64-linux-qt-30101-Default]$ ./project1 --run
Main program start
Daemon Start
Daemon Execute

^CException at 0000000000400D44: EAccessViolation:
Access violation.
Main program end
^C
output.txt (210 bytes)

Michael Van Canneyt

2017-07-20 10:59

administrator   ~0101813

Strange that this would fix things. I suspect because global variables are zeroed out:

Can you please try a different approach:

Procedure SysInitDaemonApp;

  Procedure installhandler(aSig : Longint);

  Var
    old,new : SigactionRec;

  begin
    FillChar(New,SizeOf(Sigactionrec),#0);
    FillChar(Old,SizeOf(Sigactionrec),#0);
    New.sa_handler:=@DoShutDown;
    fpSigaction(aSig,@New,@Old);
  end;

begin
  InstallHandler(SIGQUIT);
  InstallHandler(SIGTERM);
  InstallHandler(SIGINT);
end;

Cyrax

2017-07-20 11:58

reporter  

fcl_daemon_fix2.diff (811 bytes)
diff --git packages/fcl-extra/src/unix/daemonapp.inc packages/fcl-extra/src/unix/daemonapp.inc
index 1cd53be543..7eba52b11d 100644
--- packages/fcl-extra/src/unix/daemonapp.inc
+++ packages/fcl-extra/src/unix/daemonapp.inc
@@ -218,14 +218,22 @@ end;
 
 Procedure SysInitDaemonApp;
 
-Var
-  old,new : SigactionRec;
+  Procedure installhandler(aSig : Longint);
+
+  Var
+    old,new : SigactionRec;
+
+  begin
+    FillChar(New,SizeOf(Sigactionrec),#0);
+    FillChar(Old,SizeOf(Sigactionrec),#0);
+    New.sa_handler:=@DoShutDown;
+    fpSigaction(aSig,@New,@Old);
+  end;
 
 begin
-  New.sa_handler:=@DoShutDown;
-  fpSigaction(SIGQUIT,@New,@Old);
-  fpSigaction(SIGTERM,@New,@Old);
-  fpSigaction(SIGINT,@New,@Old);
+  InstallHandler(SIGQUIT);
+  InstallHandler(SIGTERM);
+  InstallHandler(SIGINT);
 end;
 
 
fcl_daemon_fix2.diff (811 bytes)

Cyrax

2017-07-20 12:01

reporter   ~0101814

Tried your suggestion, Michael. And it works too. So it was uninitialised local variables which caused this bug to occur.

Attached fcl_daemon_fix2.diff patch which uses this approach.

Thaddy de Koning

2017-07-20 12:26

reporter   ~0101816

Global variables have always been zero'd out. (heap)
Local variables have always been uninitialized (stack)
That's documented.

What's the problem? You suggest to fix this for each and every local issue?
That seems to me, well, ridiculous. (I may be wrong again)

Cyrax

2017-07-20 12:40

reporter   ~0101817

It sounds ridiculous (and lot of work, too) but it will fix strange and abnormal bugs (like this one, for example) that may occur if local variables point to random values.

Michael Van Canneyt

2017-07-20 13:39

administrator   ~0101820

Thanks for verifying, I applied the patch.

Issue History

Date Modified Username Field Change
2017-07-20 09:52 Cyrax New Issue
2017-07-20 09:52 Cyrax File Added: project1.tar.gz
2017-07-20 10:10 Cyrax File Added: fcl_daemon_fix.diff
2017-07-20 10:10 Cyrax File Added: fcl_daemon_fix.patch
2017-07-20 10:12 Cyrax Note Added: 0101811
2017-07-20 10:14 Cyrax File Added: output.txt
2017-07-20 10:15 Cyrax Tag Attached: patch
2017-07-20 10:51 Michael Van Canneyt Assigned To => Michael Van Canneyt
2017-07-20 10:51 Michael Van Canneyt Status new => assigned
2017-07-20 10:59 Michael Van Canneyt Note Added: 0101813
2017-07-20 10:59 Michael Van Canneyt Status assigned => feedback
2017-07-20 11:58 Cyrax File Added: fcl_daemon_fix2.diff
2017-07-20 12:01 Cyrax Note Added: 0101814
2017-07-20 12:01 Cyrax Status feedback => assigned
2017-07-20 12:26 Thaddy de Koning Note Added: 0101816
2017-07-20 12:40 Cyrax Note Added: 0101817
2017-07-20 13:39 Michael Van Canneyt Fixed in Revision => 36752
2017-07-20 13:39 Michael Van Canneyt Note Added: 0101820
2017-07-20 13:39 Michael Van Canneyt Status assigned => resolved
2017-07-20 13:39 Michael Van Canneyt Fixed in Version => 3.1.1
2017-07-20 13:39 Michael Van Canneyt Resolution open => fixed
2017-07-20 13:39 Michael Van Canneyt Target Version => 3.2.0
2017-11-07 07:52 Cyrax Status resolved => closed