siyuan-note/siyuan · error · Error

View state patch cannot be split within the storage limits

Error message

View state patch cannot be split within the storage limits

What it means

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.

Solutions

  1. Inspect the pending values/removals before flushing and shrink or split them yourself via patch() with explicit removeKeys
  2. Shorten view state field names; keep each field name well under 2048 chars and the total removal list small
  3. Call flush() more frequently so batches stay small instead of letting thousands of removals accumulate
  4. Catch the error from flush() and fall back to re-creating the service or clearing state

Example fix

// before: one field value near the limit plus many removals in one batch
service.patch({huge: bigString}, Array.from({length: 2000}, (_, i) => "field-".padEnd(2000, "x") + i));
// after: flush values and removals in separate, small calls
service.patch({huge: bigString});
await service.flush();
for (const key of removalKeys) { service.remove(key); await service.flush(); }
Defensive patterns

Strategy: validation

Validate before calling

const bytes = new TextEncoder().encode(JSON.stringify({values, removeKeys})).length;
if (bytes > 256 * 1024) { /* shrink or split before handing to the service */ }

Type guard

const fitsPatch = (v: unknown) =>
  new TextEncoder().encode(JSON.stringify(v)).byteLength <= 256 * 1024;

Try / catch

try {
  await service.flush();
} catch (e) {
  if (e.message.includes("cannot be split")) {
    console.error("Patch unsplittable; reduce pending state", e);
    // rebuild or clear pending state
  }
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "File too large" / "file size exceeds limit" errors: why libraries cap file sizes and how to fix them — this error's family across 46 libraries.

Related errors


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

Appendix: source

Thrown at app/src/util/viewState.ts:232

            }
            values[field] = value;
            this.pendingValues.delete(field);
            count++;
        }
        if (count < MAX_PATCH_ENTRIES && this.pendingValues.size === 0) {
            for (const field of this.pendingRemovals) {
                const nextRemoveKeys = [...removeKeys, field];
                if (count > 0 && (count >= MAX_PATCH_ENTRIES ||
                    getPatchByteLength(values, nextRemoveKeys) > MAX_PATCH_BYTES)) {
                    break;
                }
                removeKeys.push(field);
                this.pendingRemovals.delete(field);
                count++;
            }
        }
        if (count === 0) {
            throw new Error("View state patch cannot be split within the storage limits");
        }
        return {values, removeKeys};
    }

    private validateValue(field: string, value: TViewStateValue) {
        if (getPatchByteLength({[field]: value}, []) > MAX_PATCH_BYTES) {
            throw new Error("View state value is too large");
        }
    }

    private scheduleFlush() {
        this.clearFlushTimer();
        this.flushTimer = setTimeout(() => {
            this.flushTimer = undefined;
            this.flush().catch((error) => console.error(error));
        }, this.flushDelay);
    }

View on GitHub (pinned to 9f775e8a12)