siyuan-note/siyuan · error

initialize encrypted notebook document failed: %w

Error message

initialize encrypted notebook document failed: %w

What it means

Returned by CreateEncryptedBox when EnsureBoxDoc fails after the encrypted databases (SQL + blocktree) are already opened and the DEK is cached. EnsureBoxDoc initializes the notebook's root document (.sy tree); failure here means the notebook's content structure couldn't be created. The %w wraps the underlying error from EnsureBoxDoc.

Source

Thrown at kernel/model/crypto.go:2609

	markRuntimeEncryptedBox(id)
	invalidateEncryptedPublishAccessCache()

	// 复用刚派生的 DEK 直接开 db + 缓存,省去再次 Argon2id 解锁
	cachedDEKsLock.Lock()
	if err = sql.OpenEncryptedDB(id, dek); err != nil {
		cachedDEKsLock.Unlock()
		return "", err
	}
	if err = treenode.OpenEncryptedBlockTreeDB(id, dek); err != nil {
		sql.CloseEncryptedDB(id)
		cachedDEKsLock.Unlock()
		return "", err
	}
	cachedDEKs[id] = dek
	cachedDEKsLock.Unlock()

	if _, err = EnsureBoxDoc(id); err != nil {
		return "", fmt.Errorf("initialize encrypted notebook document failed: %w", err)
	}

	// 初始化自动锁定访问时间戳,与 UnlockBox 对称
	newVal := &atomic.Int64{}
	newVal.Store(time.Now().UnixNano())
	boxLastAccess.Store(id, newVal)

	setEncryptedBoxState(id, EncryptedBoxStateUnlocked)
	IncSync()
	return id, nil
}

// cleanupFailedEncryptedBox 清理创建失败的加密笔记本,清理目标必须是 DataDir 下的有效笔记本目录。
func cleanupFailedEncryptedBox(boxID string) {
	if !ast.IsNodeIDPattern(boxID) {
		logging.LogErrorf("refuse to cleanup failed encrypted box with invalid ID [%s]", boxID)
		return
	}

View on GitHub (pinned to 251596fc0d)

Solutions

  1. Check the wrapped error from EnsureBoxDoc for the specific failure (disk I/O vs. treenode vs. block ID).
  2. Verify write permissions and disk space on the notebook data directory.
  3. The deferred cleanup will remove the half-created box; retry CreateEncryptedBox after fixing the underlying issue.
Defensive patterns

Strategy: try-catch

Try / catch

id, err := model.CreateEncryptedBox(name, password)
if err != nil {
    if strings.Contains(err.Error(), "initialize encrypted notebook document") {
        // EnsureBoxDoc failed — check disk/treenode state
        logging.LogErrorf("EnsureBoxDoc failed: %+v", err)
    }
    return err
}

Prevention

When it happens

Trigger: EnsureBoxDoc fails due to a filesys write error (can't create the root .sy file), a treenode indexing error, or a block-ID collision. The encrypted DBs are open but the notebook has no valid root document.

Common situations: Disk I/O failure when writing the initial document tree. Permission issue on the notebook data subdirectory. A rare ID collision or corrupted block-tree state on a freshly created notebook.

Related errors


AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12). Data as JSON: /api/errors/d7bb2a2f41b7e019. Report an issue: GitHub.