<div dir="ltr"><div><div>Hello all,<br></div><div><br></div>I have a console (command-line) Haskell application that communicates with some hardware equipment. The equipment needs to be initialized when my program starts, and needs to be cleanly shut down when the program ends.<br><br></div><div>This is the basic structure of the program:<br>main =<br> readAvailableEquipment >>= \descs -><br> withEquipment descs $ \availableEquipment -><br></div><div>
<<< more initialization then an infinite loop as a sockets-based server, communicating with the hardware via availableEquipment >>><br></div><div><br></div><div>withEquipment is defined as<br>withEquipment descs action =<br> bracket (initializeEquipment descs) closeEquipment action<br><br></div><div>Basically I want closeEquipment to be executed when the program shuts down.<br></div><div><br></div><div>The program is stopped by the user closing the console window (hitting the upper right 'X' in the Windows title bar). I would expect that the 'closeEquipment' action is performed since it is in the cleanup part of the 'bracket', but my testing suggests that these actions never execute. Incidentally, stopping the program via Ctrl-C doesn't work.<br><br></div><div>I have tested this with GHC 8.2.2 installed via Stack (LTS 11.3). This is on Windows 10.<br></div><div><br></div><div>Summary question and TLDR: how do I reliably perform cleanup actions in a Windows console program that is closed by the user by clicking the 'X' symbol in the window title bar?<br><br></div><div>Thanks!<br></div><div>Peter<br><br></div></div>