mirror of
https://gitea.com/gitea/tea.git
synced 2026-02-22 06:13:32 +01:00
Add locking to ensure safe concurrent access to config file (#881)
Reviewed-on: https://gitea.com/gitea/tea/pulls/881 Co-authored-by: techknowlogick <techknowlogick@gitea.com> Co-committed-by: techknowlogick <techknowlogick@gitea.com>
This commit is contained in:
committed by
techknowlogick
parent
0d5bf60632
commit
ae9eb4f2c0
48
modules/config/lock_windows.go
Normal file
48
modules/config/lock_windows.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//go:build windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// lockFile acquires an exclusive lock on the file using LockFileEx.
|
||||
// It polls with non-blocking LockFileEx until timeout.
|
||||
func lockFile(file *os.File, timeout time.Duration) error {
|
||||
deadline := time.Now().Add(timeout)
|
||||
handle := windows.Handle(file.Fd())
|
||||
|
||||
// LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY
|
||||
const flags = windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY
|
||||
|
||||
for {
|
||||
// Lock the first byte (advisory lock)
|
||||
var overlapped windows.Overlapped
|
||||
err := windows.LockFileEx(handle, flags, 0, 1, 0, &overlapped)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if err != windows.ERROR_LOCK_VIOLATION {
|
||||
return fmt.Errorf("LockFileEx failed: %w", err)
|
||||
}
|
||||
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("timeout waiting for file lock")
|
||||
}
|
||||
time.Sleep(fileLockPollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
// unlockFile releases the lock on the file.
|
||||
func unlockFile(file *os.File) error {
|
||||
handle := windows.Handle(file.Fd())
|
||||
var overlapped windows.Overlapped
|
||||
return windows.UnlockFileEx(handle, 0, 1, 0, &overlapped)
|
||||
}
|
||||
Reference in New Issue
Block a user