Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve workspace lock file identity after release https://github.com/siyuan-note/siyuan/issues/20959

Danielf56c497ec2ef
diff --git a/kernel/util/working.go b/kernel/util/working.go
index 07295436f5..503c79fc5d 100644
--- a/kernel/util/working.go
+++ b/kernel/util/working.go
@@ -667,10 +667,8 @@ func UnlockWorkspace() {
return
}
- if err := os.Remove(filepath.Join(WorkspaceDir, ".lock")); err != nil {
- logging.LogErrorf("remove workspace lock failed: %s", err)
- return
- }
+ // Keep the lock file identity stable: a new kernel may already hold this file.
+ // Unlinking it would let another kernel create and lock a different file.
}
func LogDatabaseSize(dbPath string) {
diff --git a/kernel/util/working_workspace_test.go b/kernel/util/working_workspace_test.go
index ff5bd85ba5..7bb53b1241 100644
--- a/kernel/util/working_workspace_test.go
+++ b/kernel/util/working_workspace_test.go
@@ -56,3 +56,48 @@ func TestBootWorkspaceTempCleanup(t *testing.T) {
})
}
}
+
+func TestUnlockWorkspacePreservesLockIdentity(t *testing.T) {
+ previousDir, previousLock := WorkspaceDir, WorkspaceLock
+ t.Cleanup(func() { WorkspaceDir, WorkspaceLock = previousDir, previousLock })
+ WorkspaceDir = t.TempDir()
+ filename := filepath.Join(WorkspaceDir, ".lock")
+ WorkspaceLock = flock.New(filename)
+ if ok, err := WorkspaceLock.TryLock(); err != nil || !ok {
+ t.Fatalf("first kernel failed to lock: %v, %v", ok, err)
+ }
+ t.Cleanup(func() { _ = WorkspaceLock.Unlock() })
+ original, err := os.Stat(filename)
+ if err != nil {
+ t.Fatal(err)
+ }
+ UnlockWorkspace()
+ if IsWorkspaceLocked(WorkspaceDir) {
+ t.Fatal("released workspace is still reported as locked")
+ }
+ next := flock.New(filename)
+ if ok, err := next.TryLock(); err != nil || !ok {
+ t.Fatalf("next kernel failed to lock: %v, %v", ok, err)
+ }
+ defer next.Unlock()
+ // A repeated cleanup from the old owner must leave the new owner protected.
+ UnlockWorkspace()
+ current, err := os.Stat(filename)
+ if err != nil || !os.SameFile(original, current) {
+ t.Fatalf("unlock replaced or removed the lock file: %v", err)
+ }
+ if !IsWorkspaceLocked(WorkspaceDir) {
+ t.Fatal("next kernel's lock was bypassed")
+ }
+ third := flock.New(filename)
+ defer third.Unlock()
+ if ok, err := third.TryLock(); err != nil || ok {
+ t.Fatalf("third kernel bypassed the held lock: %v, %v", ok, err)
+ }
+ if err = next.Unlock(); err != nil {
+ t.Fatal(err)
+ }
+ if ok, err := third.TryLock(); err != nil || !ok {
+ t.Fatalf("lock cannot be reused after handoff: %v, %v", ok, err)
+ }
+}