Skip to content

Commit 2258078

Browse files
committed
Fix issue caused by calling F_SETLKW instead of F_SETLK
1 parent 75ec202 commit 2258078

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

flock_aix.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ const (
3434
writeLock lockType = unix.F_WRLCK
3535
)
3636

37+
type cmdType int
38+
39+
const (
40+
tryLock cmdType = unix.F_SETLK
41+
waitLock cmdType = unix.F_SETLKW
42+
)
43+
3744
type inode = uint64
3845

3946
type inodeLock struct {
@@ -90,15 +97,15 @@ func (f *Flock) lock(locked *bool, flag lockType) error {
9097
defer f.ensureFhState()
9198
}
9299

93-
if _, err := f.doLock(flag, true); err != nil {
100+
if _, err := f.doLock(waitLock, flag, true); err != nil {
94101
return err
95102
}
96103

97104
*locked = true
98105
return nil
99106
}
100107

101-
func (f *Flock) doLock(lt lockType, blocking bool) (bool, error) {
108+
func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
102109
// POSIX locks apply per inode and process, and the lock for an inode is
103110
// released when *any* descriptor for that inode is closed. So we need to
104111
// synchronize access to each inode internally, and must serialize lock and
@@ -143,10 +150,13 @@ func (f *Flock) doLock(lt lockType, blocking bool) (bool, error) {
143150
wait <- f
144151
}
145152

146-
err = setlkw(f.fh.Fd(), lt)
153+
err = setlkw(f.fh.Fd(), cmd, lt)
147154

148155
if err != nil {
149156
f.doUnlock()
157+
if cmd == tryLock && err == unix.EACCES {
158+
return false, nil
159+
}
150160
return false, err
151161
}
152162

@@ -186,7 +196,7 @@ func (f *Flock) doUnlock() (err error) {
186196
mu.Unlock()
187197

188198
if owner == f {
189-
err = setlkw(f.fh.Fd(), unix.F_UNLCK)
199+
err = setlkw(f.fh.Fd(), waitLock, unix.F_UNLCK)
190200
}
191201

192202
mu.Lock()
@@ -246,7 +256,7 @@ func (f *Flock) try(locked *bool, flag lockType) (bool, error) {
246256
defer f.ensureFhState()
247257
}
248258

249-
haslock, err := f.doLock(flag, false)
259+
haslock, err := f.doLock(tryLock, flag, false)
250260
if err != nil {
251261
return false, err
252262
}
@@ -255,10 +265,10 @@ func (f *Flock) try(locked *bool, flag lockType) (bool, error) {
255265
return haslock, nil
256266
}
257267

258-
// setlkw calls FcntlFlock with F_SETLKW for the entire file indicated by fd.
259-
func setlkw(fd uintptr, lt lockType) error {
268+
// setlkw calls FcntlFlock with cmd for the entire file indicated by fd.
269+
func setlkw(fd uintptr, cmd cmdType, lt lockType) error {
260270
for {
261-
err := unix.FcntlFlock(fd, unix.F_SETLKW, &unix.Flock_t{
271+
err := unix.FcntlFlock(fd, int(cmd), &unix.Flock_t{
262272
Type: int16(lt),
263273
Whence: io.SeekStart,
264274
Start: 0,

0 commit comments

Comments
 (0)