Skip to content

Commit 3547d9f

Browse files
authored
Fix WAL flakiness on Windows (#254)
1 parent a67165e commit 3547d9f

File tree

3 files changed

+8
-60
lines changed

3 files changed

+8
-60
lines changed

vfs/os_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func osLock(file *os.File, typ int16, start, len int64, timeout time.Duration, d
6060
}
6161
var err error
6262
switch {
63-
case timeout < 0:
64-
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLKW, &lock)
6563
default:
6664
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
65+
case timeout < 0:
66+
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLKW, &lock)
6767
}
6868
return osLockErrorCode(err, def)
6969
}

vfs/os_windows.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,10 @@ func osWriteLock(file *os.File, start, len uint32, timeout time.Duration) _Error
135135
func osLock(file *os.File, flags, start, len uint32, timeout time.Duration, def _ErrorCode) _ErrorCode {
136136
var err error
137137
switch {
138-
case timeout == 0:
138+
default:
139139
err = osLockEx(file, flags|windows.LOCKFILE_FAIL_IMMEDIATELY, start, len)
140140
case timeout < 0:
141141
err = osLockEx(file, flags, start, len)
142-
default:
143-
err = osLockExTimeout(file, flags, start, len, timeout)
144142
}
145143
return osLockErrorCode(err, def)
146144
}
@@ -162,36 +160,6 @@ func osLockEx(file *os.File, flags, start, len uint32) error {
162160
0, len, 0, &windows.Overlapped{Offset: start})
163161
}
164162

165-
func osLockExTimeout(file *os.File, flags, start, len uint32, timeout time.Duration) error {
166-
event, err := windows.CreateEvent(nil, 1, 0, nil)
167-
if err != nil {
168-
return err
169-
}
170-
defer windows.CloseHandle(event)
171-
172-
fd := windows.Handle(file.Fd())
173-
overlapped := &windows.Overlapped{
174-
Offset: start,
175-
HEvent: event,
176-
}
177-
178-
err = windows.LockFileEx(fd, flags, 0, len, 0, overlapped)
179-
if err != windows.ERROR_IO_PENDING {
180-
return err
181-
}
182-
defer windows.CancelIoEx(fd, overlapped)
183-
184-
ms := (timeout + time.Millisecond - 1) / time.Millisecond
185-
rc, err := windows.WaitForSingleObject(event, uint32(ms))
186-
if rc == windows.WAIT_OBJECT_0 {
187-
return nil
188-
}
189-
if err != nil {
190-
return err
191-
}
192-
return windows.Errno(rc)
193-
}
194-
195163
func osLockErrorCode(err error, def _ErrorCode) _ErrorCode {
196164
if err == nil {
197165
return _OK

vfs/shm_windows.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"os"
99
"sync"
10-
"time"
1110

1211
"github.com/tetratelabs/wazero/api"
1312
"golang.org/x/sys/windows"
@@ -31,8 +30,6 @@ type vfsShm struct {
3130
sync.Mutex
3231
}
3332

34-
var _ blockingSharedMemory = &vfsShm{}
35-
3633
func (s *vfsShm) Close() error {
3734
// Unmap regions.
3835
for _, r := range s.regions {
@@ -46,19 +43,11 @@ func (s *vfsShm) Close() error {
4643

4744
func (s *vfsShm) shmOpen() _ErrorCode {
4845
if s.File == nil {
49-
path, err := windows.UTF16PtrFromString(s.path)
50-
if err != nil {
51-
return _CANTOPEN
52-
}
53-
h, err := windows.CreateFile(path,
54-
windows.GENERIC_READ|windows.GENERIC_WRITE,
55-
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE,
56-
nil, windows.OPEN_ALWAYS,
57-
windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_OVERLAPPED, 0)
46+
f, err := os.OpenFile(s.path, os.O_RDWR|os.O_CREATE, 0666)
5847
if err != nil {
5948
return _CANTOPEN
6049
}
61-
s.File = os.NewFile(uintptr(h), s.path)
50+
s.File = f
6251
}
6352
if s.fileLock {
6453
return _OK
@@ -72,7 +61,7 @@ func (s *vfsShm) shmOpen() _ErrorCode {
7261
return _IOERR_SHMOPEN
7362
}
7463
}
75-
rc := osReadLock(s.File, _SHM_DMS, 1, time.Millisecond)
64+
rc := osReadLock(s.File, _SHM_DMS, 1, 0)
7665
s.fileLock = rc == _OK
7766
return rc
7867
}
@@ -140,11 +129,6 @@ func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, ext
140129
}
141130

142131
func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) (rc _ErrorCode) {
143-
var timeout time.Duration
144-
if s.blocking {
145-
timeout = time.Millisecond
146-
}
147-
148132
switch {
149133
case flags&_SHM_LOCK != 0:
150134
defer s.shmAcquire(&rc)
@@ -156,9 +140,9 @@ func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) (rc _ErrorCode) {
156140
case flags&_SHM_UNLOCK != 0:
157141
return osUnlock(s.File, _SHM_BASE+uint32(offset), uint32(n))
158142
case flags&_SHM_SHARED != 0:
159-
return osReadLock(s.File, _SHM_BASE+uint32(offset), uint32(n), timeout)
143+
return osReadLock(s.File, _SHM_BASE+uint32(offset), uint32(n), 0)
160144
case flags&_SHM_EXCLUSIVE != 0:
161-
return osWriteLock(s.File, _SHM_BASE+uint32(offset), uint32(n), timeout)
145+
return osWriteLock(s.File, _SHM_BASE+uint32(offset), uint32(n), 0)
162146
default:
163147
panic(util.AssertErr())
164148
}
@@ -189,7 +173,3 @@ func (s *vfsShm) shmUnmap(delete bool) {
189173
os.Remove(s.path)
190174
}
191175
}
192-
193-
func (s *vfsShm) shmEnableBlocking(block bool) {
194-
s.blocking = block
195-
}

0 commit comments

Comments
 (0)