{"record":{"id":"bd22b0c0ece4c11e","repo":"siyuan-note/siyuan","slug":"view-state-patch-cannot-be-split-within-the-storag","errorCode":null,"errorMessage":"View state patch cannot be split within the storage limits","messagePattern":"View state patch cannot be split within the storage limits","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/util/viewState.ts","lineNumber":232,"sourceCode":"            }\n            values[field] = value;\n            this.pendingValues.delete(field);\n            count++;\n        }\n        if (count < MAX_PATCH_ENTRIES && this.pendingValues.size === 0) {\n            for (const field of this.pendingRemovals) {\n                const nextRemoveKeys = [...removeKeys, field];\n                if (count > 0 && (count >= MAX_PATCH_ENTRIES ||\n                    getPatchByteLength(values, nextRemoveKeys) > MAX_PATCH_BYTES)) {\n                    break;\n                }\n                removeKeys.push(field);\n                this.pendingRemovals.delete(field);\n                count++;\n            }\n        }\n        if (count === 0) {\n            throw new Error(\"View state patch cannot be split within the storage limits\");\n        }\n        return {values, removeKeys};\n    }\n\n    private validateValue(field: string, value: TViewStateValue) {\n        if (getPatchByteLength({[field]: value}, []) > MAX_PATCH_BYTES) {\n            throw new Error(\"View state value is too large\");\n        }\n    }\n\n    private scheduleFlush() {\n        this.clearFlushTimer();\n        this.flushTimer = setTimeout(() => {\n            this.flushTimer = undefined;\n            this.flush().catch((error) => console.error(error));\n        }, this.flushDelay);\n    }\n","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/util/viewState.ts#L214-L250","documentation":"ViewStateService batches pending field writes and removals into a single patch bounded by MAX_PATCH_ENTRIES (1000) and MAX_PATCH_BYTES (256 KiB). takePendingBatch always takes at least the first pending item even if it exceeds the limits; if it cannot take even one item (count stays 0), no valid batch exists and the service throws. This happens when a single item is both over the byte limit by itself yet escaped validateValue, or the pending maps are inconsistent.","triggerScenarios":"flushPending -> takePendingBatch finds pendingValues/pendingRemovals non-empty but every candidate is rejected: a single pending field whose serialized JSON with empty removeKeys already exceeds 256 KiB while count===0 (batch loop only enforces limits when count>0, so an oversized first item would be taken — thus this practically fires when pendingRemovals alone holds keys whose combined key length JSON exceeds MAX_PATCH_BYTES on the first removal, or internal state desync).","commonSituations":"A removal key list with extremely long field names (validateField allows up to 2048 chars each, 1000+ keys accumulate if flushes keep failing); a custom transport whose patch() rejects repeatedly causing pendingRemovals to grow; storing one huge value that slips past validateValue via a key-length interaction.","solutions":["Inspect the pending values/removals before flushing and shrink or split them yourself via patch() with explicit removeKeys","Shorten view state field names; keep each field name well under 2048 chars and the total removal list small","Call flush() more frequently so batches stay small instead of letting thousands of removals accumulate","Catch the error from flush() and fall back to re-creating the service or clearing state"],"exampleFix":"// before: one field value near the limit plus many removals in one batch\nservice.patch({huge: bigString}, Array.from({length: 2000}, (_, i) => \"field-\".padEnd(2000, \"x\") + i));\n// after: flush values and removals in separate, small calls\nservice.patch({huge: bigString});\nawait service.flush();\nfor (const key of removalKeys) { service.remove(key); await service.flush(); }","handlingStrategy":"validation","validationCode":"const bytes = new TextEncoder().encode(JSON.stringify({values, removeKeys})).length;\nif (bytes > 256 * 1024) { /* shrink or split before handing to the service */ }","typeGuard":"const fitsPatch = (v: unknown) =>\n  new TextEncoder().encode(JSON.stringify(v)).byteLength <= 256 * 1024;","tryCatchPattern":"try {\n  await service.flush();\n} catch (e) {\n  if (e.message.includes(\"cannot be split\")) {\n    console.error(\"Patch unsplittable; reduce pending state\", e);\n    // rebuild or clear pending state\n  }\n}","preventionTips":["Keep field names short and values small","Flush frequently instead of accumulating thousands of pending removals","Pre-check serialized size of large values before set/patch","Provide a custom transport and monitor patch sizes"],"tags":["storage","limit-exceeded","frontend"],"backgroundTag":"file-size-limit-exceeded","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"}