Skip to content

fix: prevent blockquote rendering and UIKit teardown crashes - #24

Merged
gtokman merged 2 commits into
mainfrom
codex/fix-markdown-rendering-crashes
Nov 16, 2026
Merged

gtokman merged 2 commits into
mainfrom
codex/fix-markdown-rendering-crashes

Conversation

@gtokman

@gtokman gtokman commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Single-line RTL blockquotes can invoke their end callback before their start callback, crashing or drawing a bar across a following paragraph. Identify start/end marker runs so a single-line quote gets its bounds from that line, independently of visual run order. Keep the missing-start guard for clipped draws. Pixel tests cover the first draw, redraw, separated RTL quotes, and a wrapped quote's full bar.

Avoid Swift's isolated-deinit back-deployment path for attachments and the UIKit label/code/table teardown cases reproduced in review. UIKit label cleanup still executes on the main actor via MainActor.assumeIsolated; AppKit keeps its isolated cleanup. Include the nested UIKit views in those lifetimes and document that attachment subclasses must opt out separately and stored views are released on the final releaser's thread.

Also let bare TextBuilder instances build quotes, code, diffs, and tables without force-unwrapping absent drawing callbacks.

Validation with Swift 6.3.3:

  • 18 focused macOS blockquote, tiled-layout, rendering-lifetime, and highlight-lifetime tests pass.
  • 18 tests pass on an iPhone 16 / iOS 18.6 simulator: all 14 rendering-lifetime tests and all 4 blockquote drawing tests. Built with SwiftPM and ran the XCTest suites through a directly compiled simulator executable; no xcodebuild was invoked.
  • The new RTL pixel tests fail against the pre-review drawing implementation. The pre-review UIKit teardown implementation aborts in the code-view lifetime test on iOS 18.6; the updated implementation passes.
  • The UIKit package target compiles and git diff --check passes. This is focused package validation, not a full v0 app test.

Remaining scope:

  • Tiled quote drawing still shares a saved start position. Partial/out-of-order draws can miss bars or carry state between quotes; concurrent Markdown tile drawing is not fixed. A stateless rendering follow-up is still needed, as discussed in review.
  • The additional diff/math isolated-deinit classes listed in review are not covered by this change.
  • Existing CI still runs macOS tests only; the iOS 18.6 regression suites were run locally, not added to CI.

@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
markdown-view Ready Ready Preview, v0 Nov 16, 2026 5:16pm UTC
markdownview Ready Ready Preview, v0 Nov 16, 2026 5:16pm UTC

Request Review

@gtokman
gtokman force-pushed the codex/fix-markdown-rendering-crashes branch from 6359792 to 4ecf7d4 Compare November 16, 2026 16:04
@gtokman
gtokman marked this pull request as ready for review November 16, 2026 16:05

@gtokman gtokman left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewed at 4ecf7d4 and ran both fixes locally with Swift 6.3.3. Both do what they claim:

  • Blockquote: BlockquoteDrawingTests traps at TextBuilder+Do.swift:256 against f3a32b7's sources and passes here.
  • Attachments: on an iOS 18.6 simulator, testAttachmentReleaseInsideSynchronousTaskLocalScope crashes in swift_task_deinitOnExecutorMainActorBackDeploy against f3a32b7's sources and passes here.

The inline comments cover what is left:

  1. A second way to hit the blockquote crash on the normal (untiled) draw path, single-line right-to-left quotes, which now renders wrong instead of crashing.
  2. The start marker leaking between draw passes.
  3. The classes that still crash with the same teardown signature on iOS 18.6.
  4. The new lifetime tests passing on macOS with or without the fix.

One thing outside the diff, from reading the code rather than running it: BlockProcessor force-unwraps its optional callbacks (blockquoteMarking!, blockquoteDrawing!, codeDrawing!, diffDrawing!, tableDrawing!). A bare TextBuilder(nodes:context:viewProvider:).build(), the shape RenderingLifetimeTests uses, will trap as soon as the markdown has a quote, code block, diff or table.

Comment on lines +256 to +257
// A visible tile can include the end of a quote without its start marker.
guard let top = blockquoteMarkingStorage else { return }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This guard also covers a trigger that has nothing to do with tiles.

processLineDrawingActions walks CTLineGetGlyphRuns, which is in visual order. For a single-line right-to-left quote the end marker's run comes before the start marker's run on the same line, so the end callback fires first. > مرحبا بالعالم drawn through plain layout.draw(in:) (what LTXLabel.draw uses) traps at this line against f3a32b7's sources. In Sources, draw(in:visibleRect:) is only called by DiffTileRenderer on diff text, so this is the path a Markdown label can actually reach today.

With the guard it no longer crashes, but the output is wrong:

  • One RTL quote: no bar on the first draw. Later draws get a bar only because the start position stored at the end of the previous pass is still there.
  • Two single-line RTL quotes with a paragraph between them: one bar from the top of the first quote to the bottom of the second (y 0...452 in my run), straight across the paragraph.

Suggestions: add a regression test that draws an RTL quote with layout.draw(in:), and make the end callback independent of run order (for example, when the start marker's run is on the same line, use that line's maxY).

