{"record":{"id":"4419d2d204677748","repo":"siyuan-note/siyuan","slug":"unmarshal-master-password-migration-failed-w","errorCode":null,"errorMessage":"unmarshal master password migration failed: %w","messagePattern":"unmarshal master password migration failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":566,"sourceCode":"\nfunc readMasterPasswordMigration() (*masterPasswordMigration, error) {\n\tmasterPasswordMigrationMu.Lock()\n\tdefer masterPasswordMigrationMu.Unlock()\n\treturn readMasterPasswordMigrationUnlocked()\n}\n\nfunc readMasterPasswordMigrationUnlocked() (*masterPasswordMigration, error) {\n\tp := masterPasswordMigrationPath()\n\tif !filelock.IsExist(p) {\n\t\treturn nil, nil\n\t}\n\tdata, err := filelock.ReadFile(p)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read master password migration failed: %w\", err)\n\t}\n\tvar m masterPasswordMigration\n\tif err = gulu.JSON.UnmarshalJSON(data, &m); err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshal master password migration failed: %w\", err)\n\t}\n\treturn &m, nil\n}\n\nfunc removeMasterPasswordMigration() {\n\tmasterPasswordMigrationMu.Lock()\n\tdefer masterPasswordMigrationMu.Unlock()\n\tremoveMasterPasswordMigrationUnlocked()\n}\n\nfunc removeMasterPasswordMigrationUnlocked() {\n\tp := masterPasswordMigrationPath()\n\tif err := filelock.Remove(p); err != nil && !os.IsNotExist(err) {\n\t\tlogging.LogErrorf(\"remove master password migration failed: %s\", err)\n\t}\n}\n\nfunc removeMasterPasswordMigrationBox(boxID string) {","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L548-L584","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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)","Inspect the file contents and fix the JSON syntax if it was hand-edited","Restore the file from a workspace backup or sync snapshot","Check the wrapped %w cause for the exact decoder message (e.g. unexpected end of JSON input) to distinguish truncation from a schema mismatch"],"exampleFix":"// before: silently propagating a corrupt record\nm, err := readMasterPasswordMigrationUnlocked(p)\nif err != nil { return err }\n// after: detect corruption and fall back to a backup copy of the migration file\nm, err := readMasterPasswordMigrationUnlocked(p)\nif err != nil {\n    if backupErr := restoreMigrationFromBackup(p); backupErr != nil {\n        return fmt.Errorf(\"migration record unreadable and no backup: %w\", err)\n    }\n    m, err = readMasterPasswordMigrationUnlocked(p)\n}","handlingStrategy":"try-catch","validationCode":"data, err := os.ReadFile(migrationPath)\nif err == nil && len(data) > 0 {\n    var probe map[string]any\n    if jsonErr := json.Unmarshal(data, &probe); jsonErr != nil {\n        // corrupt before calling the API — restore from backup\n    }\n}","typeGuard":"func isValidMigrationJSON(data []byte) bool {\n    var m map[string]any\n    return len(data) > 0 && json.Unmarshal(data, &m) == nil\n}","tryCatchPattern":"m, err := readMasterPasswordMigration()\nif err != nil {\n    var corrupt *json.SyntaxError\n    if errors.As(err, &corrupt) {\n        // restore migration file from backup, then retry\n    }\n    return err\n}","preventionTips":["Never hand-edit the master password migration file","Ensure the kernel process is not killed mid-write (use UPS/healthy shutdown)","Keep a pre-migration backup of the workspace before running migrations","Treat unmarshal failures as corruption: restore, don't regenerate keys"],"tags":["json","unmarshal","migration","corruption"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}