openFile gives "file is locked" error on Linux when creating a non-existing file

Viktor Dukhovni ietf-dane at dukhovni.org
Wed Oct 9 03:04:07 UTC 2024


On Tue, Oct 08, 2024 at 06:08:52PM +0530, Harendra Kumar wrote:

> What if we closed a file and created another one and the inode of the
> previous file got reused for the new one? Is it possible that there is
> a small window after the deletion of the old one in which GHC keeps
> the lock in its hash table?

That SHOULD NOT happen, GHC releases the (internal hash table entry)
lock before closing the file descriptor:

    close :: FD -> IO ()
    close fd =
      do let closer realFd =
               throwErrnoIfMinus1Retry_ "GHC.IO.FD.close" $
    #if defined(mingw32_HOST_OS)
               if fdIsSocket fd then
                 c_closesocket (fromIntegral realFd)
               else
    #endif
                 c_close (fromIntegral realFd)

         -- release the lock *first*, because otherwise if we're preempted
         -- after closing but before releasing, the FD may have been reused.
         -- (#7646)
         release fd

         closeFdWith closer (fromIntegral (fdFD fd))

    release :: FD -> IO ()
    release fd = do _ <- unlockFile (fromIntegral $ fdFD fd)
                    return ()

Solved in GHC 7.8 11 years ago:

    https://gitlab.haskell.org/ghc/ghc/-/issues/7646#note_68902

This assumes that the application is not closing the file descriptor
"behind GHC's back".  That is, you're not using the POSIX package to
directly close file descriptors underlying Haskell file Handles
(which would then orphan the associated "lock").

-- 
    Viktor.


More information about the ghc-devs mailing list