Haskells on SIGTERM

I am trying to make a daemon that is stopped by sending it a SIGTERM signal (as usual for daemons). The daemon gets some resources that should be freed up after startup, and I wanted to use parentheses for this.

I noticed that the bracket cleaning part does not start when the program terminates using SIGTERM. This can be reproduced using this program:

main = bracket (return "ending") (\x -> putStrLn x) (\_ -> threadDelay 10000000000000) 

This simple program should get the line "ending" (for simplicity just reconfigure it) and print the resulting line at the end.

When I interrupt a program using ctrl-c, it behaves as expected and prints "termination" on exit, but when I kill it with killall -TERM test (the executable is called test), it prints "Beendet" ("Ended" ) in German), so the end of the bracket does not start.

Is this a mistake or am I doing something wrong?

I am using GHC 7.6.3 and I am working on Linux / GNU Debian jessie i386 (i686)

+6
source share
1 answer

As a rule, external signals are not turned into exceptions and are transferred to the program (for example, in a multi-threaded program, which thread will catch them?), But RTS are processed directly instead.

If you want to listen to an external signal and respond to it, the correct way is to call installHandler from the unix package: http://hackage.haskell.org/package/unix-2.7.1.0/docs/System-Posix-Signals.html

+1
source

Source: https://habr.com/ru/post/983989/


All Articles