Sigterm Handler Crash

Antworten
Benutzeravatar
corpsman
Lazarusforum e. V.
Beiträge: 1498
Registriert: Sa 28. Feb 2009, 08:54
OS, Lazarus, FPC: Linux Mint Mate, Lazarus GIT Head, FPC 3.0
CPU-Target: 64Bit
Wohnort: Stuttgart
Kontaktdaten:

Sigterm Handler Crash

Beitrag von corpsman »

Hallo Zusammen,

Basierend auf diesem Beitrag: viewtopic.php?t=14256 habe einen killall handler in meiner Anwendung, seit neuestem bekomme ich aber eine Zugriffsverletzung in der Anwendung wenn von außen das Killall kommt :(
Project demo raised exception class 'External: SIGTERM'.

In file '../sysdeps/unix/sysv/linux/poll.c' at line 29
Im Anhang noch mal eine Demo mit der man es nachstellen kann.

Code: Alles auswählen

Program demo;

{$MODE objfpc}{$H+}

Uses
{$IFDEF UNIX}
  cthreads,
{$ENDIF}
{$IFDEF HASAMIGA}
  athreads,
{$ENDIF}
  Interfaces, // this includes the LCL widgetset
{$IFDEF LINUX}
  BaseUnix,
{$ENDIF}
  Forms, unit1
  { you can add units after this };

{$R *.res}


{$IFDEF LINUX}

Procedure DoKill(Signal: CInt); cdecl;
Begin
  If Signal = SIGTERM Then Begin
    writeln('Going down through sigkill..');
    form1.Close;
  End;
End;

Procedure InstallSigHandler;
Var
  Action: PSigActionRec;
Begin
  New(Action);
  Action^.sa_handler := SigActionHandler(@DoKill);
  FillChar(Action^.sa_mask, SizeOf(Action^.sa_mask), #0);
  Action^.sa_flags := 0;
  Action^.sa_restorer := Nil;
  If FPSigaction(SIGTERM, Action, Nil) <> 0 Then Begin
    WriteLn('Error: ', fpgeterrno);
    Halt(1);
  End;
  Dispose(Action);
End;
{$ENDIF}

Begin
{$IFDEF LINUX}
  InstallSigHandler;
{$ENDIF}
  RequireDerivedFormResource := True;
  Application.Scaled := True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
End.

Hat einer von euch eine Idee, was da auf einmal falsch dran ist ?
Dateianhänge
demo.zip
(1.89 KiB) 112-mal heruntergeladen
--
Just try it

Warf
Beiträge: 1913
Registriert: Di 23. Sep 2014, 17:46
OS, Lazarus, FPC: Win10 | Linux
CPU-Target: x86_64

Re: Sigterm Handler Crash

Beitrag von Warf »

Zum einen ist die Fehlermeldung keine Zugriffsverletzung sondern das ein SIGTERM aufgetreten ist, was ja auch stimmt. Die Frage ist also warum greift der exception handler der sysutils für das Signal statt deines custom handlers.

Außerdem ist ein form1.close in einem signalhandler unzulässig, aus dem POSIX standard: https://pubs.opengroup.org/onlinepubs/9 ... ignal.html
the behavior is undefined if the signal handler refers to any object [CX] [Option Start] other than errno [Option End] with static storage duration other than by assigning a value to an object declared as volatile sig_atomic_t,
Vereinfacht ausgedrückt darf ein signalhandler nix machen außer boolean Flags zu setzen. Komplexe Operationen sind nicht erlaubt, da Signale asynchron sind

Benutzeravatar
corpsman
Lazarusforum e. V.
Beiträge: 1498
Registriert: Sa 28. Feb 2009, 08:54
OS, Lazarus, FPC: Linux Mint Mate, Lazarus GIT Head, FPC 3.0
CPU-Target: 64Bit
Wohnort: Stuttgart
Kontaktdaten:

Re: Sigterm Handler Crash

Beitrag von corpsman »

das heist ich setzte ein Flag Need Close im Handler und polle dass im OnIdle des Formulars

Code: Alles auswählen

Var
  Form1: TForm1;
  NeedClose: Boolean = false;

Implementation

{$R *.lfm}

{ TForm1 }

Procedure TForm1.ApplicationProperties1Idle(Sender: TObject; Var Done: Boolean);
Begin
  Done := false;
  sleep(1);
  If NeedClose Then close;
End;  

Procedure DoKill(Signal: CInt); cdecl;
Begin
  If Signal = SIGTERM Then Begin
    writeln('Going down through sigkill..');
    NeedClose := true;
  End;
End;   
Mit Debugger an bekomme ich immer noch die Meldung, ohne scheint es zu gehen ...
--
Just try it

Antworten