I’m working with a common server pattern:
- Accept a client socket
- Fork a new thread to do some action with the socket
- Ensure that the socket always gets closed once the thread ends
On the surface this seems really similar to what bracket does, but the forking throws a wrench into it.
Here’s my attempt, largely based on some code from network-simple, to abstract this into a general function:
bracketFork
:: IO resource
-- ^ Runs first, in the current thread
-> (resource -> IO a)
-- ^ Runs last, might run in either thread
-> (resource -> IO b)
-- ^ Runs in a new thread
-> IO ThreadId
bracketFork open close action = mask $ \restore ->
do
resource <- restore open
let a = action resource >> return ()
let b = close resource
forkIO (restore a `finally` b) `onException` b
I still have a really hard time reasoning about exception masking, so I’m hoping somebody can check my work here!
- Can we be sure that
close
always runs? - Can we be sure that
close
cannot run twice?
And then, if we are confident that this code is correct (or if we can fix it), I’m wondering if there is an appropriate library that might welcome the addition of this function? (Or does it already exist somewhere?) I think masking is a really difficult subject, and so the more of these patterns we can get into libraries, the better, in my opinion.