Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix locktuil on windows #2729

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions pkg/lockutil/lockutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package lockutil

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)

const parallel = 20

func TestWithDirLock(t *testing.T) {
dir := t.TempDir()
log := filepath.Join(dir, "log")

errc := make(chan error, 10)
for i := 0; i < parallel; i++ {
go func() {
err := WithDirLock(dir, func() error {
if _, err := os.Stat(log); err == nil {
return nil
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
logFile, err := os.OpenFile(log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o640)
if err != nil {
return err
}
defer logFile.Close()
if _, err := fmt.Fprintf(logFile, "writer %d\n", i); err != nil {
return err
}
return logFile.Close()
})
errc <- err
}()
}

for i := 0; i < parallel; i++ {
err := <-errc
if err != nil {
t.Error(err)
}
}

data, err := os.ReadFile(log)
if err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.Trim(string(data), "\n"), "\n")
if len(lines) != 1 {
t.Errorf("Expected one writer, got %v", lines)
}
}
25 changes: 21 additions & 4 deletions pkg/lockutil/lockutil_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,37 @@ var (
procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
)

const (
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same constant names used in windows docs to make it easy to find the flag in the docs or using search.

I see that the original code form bolt use nicer go style:
https://github.com/boltdb/bolt/blob/2f1ce7a837dcb8da3ec595b1dac9d0632f0f99e8/bolt_windows.go#L18

We can use this style.

)

func WithDirLock(dir string, fn func() error) error {
dirFile, err := os.OpenFile(dir+".lock", os.O_CREATE, 0o644)
if err != nil {
return err
}
defer dirFile.Close()
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
// 1 lock immediately
if err := lockFileEx(syscall.Handle(dirFile.Fd()), 1, 0, 1, 0, &syscall.Overlapped{}); err != nil {
if err := lockFileEx(
syscall.Handle(dirFile.Fd()), // hFile
LOCKFILE_EXCLUSIVE_LOCK, // dwFlags
0, // dwReserved
1, // nNumberOfBytesToLockLow
0, // nNumberOfBytesToLockHigh
&syscall.Overlapped{}, // lpOverlapped
); err != nil {
return fmt.Errorf("failed to lock %q: %w", dir, err)
}

defer func() {
if err := unlockFileEx(syscall.Handle(dirFile.Fd()), 0, 1, 0, &syscall.Overlapped{}); err != nil {
if err := unlockFileEx(
syscall.Handle(dirFile.Fd()), // hFile
0, // dwReserved
1, // nNumberOfBytesToLockLow
0, // nNumberOfBytesToLockHigh
&syscall.Overlapped{}, // lpOverlapped
); err != nil {
logrus.WithError(err).Errorf("failed to unlock %q", dir)
}
}()
Expand Down
Loading