{"record":{"id":"58df254ec0ed303d","repo":"siyuan-note/siyuan","slug":"view-state-patch-contains-too-many-entries","errorCode":null,"errorMessage":"view state patch contains too many entries","messagePattern":"view state patch contains too many entries","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/view_state.go","lineNumber":87,"sourceCode":"\tdefer viewStateStorageLock.Unlock()\n\n\tstorage, err := getViewStateStorage()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tstate := storage.Views[key]\n\tif nil == state || nil == state.Data {\n\t\treturn map[string]any{}, nil\n\t}\n\treturn cloneViewStateData(state.Data)\n}\n\nfunc PatchViewState(key string, values map[string]any, removeKeys []string) (ret map[string]any, err error) {\n\tif err = validateViewStateKey(key); err != nil {\n\t\treturn\n\t}\n\tif maxViewStatePatchCount < len(values)+len(removeKeys) {\n\t\treturn nil, errors.New(\"view state patch contains too many entries\")\n\t}\n\tfor valueKey := range values {\n\t\tif err = validateViewStateDataKey(valueKey); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\tfor _, removeKey := range removeKeys {\n\t\tif err = validateViewStateDataKey(removeKey); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\tpatch := &viewStatePatch{Values: values, RemoveKeys: removeKeys}\n\tpatchData, marshalErr := gulu.JSON.MarshalJSON(patch)\n\tif nil != marshalErr {\n\t\treturn nil, marshalErr\n\t}\n\tif maxViewStatePatchBytes < len(patchData) {\n\t\treturn nil, errors.New(\"view state patch is too large\")","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/view_state.go#L69-L105","documentation":"PatchViewState enforces a limit (maxViewStatePatchCount = 1000) on the total number of entries in a single patch — len(values) + len(removeKeys). Patches exceeding this count are rejected with this error to bound per-request work and storage churn on view-state.json.","triggerScenarios":"Calling PatchViewState (or the view state HTTP API) with a values map plus removeKeys list whose combined size exceeds 1000 entries, e.g. bulk-clearing thousands of view fields in one call.","commonSituations":"A frontend flushing a large in-memory state object in a single patch; a bulk migration writing thousands of keys at once; a loop accumulating removeKeys unboundedly before one final patch.","solutions":["Split the patch into batches of at most ~1000 combined entries and call PatchViewState per batch","Reduce entries by removing only stale keys rather than sending the entire state each time","If legitimately needing large bulk updates, perform sequential smaller patches inside a loop"],"exampleFix":"// before\nPatchViewState(key, allValues /* 5000 entries */, allRemoveKeys)\n// after\nfor batch := range chunks(allValues, 500) {\n    PatchViewState(key, batch, nil)\n}","handlingStrategy":"validation","validationCode":"func canPatch(values map[string]any, removeKeys []string) bool {\n    return len(values)+len(removeKeys) <= 1000\n}","typeGuard":null,"tryCatchPattern":"ret, err := PatchViewState(key, values, removeKeys)\nif err != nil && err.Error() == \"view state patch contains too many entries\" {\n    // retry in batches of <= 1000 entries\n}","preventionTips":["Batch patches to at most ~1000 combined values+removeKeys entries","Do not send the full state object every time; send only changed keys","Track accumulated removeKeys sizes in bulk-clear loops"],"tags":["view-state","limit","batching"],"backgroundTag":"payload-too-large","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"}