Skip to content

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

:bug: Duplicate search results in encrypted notebook after close/reopen https://github.com/siyuan-note/siyuan/issues/18034

Daniel409f99b63636
diff --git a/kernel/model/crypto.go b/kernel/model/crypto.go
index 744d394241..49efd77891 100644
--- a/kernel/model/crypto.go
+++ b/kernel/model/crypto.go
@@ -171,8 +171,10 @@ func IsBoxUnlocked(boxID string) bool {
return ok
}
-// LockBox 清除指定笔记本的 DEK 并关闭其加密 db。Unmount 单个加密笔记本或手动锁定时调用。
+// LockBox 清除指定笔记本的 DEK 并删除其加密 db 文件。Unmount 单个加密笔记本或手动锁定时调用。
// 同时清空所有明文缓存(树/Block/IAL/AV),避免明文在内存中残留。
+// 关闭即删 db 文件:加密索引可由 box.Index() 全量重建,文件无需持久化;
+// 同时避免关闭后残留旧索引数据导致下次解锁叠加重复行。
func LockBox(boxID string) {
cachedDEKsLock.Lock()
if dek, ok := cachedDEKs[boxID]; ok {
@@ -180,8 +182,8 @@ func LockBox(boxID string) {
delete(cachedDEKs, boxID)
}
cachedDEKsLock.Unlock()
- sql.CloseEncryptedDB(boxID)
- treenode.CloseEncryptedBlockTreeDB(boxID)
+ sql.RemoveEncryptedDBFile(boxID)
+ treenode.RemoveEncryptedBlockTreeDBFile(boxID)
// 清空明文缓存:锁定后任何加密 box 的明文都不应残留内存
cache.ClearTreeCache()
sql.ClearCache()
@@ -190,7 +192,8 @@ func LockBox(boxID string) {
cache.ClearAVCache()
}
-// LockAllBoxes 清除所有已缓存的 DEK 并关闭所有加密 db。退出登录或全局锁定时调用。
+// LockAllBoxes 清除所有已缓存的 DEK 并删除所有加密 db 文件。退出登录或全局锁定时调用。
+// 与 LockBox 一致采用"关闭即删 db 文件"策略,避免残留旧索引数据。
func LockAllBoxes() {
cachedDEKsLock.Lock()
for id, dek := range cachedDEKs {
@@ -198,9 +201,9 @@ func LockAllBoxes() {
delete(cachedDEKs, id)
}
cachedDEKsLock.Unlock()
- // 关闭所有已打开的加密 db 连接,清空明文缓存避免残留
- sql.CloseAllEncryptedDBs()
- treenode.CloseAllEncryptedBlockTreeDBs()
+ // 删除所有已打开的加密 db 文件(关闭连接 + 删文件),清空明文缓存避免残留
+ sql.RemoveAllEncryptedDBFiles()
+ treenode.RemoveAllEncryptedBlockTreeDBFiles()
cache.ClearTreeCache()
sql.ClearCache()
cache.ClearDocsIAL()
diff --git a/kernel/model/mount.go b/kernel/model/mount.go
index c33a6aca37..27263a053e 100644
--- a/kernel/model/mount.go
+++ b/kernel/model/mount.go
@@ -235,10 +235,9 @@ func unmount0(boxID string) {
box.SaveConf(boxConf)
box.Unindex()
if boxConf.Encrypted {
- // 加密笔记本关闭时清除其 DEK 缓存,恢复"已锁定"状态。
- // 先等待事务队列和 SQL 索引队列清空,确保异步索引清理任务(unindex → 删
- // blocktree/content db 数据)已完成,否则 ClearDEK 关闭加密 db 后未完成的
- // 清理任务会操作已关闭的 db 连接,导致下次 Mount 的 FlushTxQueue 无限等待。
+ // 加密笔记本关闭:先等待事务队列和 SQL 索引队列落盘(确保 pending 写入已持久化到加密 .sy),
+ // 再 ClearDEK(=LockBox)清除 DEK 并删除加密 db 文件。加密索引可由 box.Index() 全量重建,
+ // 关闭即删文件避免残留旧索引数据导致下次解锁叠加重复行。
FlushTxQueue()
sql.FlushQueue()
ClearDEK(boxID)
diff --git a/kernel/sql/database.go b/kernel/sql/database.go
index 60e4f8dba5..000bf07336 100644
--- a/kernel/sql/database.go
+++ b/kernel/sql/database.go
@@ -1373,6 +1373,10 @@ func batchUpdateHPath(tx *sql.Tx, tree *parse.Tree, context map[string]any) (err
func CloseDatabase() {
closeIndexQueue()
+ // 退出时删除所有已打开的加密 db 文件:加密索引可由 box.Index() 全量重建,
+ // 文件无需持久化,删除可避免重启后残留旧索引数据导致下次解锁叠加重复行。
+ RemoveAllEncryptedDBFiles()
+ treenode.RemoveAllEncryptedBlockTreeDBFiles()
if err := db.Close(); err != nil {
logging.LogErrorf("close database failed: %s", err)
}
@@ -1842,7 +1846,8 @@ func OpenEncryptedDB(boxID string, dek []byte) (err error) {
return nil
}
-// CloseEncryptedDB 关闭并移除加密笔记本的 db 连接。LockBox/Unmount 时调用。
+// CloseEncryptedDB 仅关闭加密笔记本的 db 连接(不删文件)。UnlockBox 创建失败回滚时调用。
+// 关闭/锁定加密笔记本请用 RemoveEncryptedDBFile(关连接 + 删文件)。
func CloseEncryptedDB(boxID string) {
if v, ok := encryptedDBs.LoadAndDelete(boxID); ok {
if boxDB, ok := v.(*sql.DB); ok {
@@ -1851,17 +1856,6 @@ func CloseEncryptedDB(boxID string) {
}
}
-// CloseAllEncryptedDBs 关闭所有已打开的加密 db 连接。全局锁定(LockAllBoxes)时调用。
-func CloseAllEncryptedDBs() {
- encryptedDBs.Range(func(key, value any) bool {
- encryptedDBs.Delete(key)
- if boxDB, ok := value.(*sql.DB); ok {
- boxDB.Close()
- }
- return true
- })
-}
-
// GetEncryptedDB 返回加密 box 的 db 句柄;未打开返回 nil。
func GetEncryptedDB(boxID string) *sql.DB {
if v, ok := encryptedDBs.Load(boxID); ok {
@@ -1883,7 +1877,7 @@ func GetEncryptedBoxIDs() (ret []string) {
return
}
-// RemoveEncryptedDBFile 关闭连接并删除加密 db 文件(含 WAL/SHM)。删除笔记本时调用。
+// RemoveEncryptedDBFile 关闭连接并删除加密 db 文件(含 WAL/SHM)。删除笔记本、关闭加密笔记本时调用。
func RemoveEncryptedDBFile(boxID string) {
CloseEncryptedDB(boxID)
dbPath := util.EncryptedDBPath(boxID)
@@ -1894,6 +1888,14 @@ func RemoveEncryptedDBFile(boxID string) {
}
}
+// RemoveAllEncryptedDBFiles 关闭所有已打开的加密 content db 连接并删除其文件(含 WAL/SHM)。
+// 进程退出(CloseDatabase)时调用,避免重启后残留旧索引数据导致下次解锁叠加重复行。
+func RemoveAllEncryptedDBFiles() {
+ for _, boxID := range GetEncryptedBoxIDs() {
+ RemoveEncryptedDBFile(boxID)
+ }
+}
+
// beginTxForBox 按 box 选 db 开事务。加密 box 用其独立 db,否则用全局 db。
func beginTxForBox(box string) (tx *sql.Tx, err error) {
if boxDB := GetEncryptedDB(box); boxDB != nil {
@@ -1916,7 +1918,6 @@ func initEncryptedDBTables(boxDB *sql.DB) (err error) {
"CREATE TABLE IF NOT EXISTS attributes (id, name, value, type, block_id, root_id, box, path)",
"CREATE TABLE IF NOT EXISTS refs (id, def_block_id, def_block_parent_id, def_block_root_id, def_block_path, block_id, root_id, box, path, content, markdown, type)",
"CREATE TABLE IF NOT EXISTS file_annotation_refs (id, file_path, annotation_id, block_id, root_id, box, path, content, type)",
- "CREATE TABLE IF NOT EXISTS block_embeddings (id TEXT PRIMARY KEY, root_id TEXT, box TEXT, path TEXT, embedding BLOB, model TEXT, content_len INTEGER, updated TEXT, fail_count INTEGER NOT NULL DEFAULT 0, last_tried INTEGER NOT NULL DEFAULT 0, ignored_type INTEGER NOT NULL DEFAULT 0)",
}
for _, stmt := range tables {
if _, err = boxDB.Exec(stmt); err != nil {
diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go
index 7208340d36..5fdc304049 100644
--- a/kernel/treenode/blocktree.go
+++ b/kernel/treenode/blocktree.go
@@ -894,7 +894,8 @@ func OpenEncryptedBlockTreeDB(boxID string, dek []byte) (err error) {
return nil
}
-// CloseEncryptedBlockTreeDB 关闭并移除加密 blocktree db 连接。LockBox/Unmount 时调用。
+// CloseEncryptedBlockTreeDB 仅关闭加密 blocktree db 连接(不删文件)。
+// 被 RemoveEncryptedBlockTreeDBFile 复用作为底层关连接实现。
func CloseEncryptedBlockTreeDB(boxID string) {
if v, ok := encryptedBlockTreeDBs.LoadAndDelete(boxID); ok {
if boxDB, ok := v.(*sql.DB); ok {
@@ -903,17 +904,6 @@ func CloseEncryptedBlockTreeDB(boxID string) {
}
}
-// CloseAllEncryptedBlockTreeDBs 关闭所有加密 blocktree db。LockAllBoxes 时调用。
-func CloseAllEncryptedBlockTreeDBs() {
- encryptedBlockTreeDBs.Range(func(key, value any) bool {
- encryptedBlockTreeDBs.Delete(key)
- if boxDB, ok := value.(*sql.DB); ok {
- boxDB.Close()
- }
- return true
- })
-}
-
// GetOpenedEncryptedBoxIDs 返回所有已打开的加密 blocktree db 对应的 boxID。
// 供 boxID 未知时遍历查找(如通用打开入口 openFileById)。
func GetOpenedEncryptedBoxIDs() (ret []string) {
@@ -926,7 +916,7 @@ func GetOpenedEncryptedBoxIDs() (ret []string) {
return
}
-// RemoveEncryptedBlockTreeDBFile 关闭连接并删除加密 blocktree db 文件。删笔记本时调用。
+// RemoveEncryptedBlockTreeDBFile 关闭连接并删除加密 blocktree db 文件。删笔记本、关闭加密笔记本时调用。
func RemoveEncryptedBlockTreeDBFile(boxID string) {
CloseEncryptedBlockTreeDB(boxID)
dbPath := util.EncryptedBlockTreeDBPath(boxID)
@@ -937,6 +927,14 @@ func RemoveEncryptedBlockTreeDBFile(boxID string) {
}
}
+// RemoveAllEncryptedBlockTreeDBFiles 关闭所有已打开的加密 blocktree db 连接并删除其文件(含 WAL/SHM)。
+// 进程退出(CloseDatabase)时调用,避免重启后残留旧索引数据。
+func RemoveAllEncryptedBlockTreeDBFiles() {
+ for _, boxID := range GetOpenedEncryptedBoxIDs() {
+ RemoveEncryptedBlockTreeDBFile(boxID)
+ }
+}
+
// getEncryptedBlockTreeDB 返回加密 box 的 blocktree db 句柄;未打开返回 nil。
func getEncryptedBlockTreeDB(boxID string) *sql.DB {
if v, ok := encryptedBlockTreeDBs.Load(boxID); ok {