Repository navigation
fix: prevent blockquote rendering and UIKit teardown crashes - #24
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
6359792 to
4ecf7d4
Compare
gtokman
left a comment
There was a problem hiding this comment.
Reviewed at 4ecf7d4 and ran both fixes locally with Swift 6.3.3. Both do what they claim:
- Blockquote:
BlockquoteDrawingTeststraps atTextBuilder+Do.swift:256against f3a32b7's sources and passes here. - Attachments: on an iOS 18.6 simulator,
testAttachmentReleaseInsideSynchronousTaskLocalScopecrashes inswift_task_deinitOnExecutorMainActorBackDeployagainst f3a32b7's sources and passes here.
The inline comments cover what is left:
- 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.
- The start marker leaking between draw passes.
- The classes that still crash with the same teardown signature on iOS 18.6.
- 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.
| // A visible tile can include the end of a quote without its start marker. | ||
| guard let top = blockquoteMarkingStorage else { return } |
There was a problem hiding this comment.
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 } | |||
There was a problem hiding this comment.
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.
| layout.draw(in: context, visibleRect: visibleRect) | ||
| layout.draw(in: context) | ||
| layout.draw(in: context, visibleRect: visibleRect) |
There was a problem hiding this comment.
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(_:).
| // Synchronous rendering must avoid the isolated-deinit back-deployment path. | ||
| nonisolated deinit {} |
There was a problem hiding this comment.
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:
LTXLabelon its own, throughCodeView.textView, and throughMarkdownTextViewteardownTableViewCellManagerthroughTableViewteardown
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
openclass in a main-actor-default module need the same line, which is worth saying in the comment. viewis now released on whichever thread drops the last reference to the attachment, instead of on the main actor.
| func testAttachmentReleaseInsideSynchronousTaskLocalScope() { | ||
| checkSynchronousRelease { LTXAttachment() } | ||
| } |
There was a problem hiding this comment.
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
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
TextBuilderinstances build quotes, code, diffs, and tables without force-unwrapping absent drawing callbacks.Validation with Swift 6.3.3:
xcodebuildwas invoked.git diff --checkpasses. This is focused package validation, not a full v0 app test.Remaining scope: