Skip to content

openFile can leak file descriptors

Summary

openFile' looks like this:

openFile' :: String -> IOMode -> Bool -> Bool -> IO Handle
openFile' filepath iomode binary non_blocking = do
  -- first open the file to get an FD
  (fd, fd_type) <- FD.openFile filepath iomode non_blocking

  mb_codec <- if binary then return Nothing else fmap Just getLocaleEncoding

  -- then use it to make a Handle
  mkHandleFromFD fd fd_type filepath iomode
                   False {- do not *set* non-blocking mode -}
                   mb_codec
            `onException` IODevice.close fd

If an asynchronous exception occurs after GHC.IO.FD.openFile opens the file but before installing the exception handler, then the file descriptor will remain open with no finalizer. Shouldn't we mask around this entire function?

Steps to reproduce

Please provide a set of concrete steps to reproduce the issue.

Expected behavior

What do you expect the reproducer described above to do?

Environment

  • GHC version used:

Optional:

  • Operating System:
  • System Architecture:
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information