You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this is expected behaviour for this package or not, it was unexpected to me at least, but if I pass a file path that doesn't exist, I will successfully be able to acquire a lock and then when reading the contents of the file it returns empty content.
The reason this was unexpected was that in my project I have a test that validates an error is returned if a path fails to be read, but it was failing because it expected an error and now (since implementing this package) it was no longer getting an error because the file did exist (and for my use case that was unexpected).
An example of what I'm describing is:
package main
import (
"context""fmt""os""time""github.com/gofrs/flock"
)
const (
// FileLockTimeout is the amount of time to wait trying to acquire a lock.FileLockTimeout=10*time.Second// FileLockRetryDelay is the mount of time to wait before attempting a retry.FileLockRetryDelay=500*time.Millisecond
)
funcmain() {
filename:="does_not_exist"fileLock:=flock.New(filename)
lockCtx, cancel:=context.WithTimeout(context.Background(), FileLockTimeout)
defercancel()
locked, err:=fileLock.TryLockContext(lockCtx, FileLockRetryDelay)
iferr!=nil {
fmt.Printf("error acquiring file lock for '%s': %v", fileLock.Path(), err)
return
}
iflocked {
fmt.Println("got a lock", fileLock.Path())
fi, err:=os.Stat(fileLock.Path())
iferr!=nil {
fmt.Printf("error stating file '%s': %v", fileLock.Path(), err)
return
}
fmt.Printf("%+v\n", fi) // seems the file now existsdata, err:=os.ReadFile(fileLock.Path())
iferr!=nil {
fmt.Printf("error reading file '%s': %v", fileLock.Path(), err)
return
}
fmt.Printf("file content: %+v\n", string(data)) // empty file contentiferr:=fileLock.Unlock(); err!=nil {
fmt.Printf("error releasing file lock for '%s': %v", fileLock.Path(), err)
}
}
}
The solution in my case was to add a .Stat() call up front before calling flock.New() but I wanted to be sure this wasn't a bug. Maybe it's I just don't understand how file locks are supposed to work (very likely).
Any feedback/guidance appreciated.
Thanks!
The text was updated successfully, but these errors were encountered:
👋🏻
I'm not sure if this is expected behaviour for this package or not, it was unexpected to me at least, but if I pass a file path that doesn't exist, I will successfully be able to acquire a lock and then when reading the contents of the file it returns empty content.
The reason this was unexpected was that in my project I have a test that validates an error is returned if a path fails to be read, but it was failing because it expected an error and now (since implementing this package) it was no longer getting an error because the file did exist (and for my use case that was unexpected).
An example of what I'm describing is:
The solution in my case was to add a
.Stat()
call up front before callingflock.New()
but I wanted to be sure this wasn't a bug. Maybe it's I just don't understand how file locks are supposed to work (very likely).Any feedback/guidance appreciated.
Thanks!
The text was updated successfully, but these errors were encountered: