Comments (8)
It might be worth comparing code to https://github.com/joeyh/git-annex/blob/master/Utility/LockFile/Windows.hs
from filelock.
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 lockAndWriteFile
is given a file path which has never been used on the system before. It is not a matter of an external process already having a lock on the file (although could multiple threads in this test program cause it?)
from filelock.
The latest commit (jmitchell@24857fb) includes tracing in System.FileLock
, System.FileLock.Internal.LockFileEx
, and the test file prefixed with the labels FL
, LFE
, and Test
, respectively.
Over several runs I'm consistently getting this same output from bin\test-filelock.exe
(and exit code 1):
Test.lockAndWriteFile: start
FL.withLockFile: start
FL.lockFile: start
LFE.lock: start
LFE.open: start
LFE.open: end
LFE.lockFirstByte: start
LFE.lockFirstByte: end (success)
LFE.lock: end
FL.newLock: start
FL.newLock: end
FL.lockFile: end
attempting write...
FL.unlockFile: start
LFE.unlock: start
LFE.unlock: end
FL.unlockFile: end
test-filelock.exe: testLockFile.txt: hClose: permission denied (Permission denied)
As I'd expect, the attempted write occurs after FL.lockFile
has returned a FileLock
and before FL.unlock
has unlocked that FileLock
.
from filelock.
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 Handle
. Unfortunately I haven't had a chance to implement this.
from filelock.
I'd say actionable item from this issue would be to improve documentation.
from filelock.
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.
from filelock.
Currently the doc on System.FileLock says:
It is not recommended to open or otherwise use lock files for other purposes, because it tends to expose differences between operating systems. For example, on Windows openFile for a lock file will fail when the lock is held, but on Unix it won't.
If you have an idea on how to improve the doc situation, feel free to open a pull request.
from filelock.
Maybe the lock file could be an internal implementation detail for the library. Something like passing a filepath
would result into filepath.XXX
lock file where XXX would be a hash of filepath.
For now, I'd mention that on windows it will result into permission denied
error and mark it documentation as warning/note.
from filelock.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from filelock.