siyuan-note/siyuan · error

unmarshal master password migration failed

Error message

unmarshal master password migration failed: %w

What it means

readMasterPasswordMigrationUnlocked loads the master password migration record from disk and JSON-decodes it. This error is thrown when the file exists and was read successfully, but its bytes are not valid JSON or do not match the masterPasswordMigration struct shape, so the migration record cannot be interpreted. The underlying decode error is wrapped for context.

Solutions

  1. Delete the corrupt master password migration file so the migration state resets, then re-run the migration (ensure the master key/KEK material is still recoverable first)
  2. Inspect the file contents and fix the JSON syntax if it was hand-edited
  3. Restore the file from a workspace backup or sync snapshot
  4. Check the wrapped %w cause for the exact decoder message (e.g. unexpected end of JSON input) to distinguish truncation from a schema mismatch

Example fix

// before: silently propagating a corrupt record
m, err := readMasterPasswordMigrationUnlocked(p)
if err != nil { return err }
// after: detect corruption and fall back to a backup copy of the migration file
m, err := readMasterPasswordMigrationUnlocked(p)
if err != nil {
    if backupErr := restoreMigrationFromBackup(p); backupErr != nil {
        return fmt.Errorf("migration record unreadable and no backup: %w", err)
    }
    m, err = readMasterPasswordMigrationUnlocked(p)
}
Defensive patterns

Strategy: try-catch

Validate before calling

data, err := os.ReadFile(migrationPath)
if err == nil && len(data) > 0 {
    var probe map[string]any
    if jsonErr := json.Unmarshal(data, &probe); jsonErr != nil {
        // corrupt before calling the API — restore from backup
    }
}

Type guard

func isValidMigrationJSON(data []byte) bool {
    var m map[string]any
    return len(data) > 0 && json.Unmarshal(data, &m) == nil
}

Try / catch

m, err := readMasterPasswordMigration()
if err != nil {
    var corrupt *json.SyntaxError
    if errors.As(err, &corrupt) {
        // restore migration file from backup, then retry
    }
    return err
}

Prevention

When it happens

Trigger: Calling readMasterPasswordMigration / removeMasterPasswordMigrationBox when the migration file at its fixed path is corrupted, truncated (e.g. crash mid-write), empty, or was hand-edited into invalid JSON.

Common situations: A power failure or kernel crash interrupted a write of the migration file; a user or backup tool partially restored the workspace; the file was edited manually and lost valid JSON syntax; an older/newer format version wrote incompatible content.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/4419d2d204677748. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:566

func readMasterPasswordMigration() (*masterPasswordMigration, error) {
	masterPasswordMigrationMu.Lock()
	defer masterPasswordMigrationMu.Unlock()
	return readMasterPasswordMigrationUnlocked()
}

func readMasterPasswordMigrationUnlocked() (*masterPasswordMigration, error) {
	p := masterPasswordMigrationPath()
	if !filelock.IsExist(p) {
		return nil, nil
	}
	data, err := filelock.ReadFile(p)
	if err != nil {
		return nil, fmt.Errorf("read master password migration failed: %w", err)
	}
	var m masterPasswordMigration
	if err = gulu.JSON.UnmarshalJSON(data, &m); err != nil {
		return nil, fmt.Errorf("unmarshal master password migration failed: %w", err)
	}
	return &m, nil
}

func removeMasterPasswordMigration() {
	masterPasswordMigrationMu.Lock()
	defer masterPasswordMigrationMu.Unlock()
	removeMasterPasswordMigrationUnlocked()
}

func removeMasterPasswordMigrationUnlocked() {
	p := masterPasswordMigrationPath()
	if err := filelock.Remove(p); err != nil && !os.IsNotExist(err) {
		logging.LogErrorf("remove master password migration failed: %s", err)
	}
}

func removeMasterPasswordMigrationBox(boxID string) {

View on GitHub (pinned to 9f775e8a12)