Files
wehub-resource-sync f99010fae1
Desktop Artifacts / Desktop Build (Linux) (push) Waiting to run
Desktop Artifacts / Desktop Build (Windows) (push) Waiting to run
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Waiting to run
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Waiting to run
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Waiting to run
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

93 lines
2.5 KiB
Go

package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/gofrs/flock"
)
const writeOwnerLockFile = "db.write.lock"
// vectorsWriteLockFile guards direct (non-daemon) writes to vectors.db —
// `embeddings build/activate/retire` take it via tryAcquireNamedLock so two
// concurrent direct-mode invocations cannot race each other. It is separate
// from writeOwnerLockFile because the two resources (sessions.db vs
// vectors.db) are written independently.
const vectorsWriteLockFile = "vectors.write.lock"
type writeOwnerLock struct {
path string
lock *flock.Flock
}
func writeOwnerLockPath(dataDir string) string {
return filepath.Join(dataDir, writeOwnerLockFile)
}
func acquireWriteOwnerLock(
ctx context.Context,
dataDir string,
) (*writeOwnerLock, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return tryAcquireWriteOwnerLock(dataDir)
}
func tryAcquireWriteOwnerLock(dataDir string) (*writeOwnerLock, error) {
return tryAcquireNamedLock(dataDir, writeOwnerLockFile)
}
// tryAcquireNamedLock acquires an exclusive flock named filename inside
// dataDir. It backs both tryAcquireWriteOwnerLock (db.write.lock, guarding
// direct sessions.db writes) and the embeddings CLI's direct path
// (vectors.write.lock, guarding direct vectors.db writes), so two direct
// (non-daemon) writers targeting the same resource cannot race each other.
// OS flock semantics release the lock when the owning process exits or
// crashes, which is the direct-writer recovery path after stale runtime
// records are ignored.
func tryAcquireNamedLock(dataDir, filename string) (*writeOwnerLock, error) {
if err := os.MkdirAll(dataDir, 0o700); err != nil {
return nil, fmt.Errorf("creating data dir for write lock: %w", err)
}
path := filepath.Join(dataDir, filename)
lock := flock.New(path)
locked, err := lock.TryLock()
if err != nil {
return nil, fmt.Errorf("acquiring write lock %s: %w", path, err)
}
if !locked {
return nil, writeOwnerLockHeldError{path: path}
}
return &writeOwnerLock{path: path, lock: lock}, nil
}
func (l *writeOwnerLock) Close() error {
if l == nil || l.lock == nil {
return nil
}
if err := l.lock.Unlock(); err != nil {
return fmt.Errorf("releasing write lock %s: %w", l.path, err)
}
return nil
}
type writeOwnerLockHeldError struct {
path string
}
func (e writeOwnerLockHeldError) Error() string {
return fmt.Sprintf(
"write lock %s is held by another process; run `agentsview daemon stop`, "+
"wait for the daemon idle timeout, or retry after the offline "+
"operation finishes",
e.path,
)
}