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

This commit is contained in:
Liang Ding 2023-01-31 17:28:47 +08:00
parent dbf6965c30
commit 204580bf36
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
4 changed files with 29 additions and 14 deletions

View File

@ -204,7 +204,7 @@ func autoIndexEmbedBlock(embedBlocks []*sql.Block) {
if "" == embedBlock.Content { if "" == embedBlock.Content {
embedBlock.Content = "no query result" embedBlock.Content = "no query result"
} }
sql.UpdateBlockContent(embedBlock) sql.UpdateBlockContentQueue(embedBlock)
if 63 <= i { // 一次任务中最多处理 64 个嵌入块,防止卡顿 if 63 <= i { // 一次任务中最多处理 64 个嵌入块,防止卡顿
break break
@ -225,7 +225,7 @@ func updateEmbedBlockContent(embedBlockID string, queryResultBlocks []*EmbedBloc
if "" == embedBlock.Content { if "" == embedBlock.Content {
embedBlock.Content = "no query result" embedBlock.Content = "no query result"
} }
sql.UpdateBlockContent(embedBlock) sql.UpdateBlockContentQueue(embedBlock)
} }
func init() { func init() {

View File

@ -66,12 +66,7 @@ func updateRootContent(tx *sql.Tx, content, updated, id string) (err error) {
return return
} }
func UpdateBlockContent(block *Block) { func updateBlockContent(tx *sql.Tx, block *Block) (err error) {
tx, err := beginTx()
if nil != err {
return
}
stmt := "UPDATE blocks SET content = ? WHERE id = ?" stmt := "UPDATE blocks SET content = ? WHERE id = ?"
if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err { if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
tx.Rollback() tx.Rollback()
@ -89,6 +84,7 @@ func UpdateBlockContent(block *Block) {
return return
} }
} }
tx.Commit()
putBlockCache(block) putBlockCache(block)
return
} }

View File

@ -474,6 +474,8 @@ func selectBlocksRawStmt(stmt string, limit int) (ret []*Block) {
if confLimit && limit < len(ret) { if confLimit && limit < len(ret) {
break break
} }
} else {
logging.LogWarnf("raw sql query [%s] failed: %s", stmt, err)
} }
} }
return return

View File

@ -41,7 +41,7 @@ var (
type dbQueueOperation struct { type dbQueueOperation struct {
inQueueTime time.Time 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 indexPath string // index
upsertTree *parse.Tree // upsert/insert_refs upsertTree *parse.Tree // upsert/insert_refs
@ -51,6 +51,7 @@ type dbQueueOperation struct {
box string // delete_box/delete_box_refs/index box string // delete_box/delete_box_refs/index
renameTree *parse.Tree // rename renameTree *parse.Tree // rename
renameTreeOldHPath string // rename renameTreeOldHPath string // rename
block *Block // update_block_content
} }
func FlushTxJob() { func FlushTxJob() {
@ -166,6 +167,8 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (e
err = insertRefs(tx, op.upsertTree) err = insertRefs(tx, op.upsertTree)
case "update_refs": case "update_refs":
err = upsertRefs(tx, op.upsertTree) err = upsertRefs(tx, op.upsertTree)
case "update_block_content":
err = updateBlockContent(tx, op.block)
default: default:
msg := fmt.Sprintf("unknown operation [%s]", op.action) msg := fmt.Sprintf("unknown operation [%s]", op.action)
logging.LogErrorf(msg) logging.LogErrorf(msg)
@ -174,13 +177,18 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (e
return return
} }
func mergeUpsertTrees() (ops []*dbQueueOperation) { func UpdateBlockContentQueue(block *Block) {
dbQueueLock.Lock() dbQueueLock.Lock()
defer dbQueueLock.Unlock() defer dbQueueLock.Unlock()
ops = operationQueue newOp := &dbQueueOperation{block: block, inQueueTime: time.Now(), action: "update_block_content"}
operationQueue = nil for i, op := range operationQueue {
if "update_block_content" == op.action && op.block.ID == block.ID {
operationQueue[i] = newOp
return return
}
}
operationQueue = append(operationQueue, newOp)
} }
func UpdateRefsTreeQueue(tree *parse.Tree) { func UpdateRefsTreeQueue(tree *parse.Tree) {
@ -320,3 +328,12 @@ func RemoveTreePathQueue(treeBox, treePathPrefix string) {
} }
operationQueue = append(operationQueue, newOp) operationQueue = append(operationQueue, newOp)
} }
func mergeUpsertTrees() (ops []*dbQueueOperation) {
dbQueueLock.Lock()
defer dbQueueLock.Unlock()
ops = operationQueue
operationQueue = nil
return
}