Skip to content

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

:bug: 索引嵌入块内容可能会导致内核崩溃 https://github.com/siyuan-note/siyuan/issues/7213

Liang Ding204580bf36db
diff --git a/kernel/model/index.go b/kernel/model/index.go
index e9d1114329..8d2c349575 100644
--- a/kernel/model/index.go
+++ b/kernel/model/index.go
@@ -204,7 +204,7 @@ func autoIndexEmbedBlock(embedBlocks []*sql.Block) {
if "" == embedBlock.Content {
embedBlock.Content = "no query result"
}
- sql.UpdateBlockContent(embedBlock)
+ sql.UpdateBlockContentQueue(embedBlock)
if 63 <= i { // 一次任务中最多处理 64 个嵌入块,防止卡顿
break
@@ -225,7 +225,7 @@ func updateEmbedBlockContent(embedBlockID string, queryResultBlocks []*EmbedBloc
if "" == embedBlock.Content {
embedBlock.Content = "no query result"
}
- sql.UpdateBlockContent(embedBlock)
+ sql.UpdateBlockContentQueue(embedBlock)
}
func init() {
diff --git a/kernel/sql/block.go b/kernel/sql/block.go
index e12b1debde..0bfa4e02e5 100644
--- a/kernel/sql/block.go
+++ b/kernel/sql/block.go
@@ -66,12 +66,7 @@ func updateRootContent(tx *sql.Tx, content, updated, id string) (err error) {
return
}
-func UpdateBlockContent(block *Block) {
- tx, err := beginTx()
- if nil != err {
- return
- }
-
+func updateBlockContent(tx *sql.Tx, block *Block) (err error) {
stmt := "UPDATE blocks SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
tx.Rollback()
@@ -89,6 +84,7 @@ func UpdateBlockContent(block *Block) {
return
}
}
- tx.Commit()
+
putBlockCache(block)
+ return
}
diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go
index 0b450083c3..d7eda5bfb7 100644
--- a/kernel/sql/block_query.go
+++ b/kernel/sql/block_query.go
@@ -474,6 +474,8 @@ func selectBlocksRawStmt(stmt string, limit int) (ret []*Block) {
if confLimit && limit < len(ret) {
break
}
+ } else {
+ logging.LogWarnf("raw sql query [%s] failed: %s", stmt, err)
}
}
return
diff --git a/kernel/sql/queue.go b/kernel/sql/queue.go
index 7952bbc248..6a9b22c1d6 100644
--- a/kernel/sql/queue.go
+++ b/kernel/sql/queue.go
@@ -41,7 +41,7 @@ var (
type dbQueueOperation struct {
inQueueTime time.Time
- action string // upsert/delete/delete_id/rename/delete_box/delete_box_refs/insert_refs/index/delete_ids
+ action string // upsert/delete/delete_id/rename/delete_box/delete_box_refs/insert_refs/index/delete_ids/update_block_content
indexPath string // index
upsertTree *parse.Tree // upsert/insert_refs
@@ -51,6 +51,7 @@ type dbQueueOperation struct {
box string // delete_box/delete_box_refs/index
renameTree *parse.Tree // rename
renameTreeOldHPath string // rename
+ block *Block // update_block_content
}
func FlushTxJob() {
@@ -166,6 +167,8 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (e
err = insertRefs(tx, op.upsertTree)
case "update_refs":
err = upsertRefs(tx, op.upsertTree)
+ case "update_block_content":
+ err = updateBlockContent(tx, op.block)
default:
msg := fmt.Sprintf("unknown operation [%s]", op.action)
logging.LogErrorf(msg)
@@ -174,13 +177,18 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (e
return
}
-func mergeUpsertTrees() (ops []*dbQueueOperation) {
+func UpdateBlockContentQueue(block *Block) {
dbQueueLock.Lock()
defer dbQueueLock.Unlock()
- ops = operationQueue
- operationQueue = nil
- return
+ newOp := &dbQueueOperation{block: block, inQueueTime: time.Now(), action: "update_block_content"}
+ for i, op := range operationQueue {
+ if "update_block_content" == op.action && op.block.ID == block.ID {
+ operationQueue[i] = newOp
+ return
+ }
+ }
+ operationQueue = append(operationQueue, newOp)
}
func UpdateRefsTreeQueue(tree *parse.Tree) {
@@ -320,3 +328,12 @@ func RemoveTreePathQueue(treeBox, treePathPrefix string) {
}
operationQueue = append(operationQueue, newOp)
}
+
+func mergeUpsertTrees() (ops []*dbQueueOperation) {
+ dbQueueLock.Lock()
+ defer dbQueueLock.Unlock()
+
+ ops = operationQueue
+ operationQueue = nil
+ return
+}
\ No newline at end of file