Skip to content

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

:bug: Fix nil pointer crash when deleting a document bound to a database https://github.com/siyuan-note/siyuan/issues/18557

Danielfc71bcb9ba27
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 69f837c58b..3de011ffb2 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -1221,7 +1221,16 @@ func (tx *Transaction) doDelete0(operation *Operation, tree *parse.Tree) (delete
}
func syncDelete2AvBlock(node *ast.Node, nodeTree *parse.Tree, delChildrenWhenDelParent bool, tx *Transaction) {
- tx.collectDeletedAttributeViewBlocks(node, delChildrenWhenDelParent)
+ if nil == tx {
+ // 非事务调用(例如 RemoveDoc)无法批量收集,收集后立即清理已删除块的属性视图绑定行
+ // https://github.com/siyuan-note/siyuan/issues/18557
+ deletedAttrViewBlockIDs := map[string]map[string]struct{}{}
+ collectDeletedAttributeViewBlocks(node, delChildrenWhenDelParent, deletedAttrViewBlockIDs)
+ flushDeletedAttributeViewBlocks(deletedAttrViewBlockIDs)
+ } else {
+ tx.collectDeletedAttributeViewBlocks(node, delChildrenWhenDelParent)
+ }
+
for _, avID := range tx.syncDelete2Block(node, nodeTree) {
ReloadAttrView(avID)
}
@@ -1280,16 +1289,20 @@ func (tx *Transaction) syncDelete2Block(node *ast.Node, nodeTree *parse.Tree) (c
}
func (tx *Transaction) collectDeletedAttributeViewBlocks(node *ast.Node, delChildrenWhenDelParent bool) {
+ collectDeletedAttributeViewBlocks(node, delChildrenWhenDelParent, tx.deletedAttrViewBlockIDs)
+}
+
+func collectDeletedAttributeViewBlocks(node *ast.Node, delChildrenWhenDelParent bool, deletedAttrViewBlockIDs map[string]map[string]struct{}) {
collect := func(n *ast.Node) {
avs := n.IALAttr(av.NodeAttrNameAvs)
if "" == avs {
return
}
for avID := range strings.SplitSeq(avs, ",") {
- blockIDs := tx.deletedAttrViewBlockIDs[avID]
+ blockIDs := deletedAttrViewBlockIDs[avID]
if nil == blockIDs {
blockIDs = map[string]struct{}{}
- tx.deletedAttrViewBlockIDs[avID] = blockIDs
+ deletedAttrViewBlockIDs[avID] = blockIDs
}
blockIDs[n.ID] = struct{}{}
}
@@ -1307,7 +1320,12 @@ func (tx *Transaction) collectDeletedAttributeViewBlocks(node *ast.Node, delChil
}
func (tx *Transaction) flushDeletedAttributeViewBlocks() {
- for avID, deletedBlockIDs := range tx.deletedAttrViewBlockIDs {
+ flushDeletedAttributeViewBlocks(tx.deletedAttrViewBlockIDs)
+ tx.deletedAttrViewBlockIDs = map[string]map[string]struct{}{}
+}
+
+func flushDeletedAttributeViewBlocks(deletedAttrViewBlockIDs map[string]map[string]struct{}) {
+ for avID, deletedBlockIDs := range deletedAttrViewBlockIDs {
attrView, err := av.ParseAttributeView(avID)
if nil != err || !removeAttributeViewBoundBlocks(attrView, deletedBlockIDs) {
continue
@@ -1316,7 +1334,6 @@ func (tx *Transaction) flushDeletedAttributeViewBlocks() {
av.SaveAttributeView(attrView)
ReloadAttrView(avID)
}
- tx.deletedAttrViewBlockIDs = map[string]map[string]struct{}{}
}
func removeAttributeViewBoundBlocks(attrView *av.AttributeView, deletedBlockIDs map[string]struct{}) (changed bool) {
diff --git a/kernel/model/transaction_attribute_view_test.go b/kernel/model/transaction_attribute_view_test.go
index 76fb7b92ac..938be64ab5 100644
--- a/kernel/model/transaction_attribute_view_test.go
+++ b/kernel/model/transaction_attribute_view_test.go
@@ -19,7 +19,9 @@ package model
import (
"testing"
+ "github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
)
func TestRemoveAttributeViewBoundBlocks(t *testing.T) {
@@ -54,3 +56,38 @@ func TestRemoveAttributeViewBoundBlocks(t *testing.T) {
t.Fatal("an unrelated block ID should not change the attribute view")
}
}
+
+func TestCollectDeletedAttributeViewBlocks(t *testing.T) {
+ avID1 := "20260805000000-av1"
+ avID2 := "20260805000000-av2"
+ root := treenode.NewParagraph(ast.NewNodeID())
+ root.SetIALAttr(av.NodeAttrNameAvs, avID1)
+ child := treenode.NewParagraph(ast.NewNodeID())
+ child.SetIALAttr(av.NodeAttrNameAvs, avID1+","+avID2)
+ root.AppendChild(child)
+
+ deletedAttrViewBlockIDs := map[string]map[string]struct{}{}
+ collectDeletedAttributeViewBlocks(root, true, deletedAttrViewBlockIDs)
+ if 2 != len(deletedAttrViewBlockIDs) {
+ t.Fatalf("expected 2 attribute views, got %d", len(deletedAttrViewBlockIDs))
+ }
+ if blockIDs, ok := deletedAttrViewBlockIDs[avID1]; !ok || 2 != len(blockIDs) {
+ t.Fatalf("expected 2 blocks bound to [%s], got %#v", avID1, blockIDs)
+ }
+ if blockIDs, ok := deletedAttrViewBlockIDs[avID2]; !ok || 1 != len(blockIDs) {
+ if _, ok := blockIDs[child.ID]; !ok {
+ t.Fatalf("expected child block bound to [%s], got %#v", avID2, blockIDs)
+ }
+ }
+
+ deletedAttrViewBlockIDs = map[string]map[string]struct{}{}
+ collectDeletedAttributeViewBlocks(root, false, deletedAttrViewBlockIDs)
+ if blockIDs, ok := deletedAttrViewBlockIDs[avID1]; !ok || 1 != len(blockIDs) {
+ if _, ok := blockIDs[root.ID]; !ok {
+ t.Fatalf("expected only root block bound to [%s], got %#v", avID1, blockIDs)
+ }
+ }
+ if _, ok := deletedAttrViewBlockIDs[avID2]; ok {
+ t.Fatal("child block should not be collected when delChildrenWhenDelParent is false")
+ }
+}