Skip to content

Commit 9de625d

Browse files
crazy-maxldez
andauthored
fix: missing read-write flag in reopenFDOnError (#95)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent ffdd9d4 commit 9de625d

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

flock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ func tryCtx(ctx context.Context, fn func() (bool, error), retryDelay time.Durati
163163
}
164164
}
165165

166-
func (f *Flock) setFh() error {
166+
func (f *Flock) setFh(flag int) error {
167167
// open a new os.File instance
168-
fh, err := os.OpenFile(f.path, f.flag, f.perm)
168+
fh, err := os.OpenFile(f.path, flag, f.perm)
169169
if err != nil {
170170
return err
171171
}

flock_unix.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package flock
99

1010
import (
1111
"errors"
12+
"os"
1213

1314
"golang.org/x/sys/unix"
1415
)
@@ -49,7 +50,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
4950
}
5051

5152
if f.fh == nil {
52-
if err := f.setFh(); err != nil {
53+
if err := f.setFh(f.flag); err != nil {
5354
return err
5455
}
5556

@@ -146,7 +147,7 @@ func (f *Flock) try(locked *bool, flag int) (bool, error) {
146147
}
147148

148149
if f.fh == nil {
149-
if err := f.setFh(); err != nil {
150+
if err := f.setFh(f.flag); err != nil {
150151
return false, err
151152
}
152153

@@ -182,6 +183,7 @@ retry:
182183
// This comes from `util-linux/sys-utils/flock.c`:
183184
// > Since Linux 3.4 (commit 55725513)
184185
// > Probably NFSv4 where flock() is emulated by fcntl().
186+
// > https://github.com/util-linux/util-linux/blob/198e920aa24743ef6ace4e07cf6237de527f9261/sys-utils/flock.c#L374-L390
185187
func (f *Flock) reopenFDOnError(err error) (bool, error) {
186188
if !errors.Is(err, unix.EIO) && !errors.Is(err, unix.EBADF) {
187189
return false, nil
@@ -198,8 +200,8 @@ func (f *Flock) reopenFDOnError(err error) (bool, error) {
198200

199201
f.resetFh()
200202

201-
// reopen the file handle
202-
err = f.setFh()
203+
// reopen in read-write mode and set the file handle
204+
err = f.setFh(f.flag | os.O_RDWR)
203205
if err != nil {
204206
return false, err
205207
}

flock_unix_fcntl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (f *Flock) lock(locked *bool, flag lockType) error {
112112
}
113113

114114
if f.fh == nil {
115-
if err := f.setFh(); err != nil {
115+
if err := f.setFh(f.flag); err != nil {
116116
return err
117117
}
118118

@@ -359,7 +359,7 @@ func (f *Flock) try(locked *bool, flag lockType) (bool, error) {
359359
}
360360

361361
if f.fh == nil {
362-
if err := f.setFh(); err != nil {
362+
if err := f.setFh(f.flag); err != nil {
363363
return false, err
364364
}
365365

flock_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (f *Flock) lock(locked *bool, flag uint32) error {
5656
}
5757

5858
if f.fh == nil {
59-
if err := f.setFh(); err != nil {
59+
if err := f.setFh(f.flag); err != nil {
6060
return err
6161
}
6262

@@ -136,7 +136,7 @@ func (f *Flock) try(locked *bool, flag uint32) (bool, error) {
136136
}
137137

138138
if f.fh == nil {
139-
if err := f.setFh(); err != nil {
139+
if err := f.setFh(f.flag); err != nil {
140140
return false, err
141141
}
142142

0 commit comments

Comments
 (0)