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

Harendra Kumar harendra.kumar at gmail.com
Mon Oct 7 02:55:21 UTC 2024


Hi everyone,

We have a long standing, elusive and intermittent test failure in
streamly's file system event test suite. The code where the error
emanates from looks like this:

createFileWithParent :: FilePath -> FilePath -> IO ()
createFileWithParent file parent = do
    let filepath = parent </> file
    let dir = takeDirectory filepath
    putStrLn $ "createFileWithParent: file ["
        ++ file ++ "] dir [" ++ dir ++ "]"
    putStrLn $ "Ensuring dir: " ++ dir
    createDirectoryIfMissing True dir
    r <- doesDirectoryExist dir
    if r
    then do
        putStrLn $ "Ensured dir: " ++ dir
        when (not (null file)) $ do
            exists <- doesFileExist filepath
            if not exists
            then do
                putStrLn $ "Creating file: " ++ (parent </> file)
                openFile (parent </> file) WriteMode >>= hClose
                putStrLn $ "Created file: " ++ (parent </> file)
            else error $ "File exists: " ++ filepath
    else error $ "Could not create dir: " ++ dir


The important thing in the code above is that we check that the file
does not exist already, and we are creating it. Creating this new file
intermittently fails with the following error messages, all of these
are coming from prints in the above:

createFileWithParent: file [file1] dir
[/tmp/fsevent_dir-e1098325dc2b0880/watch-root]
Ensuring dir: /tmp/fsevent_dir-e1098325dc2b0880/watch-root
Ensured dir: /tmp/fsevent_dir-e1098325dc2b0880/watch-root
Creating file: /tmp/fsevent_dir-e1098325dc2b0880/watch-root/file1

uncaught exception: IOException of type ResourceBusy
/tmp/fsevent_dir-e1098325dc2b0880/watch-root/file1: openFile: resource
busy (file is locked)

How can a file that does not exist and being created be locked during
creation? We see this only on Linux, the same tests always succeed on
Windows and macOS. We are facing this in GHC 9.2.8, I am not sure if
it is version specific because in the past I selectively disabled the
tests for specific GHC versions and the error still surfaced in other
GHC versions.

Anything obvious that we may be missing here? Any pointers or more
debug info to generate to debug this? I have not started looking into
the openFile code yet, hoping someone else knowing it well might help
here.

Is this some other "resource busy" error which is being wrongly
bucketed into "file is locked" error?

Thanks,
Harendra


More information about the ghc-devs mailing list