From 9324c2dd86c26426909f66e0427e2caa4a082682 Mon Sep 17 00:00:00 2001 From: Gary Tokman Date: Mon, 19 Oct 2026 12:26:42 -0400 Subject: [PATCH 1/2] fix: avoid isolated teardown crashes in synchronous rendering --- .../MarkdownTextBuilder/BlockProcessor.swift | 3 + .../ReusableViewProvider.swift | 3 + .../MarkdownTextBuilder/TextBuilder.swift | 3 + .../RenderingLifetimeTests.swift | 65 +++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/Sources/MarkdownView/MarkdownTextBuilder/BlockProcessor.swift b/Sources/MarkdownView/MarkdownTextBuilder/BlockProcessor.swift index 95ba4ee..0de16e5 100644 --- a/Sources/MarkdownView/MarkdownTextBuilder/BlockProcessor.swift +++ b/Sources/MarkdownView/MarkdownTextBuilder/BlockProcessor.swift @@ -15,6 +15,9 @@ import MarkdownParser // MARK: - BlockProcessor final class BlockProcessor { + // Synchronous rendering must avoid the isolated-deinit back-deployment path. + nonisolated deinit {} + private let theme: MarkdownTheme private let viewProvider: ReusableViewProvider private let context: MarkdownTextView.PreprocessedContent diff --git a/Sources/MarkdownView/MarkdownTextBuilder/ReusableViewProvider.swift b/Sources/MarkdownView/MarkdownTextBuilder/ReusableViewProvider.swift index 3d3b3ef..f606b2b 100644 --- a/Sources/MarkdownView/MarkdownTextBuilder/ReusableViewProvider.swift +++ b/Sources/MarkdownView/MarkdownTextBuilder/ReusableViewProvider.swift @@ -51,6 +51,9 @@ private class ObjectPool { } public final class ReusableViewProvider { + // Synchronous rendering must avoid the isolated-deinit back-deployment path. + nonisolated deinit {} + private let codeViewPool: ObjectPool = .init { CodeView(frame: .zero) } diff --git a/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift b/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift index 985902c..02ccf5b 100644 --- a/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift +++ b/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift @@ -13,6 +13,9 @@ import MarkdownParser #endif final class TextBuilder { + // Synchronous rendering must avoid the isolated-deinit back-deployment path. + nonisolated deinit {} + private let nodes: [MarkdownBlockNode] private let viewProvider: ReusableViewProvider private var theme: MarkdownTheme = .default diff --git a/Tests/MarkdownViewTests/RenderingLifetimeTests.swift b/Tests/MarkdownViewTests/RenderingLifetimeTests.swift index 6a71558..ff8e4d4 100644 --- a/Tests/MarkdownViewTests/RenderingLifetimeTests.swift +++ b/Tests/MarkdownViewTests/RenderingLifetimeTests.swift @@ -30,6 +30,71 @@ final class RenderingLifetimeTests: XCTestCase { } } + func testBlockProcessorAndOwnedCacheReleaseInsideSynchronousTaskLocalScope() { + let theme = MarkdownTheme.default + let context = MarkdownTextView.PreprocessedContent(markdown: "**Hello** `world`", theme: theme) + let viewProvider = ReusableViewProvider() + checkSynchronousRelease { + let processor = BlockProcessor( + theme: theme, + viewProvider: viewProvider, + context: context, + layoutWidth: 320, + thematicBreakDrawing: nil, + codeDrawing: nil, + diffDrawing: nil, + tableDrawing: nil, + blockquoteMarking: nil, + blockquoteDrawing: nil + ) + let text = processor.processParagraph(contents: [.text("Hello world")]) + XCTAssertTrue(text.string.contains("Hello world")) + return processor + } + } + + func testTextBuilderReleaseInsideSynchronousTaskLocalScope() { + let context = MarkdownTextView.PreprocessedContent(markdown: "Hello", theme: .default) + let viewProvider = ReusableViewProvider() + checkSynchronousRelease { + TextBuilder(nodes: context.blocks, context: context, viewProvider: viewProvider) + } + } + + func testTextBuilderAndOwnedProviderReleaseAfterSynchronousRender() { + let context = MarkdownTextView.PreprocessedContent(markdown: "**Hello** `world`", theme: .default) + checkSynchronousRelease { + let builder = TextBuilder( + nodes: context.blocks, + context: context, + viewProvider: ReusableViewProvider() + ) + let result = builder.build() + XCTAssertTrue(result.document.string.contains("Hello world")) + return builder + } + } + + func testTextBuilderAndOwnedProviderReleaseAfterSynchronousIncrementalRender() { + let context = MarkdownTextView.PreprocessedContent(markdown: "**Hello** `world`", theme: .default) + checkSynchronousRelease { + let builder = TextBuilder( + nodes: context.blocks, + context: context, + viewProvider: ReusableViewProvider() + ) + let result = builder.buildIncremental(changes: [.rebuild(newIndex: 0)], cachedSegments: []) + XCTAssertTrue(result.document.string.contains("Hello world")) + return builder + } + } + + func testReusableViewProviderReleaseInsideSynchronousTaskLocalScope() { + checkSynchronousRelease { + ReusableViewProvider() + } + } + // Keep this synchronous to match UIKit layout callbacks outside a Swift task. private func checkSynchronousRelease(_ makeObject: () -> T) { for _ in 0 ..< 100 { From f1d72c63bb14c1c19200bf15797ab0b6b5831d50 Mon Sep 17 00:00:00 2001 From: Gary Tokman Date: Mon, 19 Oct 2026 12:54:04 -0400 Subject: [PATCH 2/2] test: use default-enabled grammars in highlighting fixtures --- Tests/MarkdownViewTests/DiffViewTests.swift | 8 ++++---- Tests/MarkdownViewTests/HighlighterTests.swift | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Tests/MarkdownViewTests/DiffViewTests.swift b/Tests/MarkdownViewTests/DiffViewTests.swift index fc67562..152c672 100644 --- a/Tests/MarkdownViewTests/DiffViewTests.swift +++ b/Tests/MarkdownViewTests/DiffViewTests.swift @@ -121,10 +121,10 @@ final class DiffViewTests: XCTestCase { func testSingleTokenFenceAutoDetectsUnifiedDiffAndUsesInnerLanguage() { let content = makeContent( from: """ - ```swift + ```javascript @@ -1 +1 @@ - -let title = "Design Engineer" - +let title = "Designer" + -const title = "Design Engineer"; + +const title = "Designer"; ``` """ ) @@ -136,7 +136,7 @@ final class DiffViewTests: XCTestCase { return XCTFail("Expected auto-detected diff render block") } - XCTAssertEqual(renderBlock.language, "swift") + XCTAssertEqual(renderBlock.language, "javascript") XCTAssertEqual(renderBlock.rows[1].kind, .removed) XCTAssertFalse(renderBlock.rows[1].syntaxHighlights.isEmpty) } diff --git a/Tests/MarkdownViewTests/HighlighterTests.swift b/Tests/MarkdownViewTests/HighlighterTests.swift index 0c9896b..1377186 100644 --- a/Tests/MarkdownViewTests/HighlighterTests.swift +++ b/Tests/MarkdownViewTests/HighlighterTests.swift @@ -46,21 +46,21 @@ final class HighlighterTests: XCTestCase { func testHighlightReturnsMap() { let highlighter = CodeHighlighter.current - let code = "def greet(name):\n return f\"Hello, {name}!\"" - let map = highlighter.highlight(key: nil, content: code, language: "python") - // Should produce some color ranges for Python syntax - XCTAssertFalse(map.isEmpty, "Highlight map should contain color entries for Python code") + let code = "function greet(name) { return `Hello, ${name}!`; }" + let map = highlighter.highlight(key: nil, content: code, language: "javascript") + XCTAssertFalse(map.isEmpty, "Highlight map should contain color entries for JavaScript code") } func testHighlightCacheHit() { let highlighter = CodeHighlighter.current - let code = "def cache_hit():\n return 42" - let key = highlighter.key(for: code, language: "python") + let code = "function cacheHit() { return 42; }" + let key = highlighter.key(for: code, language: "javascript") - let map1 = highlighter.highlight(key: key, content: code, language: "python") - let map2 = highlighter.highlight(key: key, content: code, language: "python") + let map1 = highlighter.highlight(key: key, content: code, language: "javascript") + let map2 = highlighter.highlight(key: key, content: code, language: "javascript") // Both should return the same ranges (from cache on second call) + XCTAssertFalse(map1.isEmpty, "Cache test must exercise highlighted content") XCTAssertEqual(map1.count, map2.count) for (range, _) in map1 { XCTAssertNotNil(map2[range], "Cached map should contain same ranges")