gofrs / flock

Thread-safe file locking library in Go
https://pkg.go.dev/github.com/gofrs/flock
BSD 3-Clause "New" or "Revised" License
578 stars 66 forks source link

Fix exclusive lock to work on Linux+NFS #22

Closed virtuald closed 6 years ago

virtuald commented 6 years ago

I noticed that trying to obtain an exclusive lock on a file sitting on an NFS system did not work, and returned an error 'invalid file descriptor'. When debugging, I noticed that the flock command line tool was able to work without any errors, so I took a look at the source code.

https://github.com/karelzak/util-linux/blob/05541825553524e2ac353eb6c62c8b5ad049de24/sys-utils/flock.c#L294

The fix present there is to open the file in read+write mode, then try again.

virtuald commented 6 years ago

The way I've tested it is by setting up two VMs with an NFS4 share between them. Here's a Vagrantfile that you can use to set up two VMs with NFS4 automatically that I've verified the fix on: https://gist.github.com/virtuald/af30557554ea839c6d00fc2386bc0a58

However, seems like a lot of extra work to test this, and I don't really want to take more time to do that. Feel free to do so.

theckman commented 6 years ago

Thank you again. I merged and released this under v0.5.0.