-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Windows: after acquiring a lock (even Exclusive
) process can't write to file
#2
Comments
Exclusive
) process can't write to file
jmitchell@ce03f14 illustrates the problem as concisely as possible. For convenience, the important part along with explanatory comments is copied below: lockAndWriteFile :: String -> String -> IO ()
lockAndWriteFile fname contents = do
withFileLock fname Exclusive $ \_ -> do
putStrLn "took exclusive lock; attempting write..."
-- Following line fails, printing "test-filelock.exe: testLockFile.txt:
-- hClose: permission denied (Permission denied)" and process exits
-- prematurely with exit code 1.
--
-- However, there are no issues when it's commented out.
writeFile fname contents
-- Never prints (unless writeFile line is commented out).
putStrLn "released exclusive lock" The observations hold even when |
This will help diagnose and fix takano-akio#2.
The latest commit (jmitchell@24857fb) includes tracing in Over several runs I'm consistently getting this same output from
As I'd expect, the attempted write occurs after |
It might be worth comparing code to https://github.com/joeyh/git-annex/blob/master/Utility/LockFile/Windows.hs |
I haven't looked into the details yet, but just a quick reply: this mode of operation (writing to the lock file while holding a lock) is not currently supported by the library. In fact, the only supported thing is to use lock files purely as inter-process mutexes. I'm aware that it is often useful to be able to write to a locked file. Perhaps there should be an interface for locking a |
System.FileLock doesn't support a process writing to a file it previously locked. Instead the file passed to `withFileLock`/`lockFile` is meant to be an inter-process mutex (see takano-akio/filelock#2 (comment)). This change protects multiple writers from concurrently modifying the secret key file (`secret.key` by default). It does so by using System.FileLock's inter-process mutexes as intended.
I'd say actionable item from this issue would be to improve documentation. |
Thank you for the quick response, @takano-akio. After understanding the supported use case better I implemented a roughly equivalent behavior using an independent lock file. Granted, this approach doesn't prevent another process from writing to the target file associated with the lock file, but it does prevent the process and its threads from unintentionally clobbering the file. I agree with @domenkozar that more documentation would have helped. Let me know if I can help. |
Currently the doc on System.FileLock says:
If you have an idea on how to improve the doc situation, feel free to open a pull request. |
Maybe the lock file could be an internal implementation detail for the library. Something like passing a For now, I'd mention that on windows it will result into |
I adapted
tests.hs
and provided directions to reproduce the problem in jmitchell@4a850dd.Is there a different way I should secure a lock, write to the locked file, and then release the lock?
I suspect what's happening is some low-level write/close operation is attempting to lock as well and causing a
FILE LOCK CONFLICT
. However, writing to a file without usingSystem.FileLock
results in noLockFile
operations according to Process Monitor. This suggests low-level file writing/closing operations behavior varies depending on the existence of a lock, and inexplicably re-lock when there's already a lock.It's possible
System.FileLock
's upstream APIs broke this use case, or perhaps there's an issue with the LockFileEx wrapper.The text was updated successfully, but these errors were encountered: