Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haskells bracket on SIGTERM

I have been trying to make an daemon that is stopped by sending it a SIGTERM signal (like it is usual for daemons). The daemon acquires some resources that should be freed after running and I wanted to use bracket for doing so.

I noticed that the cleanup part of the bracket isn't run when the program terminates with SIGTERM. This can be reproduced with this program:

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

This simple program should acquire the string "ending" (for simplicity just by retuning it) and print that acquired string on ending.

When I interrupt the program with ctrl-c, it behaves like expected and prints "ending" on exit, but when I kill it with killall -TERM test (the executable is named test) it prints "Beendet" ("Ended" in german), so the final part of the bracket isn't run.

Is this a bug or am I doing something wrong?

I'm using GHC 7.6.3 and I'm running on Linux/GNU Debian jessie i386 (i686)

like image 488
Kritzefitz Avatar asked Mar 18 '15 22:03

Kritzefitz


1 Answers

Generally, external signals aren't turned into exceptions and passed onto the program (for one, in a multithreaded program, which thread would catch them?), but instead are handled directly by the RTS.

If you want to listen for an external signal and react to it, the proper course of action is to call installHandler from the unix package: http://hackage.haskell.org/package/unix-2.7.1.0/docs/System-Posix-Signals.html

like image 53
sclv Avatar answered Sep 22 '22 17:09

sclv