@@ -253,10 +253,12 @@ extension TextBuilder {
.withBlockquoteDrawing { context, line, lineOrigin in
let boundingBox = lineBoundingBox(line, lineOrigin: lineOrigin)
defer { blockquoteMarkingStorage = nil }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The stored start position survives between draw passes. It is only cleared when an end callback runs, so a pass that sees a start without its end leaves a value behind for the next pass. With clipped draws on a document that has two long quotes and a paragraph between them:

  • Draw the tile holding quote B's start, then the tile holding quote A's end: a bar is drawn from A's end down to B's start, over the paragraph (the negative-height rect is normalized).
  • Draw one long quote as three tiles, top to bottom: no bar in the first two tiles, and a bar in the last one only because the first tile's start was still stored.

It is also a single captured var shared by every quote in the document, so it is not safe if a Markdown layout is ever drawn from concurrent tile renders the way DiffTileRenderer draws diffs.

The description already scopes continuous bars out, so this can be a follow-up. A stateless version, where each quote line draws its own bar segment from its own bounds, would fix the pass-order dependence, the RTL case above and the missing tiles together.

Comment on lines +44 to +46
layout.draw(in: context, visibleRect: visibleRect)
layout.draw(in: context)
layout.draw(in: context, visibleRect: visibleRect)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

These three calls only prove that drawing does not crash. With the guard, the first call draws no bar for the quote's last line, and nothing here would catch a change in bar geometry.

Consider asserting on the pixels in the bar column (x 0..<4), at least for the full layout.draw(in:), the way TiledLayoutTests compares pixels(_:).

Comment on lines +19 to +20
// Synchronous rendering must avoid the isolated-deinit back-deployment path.
nonisolated deinit {}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Confirmed this takes effect. Against f3a32b7's sources both LTXAttachment and LTXHolderAttachment get an implicit __isolated_deallocating_deinit that goes through _deinitOnExecutorMainActorBackDeploy; here they only have a plain deallocating deinit.

The same teardown crash is still reachable through other classes. On an iOS 18.6 simulator at this commit, releasing these inside the synchronous task-local scope the lifetime tests use aborts with malloc: pointer being freed was not allocated in swift_task_deinitOnExecutorImpl → TaskLocal::StopLookupScope::~StopLookupScope:

  • LTXLabel on its own, through CodeView.textView, and through MarkdownTextView teardown
  • TableViewCellManager through TableView teardown

Classes in the iOS build that still have an isolated deallocating deinit:

  • Litext: LTXLabel, LTXSelectionHandle
  • MarkdownView: CodeView, LineNumberView, SymbolActionView, TableView, TableViewCellManager, GridView, LineSelectionOverlayView, DiffView, DiffGutterView, DiffContentBackgroundView, DiffTiledContentView, DiffHunkCell, DiffExpanderCell, ExpanderControl, DiffFileHeaderView, DiffFilesViewController, MathPreviewController, MathPreviewDataSource, MathPreviewItem, MathRenderer.CacheKey

MathRenderer.CacheKey is in that list but did not crash when I released it the same way.

As an experiment I gave the UIKit CodeView, LineNumberView, SymbolActionView, TableView, TableViewCellManager, GridView, LineSelectionOverlayView and LTXSelectionHandle an explicit nonisolated deinit {}, and changed LTXLabel to nonisolated deinit { MainActor.assumeIsolated { ... } }. The four crashing cases then pass on iOS 18.6. assumeIsolated relies on UIKit deallocating views on the main thread, so the AppKit variants would need a different answer.

Two smaller points on this line:

  • Subclasses do not inherit it (the holder needed its own). External subclasses of this open class in a main-actor-default module need the same line, which is worth saying in the comment.
  • view is now released on whichever thread drops the last reference to the attachment, instead of on the main actor.

Comment on lines +12 to +14
func testAttachmentReleaseInsideSynchronousTaskLocalScope() {
checkSynchronousRelease { LTXAttachment() }
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

On macOS these two tests pass with and without the deinit change (I ran them against f3a32b7's sources), and CI only runs swift test on macos-latest. So they do not guard the fix where they currently run.

On an iOS 18.6 simulator testAttachmentReleaseInsideSynchronousTaskLocalScope crashes against f3a32b7's sources and passes here. The run aborted at that first crash, so I did not get a base result for the holder test. Running this class on an iOS 18 simulator in CI would make them real regression tests:

xcodebuild test -scheme MarkdownView-Package \
  -destination 'platform=iOS Simulator,OS=18.6,name=iPhone 16' \
  -only-testing:MarkdownViewTests/RenderingLifetimeTests

@gtokman gtokman changed the title fix: prevent blockquote and attachment teardown crashes fix: prevent blockquote rendering and UIKit teardown crashes Nov 16, 2026
@gtokman
gtokman merged commit 37c676f into main Nov 16, 2026
7 checks passed

This branch was successfully deployed

1 active deployment
Preview – markdown-view — 1c1e8efc Deployed Nov 16, 2026 by vercel[bot]
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