Skip to content

[Bug] Deeply nested list can crash the renderer with structuredClone stack overflow #4747

Closed

Description

@Renakoni

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

  1. 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)"
  1. Start the desktop app:
pnpm run dev
  1. Open deep-nested-list-600.md in MarkText.

The generated Markdown starts like this:

- item 0
  - item 1
    - item 2

and the file continues nesting until:

... deeply indented ...
- item 599

btw: 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-135
  • packages/muya/src/state/index.ts:68-82
  • packages/muya/src/state/index.ts:89-111
  • packages/muya/src/state/index.ts:222-223
  • packages/muya/src/editor/index.ts:276-282
  • packages/muya/src/editor/index.ts:496-500
  • packages/muya/src/muya.ts:268-269
  1. 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);
}
  1. packages/muya/src/state/index.ts

getState() returns a deep clone of the current state:

getState(): TState[] {
    return deepClone(this._state);
}
  1. packages/muya/src/utils/index.ts

deepClone() delegates directly to structuredClone():

export function deepClone<T>(value: T): T {
    return structuredClone(value);
}
  1. 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 when structuredClone() throws a RangeError.
  • 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 public muya.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

  1. added a commit that references this issue on Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions