Skip to content

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

:bug: fix encrypted-notebook DocIAL deadlock + .sy data loss #18034

Daniel0f10020aff98
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go
index 2fe65e6356..5ea2ab4a68 100644
--- a/kernel/filesys/tree.go
+++ b/kernel/filesys/tree.go
@@ -251,9 +251,8 @@ func DocIAL(absPath string) (ret map[string]string) {
if boxID != "" && DEKProvider != nil {
if dek, err := DEKProvider(boxID); err == nil && dek != nil {
// 已解锁的加密 box:整体读密文 → 解密 → 流式解析
- filelock.Lock(absPath)
+ // 注意:filelock.ReadFile 内部已加锁,不能在外面再 Lock/Unlock(会死锁)
raw, readErr := filelock.ReadFile(absPath)
- filelock.Unlock(absPath)
if readErr != nil {
logging.LogErrorf("read file [%s] failed: %s", absPath, readErr)
return nil
diff --git a/kernel/model/file.go b/kernel/model/file.go
index f3465ff8bf..f89ee00135 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -119,6 +119,12 @@ func (box *Box) docIAL(p string) (ret map[string]string) {
filePath := filepath.Join(util.DataDir, box.ID, p)
ret = filesys.DocIAL(filePath)
if 1 > len(ret) {
+ // 加密笔记本的 .sy 解密失败(DEK 未缓存或 box 未解锁)时不应视为损坏,
+ // 否则文件会被 moveCorruptedData 移走导致数据丢失
+ if IsEncryptedBox(box.ID) {
+ logging.LogWarnf("properties not found in encrypted file [%s], skip moveCorruptedData", filePath)
+ return nil
+ }
logging.LogWarnf("properties not found in file [%s]", filePath)
box.moveCorruptedData(filePath)
return nil
diff --git a/kernel/model/index_fix.go b/kernel/model/index_fix.go
index fa17b8d9c8..897352b845 100644
--- a/kernel/model/index_fix.go
+++ b/kernel/model/index_fix.go
@@ -207,6 +207,10 @@ func resetDuplicateBlocksOnFileSys() {
blockIDs := map[string]bool{}
needRefreshUI := false
for _, box := range boxes {
+ // 关闭的加密笔记本无法解密 .sy,跳过(避免密文被当损坏移走)
+ if IsEncryptedBox(box.ID) && !IsBoxUnlocked(box.ID) {
+ continue
+ }
// 校验索引阶段自动删除历史遗留的笔记本 history 文件夹
legacyHistory := filepath.Join(util.DataDir, box.ID, ".siyuan", "history")
if gulu.File.IsDir(legacyHistory) {
diff --git a/kernel/model/mount.go b/kernel/model/mount.go
index a8d10ed3d2..f0d902d1d6 100644
--- a/kernel/model/mount.go
+++ b/kernel/model/mount.go
@@ -222,7 +222,12 @@ func unmount0(boxID string) {
box.SaveConf(boxConf)
box.Unindex()
if boxConf.Encrypted {
- // 加密笔记本关闭时清除其 DEK 缓存,恢复"已锁定"状态
+ // 加密笔记本关闭时清除其 DEK 缓存,恢复"已锁定"状态。
+ // 先等待事务队列和 SQL 索引队列清空,确保异步索引清理任务(unindex → 删
+ // blocktree/content db 数据)已完成,否则 ClearDEK 关闭加密 db 后未完成的
+ // 清理任务会操作已关闭的 db 连接,导致下次 Mount 的 FlushTxQueue 无限等待。
+ FlushTxQueue()
+ sql.FlushQueue()
ClearDEK(boxID)
}
}