Skip to content

Commit

Permalink
Merge pull request #16 from lujinda/master
Browse files Browse the repository at this point in the history
Fix the error of Close operation under windows
  • Loading branch information
alexflint authored Mar 23, 2022
2 parents ccc5b75 + f0f7278 commit 8d142ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions filemutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ func TestClose(t *testing.T) {
m.Close()
}

func TestOnlyClose(t *testing.T) {
dir, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(dir)

path := filepath.Join(dir, "x")
m, err := New(path)
require.NoError(t, err)

require.NoError(t, m.Close())
}

func TestLockErrorsAreRecoverable(t *testing.T) {
dir, err := ioutil.TempDir("", "")
require.NoError(t, err)
Expand Down
11 changes: 9 additions & 2 deletions filemutex_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

package filemutex

import "golang.org/x/sys/windows"
import (
"syscall"

"golang.org/x/sys/windows"
)

// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
var errLockUnlocked syscall.Errno = 0x9E

// FileMutex is similar to sync.RWMutex, but also synchronizes across processes.
// This implementation is based on flock syscall.
Expand Down Expand Up @@ -51,7 +58,7 @@ func (m *FileMutex) RUnlock() error {

// Close unlocks the lock and closes the underlying file descriptor.
func (m *FileMutex) Close() error {
if err := windows.UnlockFileEx(m.fd, 0, 1, 0, &windows.Overlapped{}); err != nil {
if err := windows.UnlockFileEx(m.fd, 0, 1, 0, &windows.Overlapped{}); err != nil && err != errLockUnlocked {
return err
}
return windows.Close(m.fd)
Expand Down

0 comments on commit 8d142ea

Please sign in to comment.