An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Lock workspace before clearing startup temporary files https://github.com/siyuan-note/siyuan/issues/20958
Daniel077d87aa0f9c
diff --git a/kernel/util/working.go b/kernel/util/working.goindex e9dae981c7..07295436f5 100644--- a/kernel/util/working.go+++ b/kernel/util/working.go@@ -163,6 +163,7 @@ func BootWithFlags(workspacePath, wdPath, port, readOnly, accessAuthCode, lang,// 工作空间仅允许被一个内核进程伺服tryLockWorkspace()+ clearWorkspaceStartupTemp()bootBanner := figure.NewColorFigure("SiYuan", "isometric3", "green", true)logging.LogInfo("\n" + bootBanner.String())@@ -359,15 +360,10 @@ func initWorkspaceDir(workspaceArg string) {TempDir = filepath.Join(WorkspaceDir, "temp")QueueDir = filepath.Join(TempDir, "queue")osTmpDir := filepath.Join(TempDir, "os")- os.RemoveAll(osTmpDir)if err := os.MkdirAll(osTmpDir, 0755); err != nil {logging.LogErrorf("create os tmp dir [%s] failed: %s", osTmpDir, err)os.Exit(logging.ExitCodeInitWorkspaceErr)}- os.RemoveAll(filepath.Join(TempDir, "repo"))- // export 目录只保存临时文件,启动时统一清理;插件不得依赖其中的文件跨进程存续。- os.RemoveAll(filepath.Join(TempDir, "export"))- os.RemoveAll(filepath.Join(TempDir, "clipboard"))os.Setenv("TMPDIR", osTmpDir)os.Setenv("TEMP", osTmpDir)os.Setenv("TMP", osTmpDir)@@ -379,6 +375,18 @@ func initWorkspaceDir(workspaceArg string) {ShortcutsPath = filepath.Join(userHomeConfDir, "shortcuts")}+// Only the kernel holding the workspace lock may discard previous-session files.+func clearWorkspaceStartupTemp() {+ for _, dir := range []string{"os", "repo", "export", "clipboard"} {+ os.RemoveAll(filepath.Join(TempDir, dir))+ }+ osTmpDir := filepath.Join(TempDir, "os")+ if err := os.MkdirAll(osTmpDir, 0755); err != nil {+ logging.LogErrorf("create os tmp dir [%s] failed: %s", osTmpDir, err)+ os.Exit(logging.ExitCodeInitWorkspaceErr)+ }+}+func DeduplicateWorkspacePaths(paths []string) []string {if !gulu.OS.IsWindows() {return gulu.Str.RemoveDuplicatedElem(paths)diff --git a/kernel/util/working_workspace_test.go b/kernel/util/working_workspace_test.gonew file mode 100644index 0000000000..ff5bd85ba5--- /dev/null+++ b/kernel/util/working_workspace_test.go@@ -0,0 +1,58 @@+package util++import (+ "os"+ "os/exec"+ "path/filepath"+ "testing"++ "github.com/gofrs/flock"+ "github.com/siyuan-note/logging"+)++func TestBootWorkspaceTempCleanup(t *testing.T) {+ for _, locked := range []bool{true, false} {+ t.Run(map[bool]string{true: "locked", false: "unlocked"}[locked], func(t *testing.T) {+ base := t.TempDir()+ workspace := filepath.Join(base, "workspace")+ var files []string+ for _, dir := range []string{"os", "repo", "export", "clipboard"} {+ filename := filepath.Join(workspace, "temp", dir, "active.txt")+ if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filename, []byte("active operation"), 0600); err != nil {+ t.Fatal(err)+ }+ files = append(files, filename)+ }+ if locked {+ lock := flock.New(filepath.Join(workspace, ".lock"))+ if ok, err := lock.TryLock(); err != nil || !ok {+ t.Fatalf("hold workspace lock: %v, %v", ok, err)+ }+ t.Cleanup(func() { _ = lock.Unlock() })+ }+ command := exec.Command(os.Args[0], "-test.run=^TestBootHomeDirProcess$", "--",+ "--home-dir="+filepath.Join(base, "home"), "--workspace="+workspace, "--wd="+base)+ command.Env = append(os.Environ(), "SIYUAN_TEST_BOOT_HOME="+filepath.Join(base, "system-home"))+ output, err := command.CombinedOutput()+ if locked {+ exit, ok := err.(*exec.ExitError)+ if !ok || exit.ExitCode() != logging.ExitCodeWorkspaceLocked {+ t.Fatalf("expected workspace lock rejection: %v\n%s", err, output)+ }+ } else if err != nil {+ t.Fatalf("boot failed: %v\n%s", err, output)+ }+ for _, filename := range files {+ data, err := os.ReadFile(filename)+ if locked && (err != nil || string(data) != "active operation") {+ t.Errorf("rejected startup changed %s: %q, %v", filename, data, err)+ } else if !locked && !os.IsNotExist(err) {+ t.Errorf("successful startup retained stale temp file %s: %v", filename, err)+ }+ }+ })+ }+}