From 9953ca366254f1e4e7ea73befb58c3cda1ba6c70 Mon Sep 17 00:00:00 2001 From: Ulf Saran Date: Fri, 5 Jan 2024 15:42:26 +0100 Subject: [PATCH] fix: Calling Close fails when running in Wine --- filemutex_windows.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/filemutex_windows.go b/filemutex_windows.go index 468b9f0..5f84a64 100644 --- a/filemutex_windows.go +++ b/filemutex_windows.go @@ -5,14 +5,9 @@ package filemutex 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. type FileMutex struct { @@ -58,8 +53,11 @@ 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 && err != errLockUnlocked { - return err - } + // See comment section of https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex + // It's recommended to unlock a file explicitly before closing in order to + // avoid delays, but all locks are definitly unlocked when closing a file. + // So any unlocking error can be ignored. + _ = windows.UnlockFileEx(m.fd, 0, 1, 0, &windows.Overlapped{}) + return windows.Close(m.fd) }