withDev: Close the FD on exception

This commit is contained in:
Ömer Sinan Ağacan 2017-11-30 10:30:29 +03:00
parent 5e354f9bfc
commit d7aeb5f545

View File

@ -58,7 +58,7 @@ withDev :: String -> (H -> IO a) -> IO a
withDev filepath f = openDev filepath >>= \h ->
case h of
Nothing -> error ("device " ++ filepath ++ " cannot be grabbed")
Just fd -> f fd >>= \r -> (closeDev fd >> return r)
Just fd -> f fd `E.finally` closeDev fd
closeDev :: H -> IO ()
closeDev h = hClose h