siyuan-note/siyuan · error

marshal notebook crypt backup failed

Error message

marshal notebook crypt backup failed: %w

What it means

After creating the backup directory, writeNotebookCryptBackup serializes the conf.BoxEncryption struct with gulu.JSON.MarshalIndentJSON. This wrapped error means that JSON marshaling failed, which is nearly impossible for a valid in-memory struct and usually signals an internal invariant problem (e.g. an unmarshalable value injected into the struct).

Solutions

  1. Inspect the wrapped error to identify which field fails to marshal
  2. Verify conf.BoxEncryption only contains plain serializable fields (strings/bytes-as-base64); fix or upgrade the code that populated it
  3. Re-run CreateEncryptedBox / ChangeMasterPassword with a freshly constructed BoxEncryption
  4. Report as a kernel bug if a stock configuration reproduces it
Defensive patterns

Strategy: try-catch

Try / catch

if err := model.CreateEncryptedBox(box, password); err != nil {
    if strings.Contains(err.Error(), "marshal notebook crypt backup") {
        // internal invariant broken: report bug, do not retry blindly
        return reportKernelBug(err)
    }
    return err
}

Prevention

When it happens

Trigger: gulu.JSON.MarshalIndentJSON(crypt, "", " ") returns an error while writing the backup in CreateEncryptedBox or ChangeMasterPassword — e.g. crypt contains a field that cannot be serialized (channel/func value) due to a code defect or corrupted conf object.

Common situations: A plugin or patched build placing unexpected values in BoxEncryption; version drift where conf.BoxEncryption gained a non-serializable field; memory corruption is effectively ruled out.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/1d0169af39d1744d. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:2533

	return filepath.Join(util.DataDir, boxID, ".siyuan", notebookCryptoBackupFilename)
}

// writeNotebookCryptBackup 写入加密笔记本的 BoxCrypt 备份。
// 仅在 Encrypted=true 的笔记本上调用,配合 CreateEncryptedBox / ChangeMasterPassword 写入。
func writeNotebookCryptBackup(boxID string, crypt *conf.BoxEncryption) error {
	if !ast.IsNodeIDPattern(boxID) {
		return errors.New("invalid notebook ID")
	}
	if err := validateBoxEncryption(crypt); err != nil {
		return err
	}
	backupPath := notebookCryptoBackupPath(boxID)
	if err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {
		return fmt.Errorf("mkdir notebook crypt backup dir failed: %w", err)
	}
	data, err := gulu.JSON.MarshalIndentJSON(crypt, "", "  ")
	if err != nil {
		return fmt.Errorf("marshal notebook crypt backup failed: %w", err)
	}
	if err := filelock.WriteFile(backupPath, data); err != nil {
		return fmt.Errorf("write notebook crypt backup failed: %w", err)
	}
	return nil
}

// readNotebookCryptBackup 读取加密笔记本的 BoxCrypt 备份。
// 备份文件不存在时返回 (nil, nil),调用方据此区分"非加密笔记本"和"备份不存在"。
func readNotebookCryptBackup(boxID string) (*conf.BoxEncryption, error) {
	if !ast.IsNodeIDPattern(boxID) {
		return nil, errors.New("invalid notebook ID")
	}
	backupPath := notebookCryptoBackupPath(boxID)
	if !filelock.IsExist(backupPath) {
		return nil, nil
	}
	return readBoxEncryptionFile(backupPath)

View on GitHub (pinned to 8641553a1f)