简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM