Skip to content

Commit 72059a4

Browse files
authored
Return original error when WaitLock times out (#47)
* Return original error when WaitLock times out * WaitLock logs when timeout or context canceled
1 parent 16843c3 commit 72059a4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

fslock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ retry:
104104
}
105105
select {
106106
case <-ctx.Done():
107-
return nil, ctx.Err()
107+
log.Warnf("did not acquire lock: %s", ctx.Err())
108+
return nil, err
108109
case <-ticker.C:
109110
goto retry
110111
}

fslock_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fslock_test
33
import (
44
"bufio"
55
"context"
6-
"errors"
76
"os"
87
"os/exec"
98
"path/filepath"
@@ -158,6 +157,7 @@ func TestWaitLock(t *testing.T) {
158157
someFile = "somefile"
159158
permErr = "permission denied"
160159
heldErr = "lock is already held by us"
160+
wantErr = "someone else has the lock"
161161
)
162162

163163
confdir := t.TempDir()
@@ -259,7 +259,11 @@ func TestWaitLock(t *testing.T) {
259259
if err == nil {
260260
t.Fatalf("parent successfully acquired the lock")
261261
}
262-
if !errors.Is(err, context.DeadlineExceeded) {
263-
t.Fatalf("did not get expected error: %s", context.DeadlineExceeded)
262+
pe, ok = err.(*os.PathError)
263+
if !ok {
264+
t.Fatalf("wrong error type %T", err)
265+
}
266+
if got := pe.Error(); !strings.Contains(got, wantErr) {
267+
t.Fatalf("error %q does not contain %q", got, wantErr)
264268
}
265269
}

0 commit comments

Comments
 (0)