Repository navigation
Description
Description
A Markdown document with hundreds of nested bullet-list levels can trigger a renderer-side RangeError: Maximum call stack size exceeded.
The failure is caused by cloning the parsed Muya editor state with structuredClone(). The document parses into a very deeply nested state tree; then JSONState.getState() calls deepClone(this._state), and deepClone() delegates directly to structuredClone(). Chromium/Electron exceeds its clone recursion depth before the editor can finish loading or rebuilding the document.
This appears to be depth-related, not just size-related. Shallower nested lists load successfully, and large flat documents do not hit this same boundary.
A 600-level nested list is an extreme input, but it is still plain Markdown text. If MarkText chooses not to support such deep nesting, it should fail gracefully with a controlled error instead of throwing an unhandled renderer-side stack overflow.
Steps to Reproduce
- Create a Markdown file containing a 600-level nested bullet list:
node -e "const fs=require('node:fs'); const md=Array.from({length:600},(_,i)=>' '.repeat(i)+'- item '+i).join('\n')+'\n'; fs.writeFileSync('deep-nested-list-600.md', md)"- Start the desktop app:
pnpm run dev
- Open
deep-nested-list-600.mdin MarkText.
The generated Markdown starts like this:
- item 0
- item 1
- item 2and the file continues nesting until:
... deeply indented ...
- item 599btw: the same failure can also be reproduced by callingmuya.setContent(markdown) with the generated 600-level Markdown in a Muya test page. The desktop file-open path and the direct setContent() path both fail inJSONState.getState() -> deepClone().
Expected Behavior
MarkText should not crash or leave the renderer half-loaded when opening a valid Markdown file with deeply nested list structure.
Reasonable behavior would be any of:
- Load the document successfully.
- Reject excessive nesting with a controlled, user-visible error.
- Gracefully degrade rendering for the deeply nested subtree.
The renderer should not throw an unhandled RangeError from internal state cloning.
Actual Behavior
The 600-level nested list can throw:
RangeError: Failed to execute 'structuredClone' on 'Window': Maximum call stack size exceeded
In a direct Muya API probe, this is reproducible from muya.setContent(markdown) using the generated 600-level Markdown.
Observed control data from the same probe:
[
{
"n": 100,
"ok": true,
"ms": 46,
"stateLen": 1,
"inputLength": 10890,
"outputLength": 10890
},
{
"n": 300,
"ok": true,
"ms": 281,
"stateLen": 1,
"inputLength": 92890,
"outputLength": 92890
},
{
"n": 600,
"ok": false,
"ms": 351,
"name": "RangeError",
"message": "Failed to execute 'structuredClone' on 'Window': Maximum call stack size exceeded"
}
]In desktop development mode, opening the 600-level repro file has also produced an unexpected renderer-process error with this stack:
Unexpected renderer process error
RangeError: Maximum call stack size exceeded
at deepClone (packages/muya/src/utils/index.ts)
at JSONState.getState (packages/muya/src/state/index.ts)
at Editor.setContent (packages/muya/src/editor/index.ts)
at Muya.setContent (packages/muya/src/muya.ts)
at handleFileChange (packages/desktop/src/renderer/src/components/editorWithTabs/editor.vue)
An isolated Electron open-file probe reproduced the same root failure during initial editor mount:
RangeError: Failed to execute 'structuredClone' on 'Window': Maximum call stack size exceeded
at deepClone
at JSONState.getState
at Editor.init
at Muya.init
Screenshots / Logs
No screenshot is required to see the failure; the console stack is the useful artifact.
Primary stack from the direct Muya API repro:
RangeError: Maximum call stack size exceeded
at deepClone (packages/muya/src/utils/index.ts)
at JSONState.getState (packages/muya/src/state/index.ts)
at Editor.setContent (packages/muya/src/editor/index.ts)
at Muya.setContent (packages/muya/src/muya.ts)
The likely failing path is in Muya state cloning, after Markdown has already been converted into the internal state tree.
On current upstream/develop (68ce87ca):
Current source references:
packages/muya/src/utils/index.ts:134-135packages/muya/src/state/index.ts:68-82packages/muya/src/state/index.ts:89-111packages/muya/src/state/index.ts:222-223packages/muya/src/editor/index.ts:276-282packages/muya/src/editor/index.ts:496-500packages/muya/src/muya.ts:268-269
packages/muya/src/state/index.ts
JSONState parses Markdown into _state:
constructor(private _muya: Muya, stateOrMarkdown: TState[] | string) {
this.setContent(stateOrMarkdown);
}
setContent(content: TState[] | string) {
if (typeof content === 'object')
this._setState(content);
else
this._setMarkdown(content);
}
private _setMarkdown(markdown: string) {
this._state = this.markdownToState(markdown);
}packages/muya/src/state/index.ts
getState() returns a deep clone of the current state:
getState(): TState[] {
return deepClone(this._state);
}packages/muya/src/utils/index.ts
deepClone() delegates directly to structuredClone():
export function deepClone<T>(value: T): T {
return structuredClone(value);
}packages/muya/src/editor/index.ts
Editor.init() immediately clones the just-parsed state:
const state = this.jsonState.getState();
this.scrollPage = ScrollPage.create(muya, state);Editor.setContent() also immediately clones after replacing content:
this.jsonState.setContent(content);
const state = this.jsonState.getState();
this.scrollPage!.updateState(state);That means a deeply nested Markdown document can parse into _state, but then fail before rendering because structuredClone() recursively clones the deeply nested state tree and exhausts the browser stack.
Potential fix points:
- Avoid cloning the entire state immediately after internal
setContent()/init()when ownership is known and the result is only used to build/update the editor view. - Replace
structuredClone()for editor state with a stack-safe iterative clone for plain arrays/objects, or add a fallback whenstructuredClone()throws aRangeError. - Add a maximum nesting-depth guard during Markdown-to-state conversion and surface a controlled error before the state reaches clone/render paths.
- If
getState()must preserve defensive-copy semantics for public API callers, consider an internal non-cloning accessor for trusted editor-render paths and keep publicmuya.getState()cloned.
Operating System
Windows
MarkText Version
0.20.0-beta.2
OS Version
Windows 11 Pro
Reproducibility
- I can reliably reproduce this issue
Activity
fix: cherry-pick seven upstream fixes; cure state-clone aliasing
fix(muya): #4747 deep clone editor state without recursing (#5259)
test(muya): stop the deepClone spec from overflowing the stack itself (…