Skip to content

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

:bug: Prevent database indexing deadlock during boot sync https://github.com/siyuan-note/siyuan/issues/19221

Danielcb20ce30af3d
diff --git a/kernel/av/av.go b/kernel/av/av.go
index b8aea597f8..d86a260061 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -689,7 +689,7 @@ func GetAttributeViewContent(avID string) (content string) {
return
}
- attrView, err := ParseAttributeView(avID)
+ attrView, err := parseAttributeView(avID, false)
if err != nil {
logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
return
@@ -701,7 +701,7 @@ func GetAttributeViewContent(avID string) (content string) {
}
func GetAttributeViewContentByPath(avJSONPath string) (content string) {
- attrView, err := ParseAttributeViewByPath(avJSONPath)
+ attrView, err := parseAttributeViewByPathInBoxWithOptions(avJSONPath, avBoxIDFromPath(avJSONPath), false)
if err != nil {
logging.LogErrorf("parse attribute view [%s] failed: %s", avJSONPath, err)
return
@@ -746,6 +746,10 @@ func IsAttributeViewExist(avID string) bool {
}
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
+ return parseAttributeView(avID, true)
+}
+
+func parseAttributeView(avID string, resolveColors bool) (ret *AttributeView, err error) {
if !ast.IsNodeIDPattern(avID) {
err = ErrInvalidAttributeViewID
return
@@ -756,15 +760,24 @@ func ParseAttributeView(avID string) (ret *AttributeView, err error) {
if avJSONPath == "" {
// 文件不存在,可能是首次创建,按全局路径返回(由调用方处理)
avJSONPath = GetAttributeViewDataPath(avID)
- return parseAttributeViewByPathInBox(avJSONPath, "")
+ return parseAttributeViewByPathInBoxWithOptions(avJSONPath, "", resolveColors)
}
if boxID != "" {
SetAVBoxID(avID, boxID)
}
- return parseAttributeViewByPathInBox(avJSONPath, boxID)
+ return parseAttributeViewByPathInBoxWithOptions(avJSONPath, boxID, resolveColors)
}
func ParseAttributeViewInBox(avID, boxID string) (ret *AttributeView, err error) {
+ return parseAttributeViewInBox(avID, boxID, true)
+}
+
+// ParseAttributeViewForIndexInBox 读取索引所需的数据并保留解密认证,不解析显示配色,避免等待同步自身结束。
+func ParseAttributeViewForIndexInBox(avID, boxID string) (ret *AttributeView, err error) {
+ return parseAttributeViewInBox(avID, boxID, false)
+}
+
+func parseAttributeViewInBox(avID, boxID string, resolveColors bool) (ret *AttributeView, err error) {
if !ast.IsNodeIDPattern(avID) {
err = ErrInvalidAttributeViewID
return
@@ -784,7 +797,7 @@ func ParseAttributeViewInBox(avID, boxID string) (ret *AttributeView, err error)
SetAVBoxID(avID, boxID)
}
}
- return parseAttributeViewByPathInBox(avJSONPath, avBoxID)
+ return parseAttributeViewByPathInBoxWithOptions(avJSONPath, avBoxID, resolveColors)
}
func ParseAttributeViewByPath(avJSONPath string) (ret *AttributeView, err error) {
diff --git a/kernel/model/attribute_view_ref_index.go b/kernel/model/attribute_view_ref_index.go
index 49fc746968..8361c131a1 100644
--- a/kernel/model/attribute_view_ref_index.go
+++ b/kernel/model/attribute_view_ref_index.go
@@ -34,7 +34,7 @@ func queueAttributeViewRefIndex(avID, avBoxID string) {
if "" == avID {
return
}
- attrView, err := av.ParseAttributeViewInBox(avID, avBoxID)
+ attrView, err := av.ParseAttributeViewForIndexInBox(avID, avBoxID)
if nil != err || nil == attrView || av.RichTextSpec > attrView.Spec {
return
}
@@ -48,7 +48,7 @@ func queueExternalAttributeViewRefIndex(avID, avBoxID string) {
return
}
var newDefIDs []string
- if attrView, err := av.ParseAttributeViewInBox(avID, avBoxID); nil == err && nil != attrView &&
+ if attrView, err := av.ParseAttributeViewForIndexInBox(avID, avBoxID); nil == err && nil != attrView &&
av.RichTextSpec <= attrView.Spec {
newDefIDs = attributeViewRichTextRefDefIDs(attrView)
}
diff --git a/kernel/model/attribute_view_ref_index_test.go b/kernel/model/attribute_view_ref_index_test.go
index 75990d7b16..6d8e6bd163 100644
--- a/kernel/model/attribute_view_ref_index_test.go
+++ b/kernel/model/attribute_view_ref_index_test.go
@@ -78,6 +78,14 @@ func TestAttributeViewRefCarrierTreesRequirePhysicalCarrier(t *testing.T) {
if trees := attributeViewRefCarrierTrees(attributeView, ""); 0 != len(trees) {
t.Fatalf("attribute view without a carrier produced index trees: %+v", trees)
}
+ originalPalette := av.LoadWorkspacePalette
+ av.LoadWorkspacePalette = func() ([]*av.AttributeViewCustomColor, []string) {
+ t.Fatal("reference index scheduling must not load the workspace palette")
+ return nil, nil
+ }
+ t.Cleanup(func() { av.LoadWorkspacePalette = originalPalette })
+ queueAttributeViewRefIndex(attributeView, "")
+ queueExternalAttributeViewRefIndexByRepoPath("/storage/av/" + attributeView + ".json")
}
func TestAttributeViewRichTextRefDefIDs(t *testing.T) {
diff --git a/kernel/model/boot_sync_attribute_view_fts5_test.go b/kernel/model/boot_sync_attribute_view_fts5_test.go
new file mode 100644
index 0000000000..e2c1fb4273
--- /dev/null
+++ b/kernel/model/boot_sync_attribute_view_fts5_test.go
@@ -0,0 +1,86 @@
+//go:build fts5
+
+package model
+
+import (
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/dejavu"
+ "github.com/siyuan-note/dejavu/entity"
+ "github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/sql"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestBootSyncAttributeViewIndex(t *testing.T) {
+ const childEnv = "SIYUAN_TEST_BOOT_SYNC_AV_INDEX"
+ if os.Getenv(childEnv) == "" {
+ cmd := exec.Command(os.Args[0], "-test.run=^TestBootSyncAttributeViewIndex$", "-test.timeout=30s")
+ cmd.Env = append(os.Environ(), childEnv+"=1")
+ if output, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("boot sync subprocess failed: %v\n%s", err, output)
+ }
+ return
+ }
+ fixture, _, _, _ := prepareAssetDownloadDocumentTest(t)
+ setupAttributeViewRefI18n(t)
+ util.QueueDir = filepath.Join(util.TempDir, "queue")
+ util.DBPath = filepath.Join(util.TempDir, util.DBName)
+ util.HistoryDBPath = filepath.Join(util.TempDir, "history.db")
+ util.AssetContentDBPath = filepath.Join(util.TempDir, "asset_content.db")
+ if err := os.MkdirAll(util.TempDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ sql.InitDatabase(true)
+ sql.InitHistoryDatabase(true)
+ sql.InitAssetContentDatabase(true)
+ t.Cleanup(sql.CloseDatabase)
+ const avID = "20260909170000-av00001"
+ if err := av.SaveAttributeView(av.NewAttributeView(avID)); err != nil {
+ t.Fatal(err)
+ }
+ tree := treenode.NewTree(fixture.box.ID, "/20260909170001-doc0001.sy", "/RemoteTitle", "RemoteTitle")
+ tree.Root.AppendChild(&ast.Node{Type: ast.NodeAttributeView, ID: "20260909170002-db00001", AttributeViewID: avID})
+ writeAssetDownloadDocumentTest(t, tree)
+
+ // 启动同步标志只能在后处理返回后清除;超时解除等待,使回归失败能够正常报告。
+ originalBootSyncing := isBootSyncing.Swap(true)
+ var timedOut atomic.Bool
+ watchdogDone := make(chan struct{})
+ watchdog := time.AfterFunc(5*time.Second, func() {
+ stack := make([]byte, 1024*1024)
+ t.Logf("blocked boot sync stacks:\n%s", stack[:runtime.Stack(stack, true)])
+ timedOut.Store(true)
+ isBootSyncing.Store(false)
+ close(watchdogDone)
+ })
+ stopWatchdog := sync.OnceFunc(func() {
+ if !watchdog.Stop() {
+ <-watchdogDone
+ }
+ isBootSyncing.Store(originalBootSyncing)
+ })
+ t.Cleanup(stopWatchdog)
+ processSyncMergeResult(false, false, &dejavu.MergeResult{Upserts: []*entity.File{
+ {Path: "/" + tree.Box + tree.Path},
+ {Path: "/storage/av/" + avID + ".json"},
+ }}, &dejavu.TrafficStat{}, "a", 0, true)
+ // 数据库定义变更追加的引用索引也必须在启动同步期间完成。
+ sql.FlushQueue()
+ stopWatchdog()
+ if timedOut.Load() {
+ t.Fatal("attribute view indexing waited for boot synchronization to finish")
+ }
+ if block := sql.GetBlock(tree.ID); block == nil || block.Content != "RemoteTitle" {
+ t.Fatalf("synced document was not indexed: %+v", block)
+ }
+}
diff --git a/kernel/sql/attribute_view_ref.go b/kernel/sql/attribute_view_ref.go
index 6843a9bc8f..7760329b22 100644
--- a/kernel/sql/attribute_view_ref.go
+++ b/kernel/sql/attribute_view_ref.go
@@ -32,7 +32,7 @@ func attributeViewRefsFromNode(tree *parse.Tree, databaseNode *ast.Node) (ret []
}
avBoxID := attributeViewRefStorageBoxID(tree.Box)
- attrView, err := av.ParseAttributeViewInBox(databaseNode.AttributeViewID, avBoxID)
+ attrView, err := av.ParseAttributeViewForIndexInBox(databaseNode.AttributeViewID, avBoxID)
if nil != err || nil == attrView {
return
}
diff --git a/kernel/sql/attribute_view_ref_test.go b/kernel/sql/attribute_view_ref_test.go
index 600ce4c9db..77f4aa8257 100644
--- a/kernel/sql/attribute_view_ref_test.go
+++ b/kernel/sql/attribute_view_ref_test.go
@@ -117,6 +117,12 @@ func TestRefsFromTreeIncludesAttributeViewRichTextReferences(t *testing.T) {
}
root := &ast.Node{Type: ast.NodeDocument, ID: rootID}
+ originalPalette := av.LoadWorkspacePalette
+ av.LoadWorkspacePalette = func() ([]*av.AttributeViewCustomColor, []string) {
+ t.Fatal("reference indexing must not load the workspace palette")
+ return nil, nil
+ }
+ t.Cleanup(func() { av.LoadWorkspacePalette = originalPalette })
databaseNode := &ast.Node{Type: ast.NodeAttributeView, ID: databaseID, AttributeViewID: attributeView}
root.AppendChild(databaseNode)
tree := &parse.Tree{Root: root, ID: rootID, Box: "20260904009004-box0001", Path: "/" + rootID + ".sy"}