| 1 | //go:build windows |
| 2 | |
| 3 | package agent |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | "sync/atomic" |
| 9 | "unsafe" |
| 10 | |
| 11 | "reasonix/internal/store" |
| 12 | |
| 13 | "golang.org/x/sys/windows" |
| 14 | ) |
| 15 | |
| 16 | // tryLockSessionFile attempts the compatibility save lock once without |
| 17 | // blocking. The shared wrapper in save.go supplies the bounded retry window. |
| 18 | func tryLockSessionFile(path string) (func(), error) { |
| 19 | f, err := os.OpenFile(store.SessionLockFile(path), os.O_CREATE|os.O_RDWR, 0o600) |
| 20 | if err != nil { |
| 21 | if errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 22 | return nil, ErrSessionFileLockHeld |
| 23 | } |
| 24 | return nil, err |
| 25 | } |
| 26 | handle := windows.Handle(f.Fd()) |
| 27 | var overlapped windows.Overlapped |
| 28 | flags := uint32(windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY) |
| 29 | if err := windows.LockFileEx(handle, flags, 0, 1, 0, &overlapped); err != nil { |
| 30 | _ = f.Close() |
| 31 | if errors.Is(err, windows.ERROR_LOCK_VIOLATION) || errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 32 | return nil, ErrSessionFileLockHeld |
| 33 | } |
| 34 | return nil, err |
| 35 | } |
| 36 | return func() { |
| 37 | _ = windows.UnlockFileEx(handle, 0, 1, 0, &overlapped) |
| 38 | _ = f.Close() |
| 39 | }, nil |
| 40 | } |
| 41 | |
| 42 | // sessionLockFile is a non-blocking exclusive lock on a lock file itself, |
| 43 | // used by cleanup paths that may need to delete the file they locked. |
| 44 | type sessionLockFile struct { |
| 45 | handle windows.Handle |
| 46 | path string |
| 47 | overlapped windows.Overlapped |
| 48 | } |
| 49 | |
| 50 | // sessionLockDispositionFallbacks counts RemoveAndUnlock calls that could not |
| 51 | // delete through the held handle and fell back to a path-based remove. The |
| 52 | // fallback reopens the cleanup-vs-saver window, so tests pin it at zero. |
| 53 | var sessionLockDispositionFallbacks atomic.Int64 |
| 54 | |
| 55 | // tryTakeSessionLockFile opens lockPath and takes its exclusive LockFileEx |
| 56 | // region without blocking. A live holder surfaces as ErrSessionFileLockHeld. |
| 57 | // |
| 58 | // The handle asks for DELETE access up front: FileDispositionInfo requires it, |
| 59 | // and requesting it at open time keeps RemoveAndUnlock's deletion on the very |
| 60 | // handle that owns the lock. A sharing violation here means some process has |
| 61 | // the file open through Go's default share mode (which excludes DELETE) — for |
| 62 | // a lock file that is the same answer as losing the LockFileEx race. |
| 63 | func tryTakeSessionLockFile(lockPath string) (*sessionLockFile, error) { |
| 64 | pathp, err := windows.UTF16PtrFromString(lockPath) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | handle, err := windows.CreateFile(pathp, |
| 69 | windows.GENERIC_READ|windows.GENERIC_WRITE|windows.DELETE, |
| 70 | windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, |
| 71 | nil, windows.OPEN_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL, 0) |
| 72 | if err != nil { |
| 73 | if errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 74 | return nil, ErrSessionFileLockHeld |
| 75 | } |
| 76 | return nil, err |
| 77 | } |
| 78 | l := &sessionLockFile{handle: handle, path: lockPath} |
| 79 | flags := uint32(windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY) |
| 80 | if err := windows.LockFileEx(handle, flags, 0, 1, 0, &l.overlapped); err != nil { |
| 81 | _ = windows.CloseHandle(handle) |
| 82 | if errors.Is(err, windows.ERROR_LOCK_VIOLATION) { |
| 83 | return nil, ErrSessionFileLockHeld |
| 84 | } |
| 85 | return nil, err |
| 86 | } |
| 87 | return l, nil |
| 88 | } |
| 89 | |
| 90 | func (l *sessionLockFile) Unlock() { |
| 91 | _ = windows.UnlockFileEx(l.handle, 0, 1, 0, &l.overlapped) |
| 92 | _ = windows.CloseHandle(l.handle) |
| 93 | } |
| 94 | |
| 95 | // RemoveAndUnlock deletes the lock file atomically with the release. Windows |
| 96 | // refuses a path-based delete of a file this process still holds open, so the |
| 97 | // removal is expressed on the held handle instead: mark the delete |
| 98 | // disposition, then unlock and close. The name dies with the handle, leaving |
| 99 | // no window where another process could adopt a lock file that is already |
| 100 | // doomed. |
| 101 | func (l *sessionLockFile) RemoveAndUnlock() error { |
| 102 | // FILE_DISPOSITION_INFO with its BOOLEAN widened to a full word. |
| 103 | info := struct{ DeleteFile uint32 }{DeleteFile: 1} |
| 104 | dispErr := windows.SetFileInformationByHandle(l.handle, windows.FileDispositionInfo, |
| 105 | (*byte)(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info))) |
| 106 | l.Unlock() |
| 107 | if dispErr != nil { |
| 108 | // Delete disposition unsupported (exotic filesystem): fall back to a |
| 109 | // path-based remove after the release. A short adoption window beats |
| 110 | // leaving the sidecar behind forever. |
| 111 | sessionLockDispositionFallbacks.Add(1) |
| 112 | if err := os.Remove(l.path); err != nil && !os.IsNotExist(err) { |
| 113 | return err |
| 114 | } |
| 115 | } |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | func tryLockSessionLeaseFile(path string) (func(), error) { |
| 120 | f, err := os.OpenFile(store.SessionLeaseLock(path), os.O_CREATE|os.O_RDWR, 0o600) |
| 121 | if err != nil { |
| 122 | // Cleanup holds lease lock files with DELETE access for a moment; |
| 123 | // Go's default share mode cannot coexist with that, so the open |
| 124 | // itself reports the file busy. Same retryable answer as a held lock. |
| 125 | if errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 126 | return nil, ErrSessionLeaseHeld |
| 127 | } |
| 128 | return nil, err |
| 129 | } |
| 130 | handle := windows.Handle(f.Fd()) |
| 131 | var overlapped windows.Overlapped |
| 132 | flags := uint32(windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY) |
| 133 | if err := windows.LockFileEx(handle, flags, 0, 1, 0, &overlapped); err != nil { |
| 134 | _ = f.Close() |
| 135 | if errors.Is(err, windows.ERROR_LOCK_VIOLATION) { |
| 136 | return nil, ErrSessionLeaseHeld |
| 137 | } |
| 138 | return nil, err |
| 139 | } |
| 140 | return func() { |
| 141 | _ = windows.UnlockFileEx(handle, 0, 1, 0, &overlapped) |
| 142 | _ = f.Close() |
| 143 | }, nil |
| 144 | } |
| 145 |