openFile gives "file is locked" error on Linux when creating a non-existing file
Tom Smeding
x at tomsmeding.com
Tue Oct 8 07:06:48 UTC 2024
Inspired by Viktor's latest email in the thread:
> Any pointers or more debug info to generate to debug this?
Given that this happens on Linux, it would be nice to have `strace`
output on the program. If you know precisely which executable does this,
`strace -o log.txt <the normal command to start that program>` suffices;
additionally passing `-f` to strace instructs it to also trace child
processes, at the cost of (usually) significantly blowing up the size of
the trace.
Then we can see exactly which system call fails, and what happened right
before.
- Tom
On 07/10/2024 04:55, Harendra Kumar wrote:
> 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
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
More information about the ghc-devs
mailing list