Skip to content

fix(markdown): cap rendered size of large Markdown previews to stop renderer freezes - #23634

Merged
OrcaWin merged 3 commits into
mainfrom
fix/scan29-large-markdown-render-freeze
Oct 2, 2026
Merged

OrcaWin merged 3 commits into
mainfrom
fix/scan29-large-markdown-render-freeze

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​265 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​264
Prod 13 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​175 $\color{#cf222e}{\Huge{\mathbf{−}}}$​37 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​138

Description

Opening a very large Markdown file could freeze Orca (and the user's whole PC) for tens of seconds and push the window's memory past 1-4 GB, until the user force-quit the app.

Before: The "Open Preview" tab and the Preview toggle on a Markdown diff tab rendered the whole file at once, whatever its size. In rich mode, "Open anyway" removed the existing 600 KB cap entirely.
After: Every path that renders a whole Markdown document at once now has a size check:

  • Open Preview tab: over 600 KB, a notice appears with a Render anyway button. Over 1 MiB, the file is never rendered and the notice points to the source view.
  • Diff-tab Preview toggle: gated the same way. The notice tells the user to switch to Source mode to see the diff.
  • Rich mode "Open anyway": now allowed only up to 1 MiB. Above that, the button is hidden and the file stays in the normal source editor.

Mechanism:

  • markdown-rich-size-limit.ts adds canRenderMarkdownAtSize, a fast UTF-8 byte-size check with a hard 1 MiB cap, next to the existing 600 KB soft limit.
  • The new MarkdownPreviewSizeGate component wraps MarkdownPreview in EditorContent.tsx (the preview tab) and in EditorDiffFileSurface.tsx (the modified side of a diff).
  • The per-tab "override" flag the rich-mode banner already uses is reused, keyed by the tab id. The existing tab close, rekey and cleanup code already clears it.
  • getMarkdownRichModeEligibilityDecision now respects the hard cap even when the override is set.
  • Export to PDF from a preview tab uses the same gate.
  • The notice text is added to all six locale files.

Why

The Markdown renderer (react-markdown with the gfm, math, raw-HTML, slug, highlight and KaTeX plugins) builds the whole document in one synchronous main-thread task. It is not chunked or virtualized, so cost grows with file size and there is nothing to yield to. Adding a size gate is the smallest change that removes the freeze. It matches the existing rich-mode fallback, so users see a familiar pattern.

Alternatives considered:

  • Chunked or virtualized preview. This would fix the problem at its root, but it is a large rewrite of the preview. Heading anchors, find and PDF export all assume a single DOM.
  • Web Worker parsing. The heavy memory is DOM and Blink layout, not parsing, so moving the parse off the main thread would not stop the freeze.

Linked Issue

No GitHub issue. Linked crash reports are listed below.

Fix evidence

1. Real-app repro on the released Electron. A hidden Electron 43.7.0 probe (same Electron as v1.4.215; show:false) mounted the production MarkdownPreviewBody.tsx and recorded long tasks, DOM nodes and working set:

File Longest main-thread task DOM nodes Renderer working set
300 KB 1.4 s 70k 562 MB
590 KB 1.6 s 138k 896 MB
2 MB 7.8 s 471k 2.3 GB
5 MB 38 s 1.17M 4.0 GB

The V8 heap stays small (159 and 391 MB), so almost all of the memory is in Blink and the DOM. The crash telemetry has the same shape: renderer private memory 498 -> 1531 MB, Blink 32 -> 427 MB, V8 about 105 MB, main-thread gaps of 68-88 s between memory samples, and then a user kill.

2. Regression tests. Repro command:

pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/editor/large-markdown-render-freeze.test.tsx src/renderer/src/components/editor/markdown-rich-size-limit.test.ts

Tests in large-markdown-render-freeze.test.tsx:

  • "Open anyway" still reaches the rich editor below the hard cap

  • "Open anyway" cannot push a document past the hard cap into the rich editor

  • preview tab renders small documents directly

  • preview tab gates documents over the preview limit behind "Render anyway"

  • preview tab never renders documents over the hard cap

  • diff preview toggle gates the modified side over the preview limit

  • diff preview toggle never renders a modified side over the hard cap

  • Before the fix, with EditorContent.tsx and markdown-rich-mode.ts reverted to main: 3 of the first 5 cases fail. With EditorDiffFileSurface.tsx reverted: × diff preview toggle gates the modified side over the preview limit and × diff preview toggle never renders a modified side over the hard cap fail (Tests 2 failed | 5 passed).

  • After the fix: Test Files 2 passed (2) / Tests 14 passed (14).

3. Wider checks.

  • pnpm test src/renderer/src/components/editor: 257 files, 1823 passed, 3 skipped.
  • i18n suite: passes.
  • pnpm tc: exits 0.
  • oxlint: clean.
  • pnpm run check:code-quality:changed: 0 findings.

ELI5

Orca used to try to paint a whole huge Markdown file onto the screen in one go. For a 5 MB file that is over a million pieces, and while it paints, nothing else can happen: the app freezes for up to 38 seconds and eats 4 GB of memory.

Now Orca checks the file's size first:

  • If the file is big, it asks "Render anyway?".
  • If the file is huge, it doesn't try at all and shows you the plain text instead, which stays fast at any size.

Trade-offs

  • Preview tab and diff-tab Preview over 600 KB: these now open to a notice instead of the rendered preview. Up to 1 MiB, the user can click Render anyway. Over 1 MiB, rendered preview is not available and the user must read the source.
  • Rich mode "Open anyway": no longer works above 1 MiB. Those files stay in the source editor.
  • No source button on the preview-tab notice: it says to open the file but has no button that does it.
  • Rendering below the hard cap can still be slow: an explicit "Render anyway" up to 1 MiB can still cause a multi-second stall (about 3.8 s and 1.3 GB, estimated).
  • Diff preview shares the rich-mode choice in Changes mode: there the override key is the edit tab's id, so "Open anyway" in rich mode also skips the diff-preview notice for that file. It is still capped at 1 MiB.
  • Machine-drafted translations: the new es/fr/ja/ko/zh strings were not written by a translator. A native speaker may want to polish them.

Adversarial review

It took 2 loops to come back clean. Each loop was a fresh reviewer.

  • Loop 1: 2 issues, both fixed.
    1. The Preview toggle on a single-file Markdown diff tab was still ungated. It was bounded only by the large-diff limit of 6M characters. Fixed: the diff side is now wrapped in MarkdownPreviewSizeGate keyed by the diff tab id, with diff-specific wording, and 2 tests were added.
    2. The new notice strings were missing from the locale catalogs. Fixed: they are added to en/es/fr/ja/ko/zh.
  • Loop 2: clean. The reviewer checked every path that renders Markdown:
    • The remaining react-markdown users are comments, notebook cells and composers. All of them hold small, user-written text.
    • PDF export is gated with the same key.
    • The override is cleaned up on tab close.
    • The fast paths of the UTF-8 byte check are sound.
    • The reviewer re-confirmed that the tests fail without the fix.

Linked crash reports

Visual Proof

N/A for screenshots. This is a size gate that shows a text notice in place of the rendered preview. The behavior is covered by the component tests above.

Testing

  • Tested locally in a hidden Electron 43.7.0 probe (macOS) for the before measurements.
  • Automated tests added and updated.

This is renderer-only, content-string logic, with no platform, SSH or folder-workspace specific behavior.

Agent skill upstream boundary

  • Not applicable.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • Typecheck, oxlint and targeted tests pass locally

Additional merge-risk verification (2026-10-02)

Integrated current main and resolved the Spanish catalog conflict by retaining every current-main entry and adding the three Markdown preview translations. On this integrated head, 26 regression tests passed, renderer typecheck passed, and the changed-code quality gate passed with zero findings. Existing unrelated untracked reproduction artifacts were excluded from that gate. The recorded hidden Electron reproduction establishes the huge-document freeze; the second field report lacks evidence that a Markdown file was open. The 600 KiB default / 1 MiB absolute cap intentionally leaves huge files in plain text.

m4air added 2 commits September 28, 2026 07:18
…rride

The Open Preview tab rendered any file synchronously through react-markdown
with no size check, and 'Open anyway' removed the rich-editor cap entirely.
A 2 MB file blocked the renderer 7.8 s at 2.3 GB (5 MB: 38 s, 4 GB), matching
scan29 crash reports where users killed a frozen Orca after opening a large .md.

- Preview tab: over 600 KB shows a 'Render anyway' gate instead of rendering.
- Both overrides stop at a 1 MiB hard cap; above it no render is offered.
…te copy

The single-file diff Preview toggle sent the whole modified side to
MarkdownPreview with only the 6 MB large-diff limit, so a multi-MB .md diff
still froze the renderer. Wrap it in MarkdownPreviewSizeGate keyed by the
diff tab id, and add the gate's strings to all locale catalogs.
@coderabbitai

coderabbitai Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 8a3768b0-89f8-4be0-b429-90c570f0cec8

📥 Commits

Reviewing files that changed from the base of the PR and between 8821b05 and 5d2e1bc.

📒 Files selected for processing (5)
  • src/renderer/src/i18n/locales/es.json
  • src/renderer/src/i18n/locales/fr.json
  • src/renderer/src/i18n/locales/ja.json
  • src/renderer/src/i18n/locales/ko.json
  • src/renderer/src/i18n/locales/zh.json
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/renderer/src/i18n/locales/ja.json
  • src/renderer/src/i18n/locales/ko.json
  • src/renderer/src/i18n/locales/es.json
  • src/renderer/src/i18n/locales/zh.json
  • src/renderer/src/i18n/locales/fr.json

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

Markdown rendering now uses a shared UTF-8 size check with a 1 MiB override limit. Rich mode, Markdown previews, and diff previews apply size limits and show override actions where allowed. PDF export eligibility checks the selected preview content against the active limit. Tests cover byte counting, override boundaries, and preview rendering. Locale entries provide preview and diff size-limit messages.

Priority: ➖ Normal

Merge Risk: 🟡 Moderate · up to 5d2e1

Large Markdown files can still freeze the app when opened in Edit-tab Preview, even above the 1 MiB cap. Gate that path and its PDF eligibility before merging.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 8821b

The main Markdown rendering paths now limit large documents, including when a user chooses to render anyway. No new security weakness was established, though the review does not cover every rendering path or every costly document shape.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The addressed availability exposure is rendering document or diff content in the desktop renderer. The supplied paths do not establish a change to server-side authority or a cross-tenant boundary.

Trust Boundaries and Controls

  • observed — The gate checks the content passed to its renderer before mounting it; a stored override changes the permitted limit but cannot remove the hard cap.

Resilience and Maintainability Implications

  • inferred — Rechecking current content at render time limits the effect of a stale override when a document grows; tab close, replacement, and rekey handling maintain the override’s tab ownership.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.38% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 10 files. (5 skipped:… Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The description is detailed and covers the change, motivation, testing, visual proof, risks, and checklist. However, it does not provide the required actual linked issue; it states that no GitHub issu… Add an actual linked issue using the required Fixes #<issue-number> format. If the author is a maintainer and no new issue is needed, link a relevant existing issue and explain the exception. Complete any remaining required checklist item…
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the primary change: limiting rendered Markdown preview size to prevent renderer freezes.
Full details: Docstring Coverage

Explanation

Docstring coverage is 15.38% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 10 files. (5 skipped: 5 unsupported.)

Full details: Description check

Explanation

The description is detailed and covers the change, motivation, testing, visual proof, risks, and checklist. However, it does not provide the required actual linked issue; it states that no GitHub issue exists and only lists crash-report links.

Resolution

Add an actual linked issue using the required Fixes #&lt;issue-number&gt; format. If the author is a maintainer and no new issue is needed, link a relevant existing issue and explain the exception. Complete any remaining required checklist items or state why they do not apply.

  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · Gate the edit-tab Preview mode before rendering. · EditorMarkdownFileSurface.tsx:151-153

src/renderer/src/components/editor/EditorMarkdownFileSurface.tsx:151-153
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Gate the edit-tab Preview mode before rendering.

When a user selects Preview on a Markdown edit tab, this branch mounts MarkdownPreview without a size check. getEditorPanelRenderModel skips rich-size classification in Preview mode, so a document above 1 MiB can still reach the whole-document renderer. Wrap this preview in MarkdownPreviewSizeGate using activeFile.id and currentContent. Apply the same size decision to edit-tab PDF export eligibility, which currently accepts a non-source render mode without checking its content size.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: f89d5fa2-75de-45e4-9721-c5ac7b2c919d

📥 Commits

Reviewing files that changed from the base of the PR and between aedb930 and 8821b05.

📒 Files selected for processing (16)
  • src/renderer/src/components/editor/EditorContent.markdown-classification.test.tsx
  • src/renderer/src/components/editor/EditorContent.tsx
  • src/renderer/src/components/editor/EditorDiffFileSurface.tsx
  • src/renderer/src/components/editor/EditorMarkdownFileSurface.tsx
  • src/renderer/src/components/editor/MarkdownPreviewSizeGate.tsx
  • src/renderer/src/components/editor/editor-panel-render-model.ts
  • src/renderer/src/components/editor/large-markdown-render-freeze.test.tsx
  • src/renderer/src/components/editor/markdown-rich-mode.ts
  • src/renderer/src/components/editor/markdown-rich-size-limit.test.ts
  • src/renderer/src/components/editor/markdown-rich-size-limit.ts
  • src/renderer/src/i18n/locales/en.json
  • src/renderer/src/i18n/locales/es.json
  • src/renderer/src/i18n/locales/fr.json
  • src/renderer/src/i18n/locales/ja.json
  • src/renderer/src/i18n/locales/ko.json
  • src/renderer/src/i18n/locales/zh.json

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No blocking issues — the size gate covers the reachable render paths. One coverage note and a small copy nitpick below.

Reviewed changes

Reviewed the complete 16-file diff at 8821b05.

  • markdown-rich-size-limit.ts — extracted exceedsUtf8ByteLimit, added the 1 MiB MARKDOWN_RENDER_OVERRIDE_MAX_SIZE_BYTES hard cap and canRenderMarkdownAtSize; reuses one probe buffer for both limits.
  • MarkdownPreviewSizeGate.tsx — new wrapper that shows a notice plus "Render anyway" over the 600 KB soft limit, and no button over the hard cap.
  • EditorContent.tsx / EditorDiffFileSurface.tsx — gate the preview tab and the diff-tab preview.
  • editor-panel-render-model.ts — gate the preview tab's PDF export with the same size decision.
  • markdown-rich-mode.ts / EditorMarkdownFileSurface.tsx — rich eligibility respects the hard cap even when overridden; "Open anyway" is hidden above it.
  • i18n — editor.markdownPreview.* added to en/es/fr/ja/ko/zh.
  • Tests — new large-markdown-render-freeze.test.tsx plus extended size-limit cases.

ℹ️ The diff-preview gate wraps a branch the UI cannot currently enter

I traced every producer of markdownViewMode and the diff/edit preview mode is unreachable: getMarkdownViewModes returns only ['source', 'rich'], and getEditorPanelRenderModel drops any stored value not in that list. So the mdViewMode === 'preview' branch in EditorDiffFileSurface (and the renderMode === 'preview' branch in EditorMarkdownFileSurface, whose <MarkdownPreview> is still ungated) is dead code today. The reachable freeze paths (preview tab, rich-editor override, PDF export) are correctly gated, so the fix itself is effective; the new diff-path tests characterize an injected prop rather than a user-reachable state.

Technical details
# Diff-tab preview gate and its tests cover an unreachable state

## Affected sites
- `src/renderer/src/components/editor/EditorDiffFileSurface.tsx:107` — `mdViewMode === 'preview'` branch the new gate wraps
- `src/renderer/src/components/editor/EditorMarkdownFileSurface.tsx:140` — sibling `renderMode === 'preview'` branch, renders `<MarkdownPreview>` without the gate
- `src/renderer/src/components/editor/editor-panel-render-model.ts:103` — `mdViewMode` constrained by `markdownViewModes.includes(stored)`
- `src/renderer/src/components/editor/markdown-preview-controls.ts:9-14,40-69` — no `*_VIEW_MODES` list contains `'preview'`
- `src/renderer/src/components/editor/large-markdown-render-freeze.test.tsx:163` — test injects `mdViewMode: 'preview'` directly

## Required outcome
- No change is needed for the freeze fix: preview tab, rich override, and PDF export are gated.
- If the inline diff/edit preview is intended to ship, its producer must assign `markdownViewMode = 'preview'`; until then the branch and its tests exercise dead code.

## Open questions for the human
- Is the diff-tab inline "Preview" toggle still planned (should `MARKDOWN_DIFF_VIEW_MODES`/`MARKDOWN_EDIT_VIEW_MODES` gain `'preview'`)? The PR description reads as though it exists today, which is why the diff gate was added.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

: translate(
'editor.markdownPreview.tooLarge',
'File is larger than the {{limit}} preview limit. Open the file to view its source.',
{ limit: formatBytes(RICH_MARKDOWN_MAX_SIZE_BYTES) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notice always cites the 600 KB soft limit, including files above the 1 MiB hard cap where no "Render anyway" button is offered. For those files rendering is not possible at all, so the wording (and the absent button) can read as a bug rather than a ceiling. Consider a distinct hard-cap string, or wording that says the file is too large to preview.

# Conflicts:
#	src/renderer/src/i18n/locales/es.json
@OrcaWin
OrcaWin merged commit 54be527 into main Oct 2, 2026
3 of 4 checks passed
@nwparker nwparker mentioned this pull request Oct 2, 2026
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant