NSMutableAttributedString initialisation to prevent crash

We keep getting intermittent app crash from our customers and it is related to initialisation of NSMutableAttributedString.


Code:

let result = try NSMutableAttributedString(data: data, options: htmlOptions, documentAttributes: nil) <- Crash here

The above code is executed in cellForRowAt inside a TableViewCell and in some code also in viewDidLoad of a view.

data only contains short html text or sometimes even empty string "".

We also cannot reproduce the issue when testing with simulator and our test devices.


If I google this issue, it seems like there is no official solution that really worked.

Suggestions to run in main thread or run in background and then post in main thread seems to contradict the description in the documentation about NSMutableAttributedString.


The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.


Any recommendation on how to use NSMutableAttributedString properly without running into this crash?


Here is the crash logs:

Fatal Exception: NSInternalInconsistencyException

0 CoreFoundation 0x1b3a9c518 __exceptionPreprocess

1 libobjc.A.dylib 0x1b2c779f8 objc_exception_throw

2 CoreFoundation 0x1b39b6148 +[_CFXNotificationTokenRegistration keyCallbacks]

3 Foundation 0x1b44c5f5c -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:]

4 UIKitCore 0x1e000d9b0 _prepareForCAFlush

5 UIKitCore 0x1e003a674 _beforeCACommitHandler

6 CoreFoundation 0x1b3a2d89c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__

7 CoreFoundation 0x1b3a285c4 __CFRunLoopDoObservers

8 CoreFoundation 0x1b3a28b40 __CFRunLoopRun

9 CoreFoundation 0x1b3a28354 CFRunLoopRunSpecific

10 UIFoundation 0x1be08fc78 -[NSHTMLReader _loadUsingWebKit]

11 UIFoundation 0x1be092bfc -[NSHTMLReader attributedString]

12 UIFoundation 0x1be0deb88 _NSReadAttributedStringFromURLOrData

13 UIFoundation 0x1be092b38 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:]

14 MyApp 0x10140b140 @nonobjc NSMutableAttributedString.init(data:options:documentAttributes:) (<compiler-generated>)

Answered by DTS Engineer in 356326022

I really need the info that such a report contains

Someone else sent me an Apple crash report and that let me refresh my understanding of this issue. Sadly, this is a known bug in iOS (r. 23592459) that can potentially affect anyone who constructs an NSAttributedString from HTML.

There isn’t a good workaround for this other than to avoid this API altogether. My advice:

  • If you’re displaying large chunks of complex HTML, use a WKWebView.

  • If this HTML is highly constrained — perhaps you’re just using HTML as an easy way to transfer a constrained set of attributes, like bold and italics — create your own markup system that doesn’t relying on HTML. Or parse the HTML for just these attributes and use the result to create your attributed string.

  • Alternatively, consider using the Markdown support we added in macOS 12 and iOS 15. For the details, watch WWDC 2021 Session 10109 What’s new in Foundation.

IMPORTANT Foundation’s Markdown support is strongly integrated with SwiftUI but it’s not limited to SwiftUI, or even to Swift. It’s compatible with other UI frameworks and Objective-C.

I’m sorry I don’t have better news here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Did you test with failable try?

I’ve seen this issue before but I can’t remember the context. It looks like you have an Apple crash report for this handy. If so, please post the full report [1]. That’ll let me track down the context.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] If you have problems posting it, feel free to email me a copy.

Here you go. I suspect this is first time initialisation of NSMutableAttributedString which is very slow which blocks the Main Thread.



#0. Crashed: com.twitter.crashlytics.ios.exception

0 My App 0x1038c23a0 CLSProcessRecordAllThreads (CLSProcess.c:376)

1 My App 0x1038c2788 CLSProcessRecordAllThreads (CLSProcess.c:407)

2 My App 0x1038b189c CLSHandler (CLSHandler.m:26)

3 My App 0x1038c07c0 __CLSExceptionRecord_block_invoke (CLSException.mm:199)

4 libdispatch.dylib 0x22a9957d4 _dispatch_client_callout + 16

5 libdispatch.dylib 0x22a9765fc _dispatch_lane_barrier_sync_invoke_and_complete + 56

6 My App 0x1038c025c CLSExceptionRecord (CLSException.mm:206)

7 My App 0x1038c0090 CLSExceptionRecordNSException (CLSException.mm:102)

8 My App 0x1038bfcac CLSTerminateHandler() (CLSException.mm:259)

9 libc++abi.dylib 0x22a124838 std::__terminate(void (*)()) + 16

10 libc++abi.dylib 0x22a1248c4 std::terminate() + 84

11 libdispatch.dylib 0x22a9957e8 _dispatch_client_callout + 36

12 libdispatch.dylib 0x22a9759e4 _dispatch_main_queue_callback_4CF$VARIANT$armv81 + 1008

13 CoreFoundation 0x22aee5ec0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12

14 CoreFoundation 0x22aee0df8 __CFRunLoopRun + 1924

15 CoreFoundation 0x22aee0354 CFRunLoopRunSpecific + 436

16 GraphicsServices 0x22d0e079c GSEventRunModal + 104

17 UIKitCore 0x2574cbb68 UIApplicationMain + 212

18 My App 0x1026ae900 main (UnusableCardCell.swift:21)

19 libdyld.dylib 0x22a9a68e0 start + 4



--



Fatal Exception: NSInternalInconsistencyException

0 CoreFoundation 0x22af54518 __exceptionPreprocess

1 libobjc.A.dylib 0x22a12f9f8 objc_exception_throw

2 CoreFoundation 0x22ae6e148 +[_CFXNotificationTokenRegistration keyCallbacks]

3 Foundation 0x22b97df5c -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:]

4 UIKitCore 0x2574c59b0 _prepareForCAFlush

5 UIKitCore 0x2574f2674 _beforeCACommitHandler

6 CoreFoundation 0x22aee589c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__

7 CoreFoundation 0x22aee05c4 __CFRunLoopDoObservers

8 CoreFoundation 0x22aee0b40 __CFRunLoopRun

9 CoreFoundation 0x22aee0354 CFRunLoopRunSpecific

10 UIFoundation 0x235547c78 -[NSHTMLReader _loadUsingWebKit]

11 UIFoundation 0x23554abfc -[NSHTMLReader attributedString]

12 UIFoundation 0x235596b88 _NSReadAttributedStringFromURLOrData

13 UIFoundation 0x23554ab38 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:]

14 My App 0x1034cb140 @nonobjc NSMutableAttributedString.init(data:options:documentAttributes:) (<compiler-generated>)

15 My App 0x1034c6054 NSMutableAttributedString.__allocating_init(data:options:documentAttributes:) (HtmlHelper.swift)

16 My App 0x1034c623c String.convertHTMLtag(defaultFont:boldFont:) (HtmlHelper.swift:132)

17 My App 0x1034c7a24 static HtmlHelper.parseStringAndElements(from:) (HtmlHelper.swift:84)

18 My App 0x1033b5be4 HTMLTextView.updateContents() (HTMLTextView.swift:46)

19 My App 0x1033b51f8 HTMLTextView.htmlContent.didset (HTMLTextView.swift:28)

20 My App 0x1033b53ac HTMLTextView.htmlContent.setter (HTMLTextView.swift)

21 My App 0x1028756b8 AlertBarTableViewCell.importData(_:withStyle:) (AlertBarTableViewCell.swift:31)

22 My App 0x102d94c90 FooterAlertBarTableViewCell.importData(_:withStyle:) (FooterAlertBarTableViewCell.swift:27)

23 My App 0x1034ce9f8 AlertBarView.tableView(_:cellForRowAt:) (AlertBarView.swift:108)

24 My App 0x1034cf044 @objc AlertBarView.tableView(_:cellForRowAt:) (<compiler-generated>)

25 UIKitCore 0x2576e6a38 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]

26 UIKitCore 0x2576e6f38 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]

27 UIKitCore 0x2576b3740 -[UITableView _updateVisibleCellsNow:isRecursive:]

28 UIKitCore 0x2576d0a60 -[UITableView layoutSubviews]

29 UIKitCore 0x257953e54 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]

30 QuartzCore 0x22f3e21f0 -[CALayer layoutSublayers]

31 QuartzCore 0x22f3e7198 CA::Layer::layout_if_needed(CA::Transaction*)

32 QuartzCore 0x22f34a0a8 CA::Context::commit_transaction(CA::Transaction*)

33 QuartzCore 0x22f378108 CA::Transaction::commit()

34 UIKitCore 0x2574d6e80 __83-[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]_block_invoke_4

35 UIKitCore 0x2574d6140 -[UIApplication _performWithUICACommitStateSnapshotting:]

36 UIKitCore 0x2574d6d88 __83-[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]_block_invoke_2

37 UIKitCore 0x2579465d4 +[UIView(Animation) performWithoutAnimation:]

38 UIKitCore 0x2574d6b2c __83-[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]_block_invoke

39 UIKitCore 0x256d96318 -[_UICanvas _performActions:withOverrideSettings:]

40 UIKitCore 0x2574d6a3c -[UIApplication _createSnapshotContextForScene:withName:performLayoutWithSettings:]

41 UIKitCore 0x2574d86c0 __39-[UIApplication _saveSnapshotWithName:]_block_invoke

42 UIKitCore 0x2574d7778 -[UIApplication _beginSnapshotSessionForScene:withSnapshotBlock:]

43 UIKitCore 0x2574d8620 -[UIApplication _saveSnapshotWithName:]

44 UIKitCore 0x2574e2ff0 __125-[UIApplication _updateStateRestorationArchiveForBackgroundEvent:saveState:exitIfCouldNotRestoreState:updateSnapshot:canvas:]_block_invoke_2

45 libdispatch.dylib 0x22a994a38 _dispatch_call_block_and_release

46 libdispatch.dylib 0x22a9957d4 _dispatch_client_callout

47 libdispatch.dylib 0x22a9759e4 _dispatch_main_queue_callback_4CF$VARIANT$armv81

48 CoreFoundation 0x22aee5ec0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__

49 CoreFoundation 0x22aee0df8 __CFRunLoopRun

50 CoreFoundation 0x22aee0354 CFRunLoopRunSpecific

51 GraphicsServices 0x22d0e079c GSEventRunModal

52 UIKitCore 0x2574cbb68 UIApplicationMain

53 My App 0x1026ae900 main (UnusableCardCell.swift:21)

54 libdyld.dylib 0x22a9a68e0 start



#0. Crashed: com.twitter.crashlytics.ios.exception

0 My App 0x1038c23a0 CLSProcessRecordAllThreads (CLSProcess.c:376)

1 My App 0x1038c2788 CLSProcessRecordAllThreads (CLSProcess.c:407)

2 My App 0x1038b189c CLSHandler (CLSHandler.m:26)

3 My App 0x1038c07c0 __CLSExceptionRecord_block_invoke (CLSException.mm:199)

4 libdispatch.dylib 0x22a9957d4 _dispatch_client_callout + 16

5 libdispatch.dylib 0x22a9765fc _dispatch_lane_barrier_sync_invoke_and_complete + 56

6 My App 0x1038c025c CLSExceptionRecord (CLSException.mm:206)

7 My App 0x1038c0090 CLSExceptionRecordNSException (CLSException.mm:102)

8 My App 0x1038bfcac CLSTerminateHandler() (CLSException.mm:259)

9 libc++abi.dylib 0x22a124838 std::__terminate(void (*)()) + 16

10 libc++abi.dylib 0x22a1248c4 std::terminate() + 84

11 libdispatch.dylib 0x22a9957e8 _dispatch_client_callout + 36

12 libdispatch.dylib 0x22a9759e4 _dispatch_main_queue_callback_4CF$VARIANT$armv81 + 1008

13 CoreFoundation 0x22aee5ec0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12

14 CoreFoundation 0x22aee0df8 __CFRunLoopRun + 1924

15 CoreFoundation 0x22aee0354 CFRunLoopRunSpecific + 436

16 GraphicsServices 0x22d0e079c GSEventRunModal + 104

17 UIKitCore 0x2574cbb68 UIApplicationMain + 212

18 My App 0x1026ae900 main (UnusableCardCell.swift:21)

19 libdyld.dylib 0x22a9a68e0 start + 4



#1. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab751f8 _pthread_wqthread + 532

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#2. com.apple.uikit.eventfetch-thread

0 libsystem_kernel.dylib 0x22aae70f4 mach_msg_trap + 8

1 libsystem_kernel.dylib 0x22aae65a0 mach_msg + 72

2 CoreFoundation 0x22aee5cb4 __CFRunLoopServiceMachPort + 236

3 CoreFoundation 0x22aee0bc4 __CFRunLoopRun + 1360

4 CoreFoundation 0x22aee0354 CFRunLoopRunSpecific + 436

5 Foundation 0x22b8adfcc -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 300

6 Foundation 0x22b8ade5c -[NSRunLoop(NSRunLoop) runUntilDate:] + 96

7 UIKitCore 0x2575b1540 -[UIEventFetcher threadMain] + 136

8 Foundation 0x22b9da6e4 __NSThread__start__ + 984

9 libsystem_pthread.dylib 0x22ab742c0 _pthread_body + 128

10 libsystem_pthread.dylib 0x22ab74220 _pthread_start + 44

11 libsystem_pthread.dylib 0x22ab77cdc thread_start + 4



#3. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab751f8 _pthread_wqthread + 532

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#4. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab75138 _pthread_wqthread + 340

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#5. com.twitter.crashlytics.ios.MachExceptionServer

0 libsystem_kernel.dylib 0x22aae70f4 mach_msg_trap + 8

1 libsystem_kernel.dylib 0x22aae65a0 mach_msg + 72

2 My App 0x1038ac520 CLSMachExceptionServer (CLSMachException.c:180)

3 libsystem_pthread.dylib 0x22ab742c0 _pthread_body + 128

4 libsystem_pthread.dylib 0x22ab74220 _pthread_start + 44

5 libsystem_pthread.dylib 0x22ab77cdc thread_start + 4



#6. Thread

0 libsystem_pthread.dylib 0x22ab77cd0 start_wqthread + 190



#7. com.apple.NSURLConnectionLoader

0 libsystem_kernel.dylib 0x22aae70f4 mach_msg_trap + 8

1 libsystem_kernel.dylib 0x22aae65a0 mach_msg + 72

2 CoreFoundation 0x22aee5cb4 __CFRunLoopServiceMachPort + 236

3 CoreFoundation 0x22aee0bc4 __CFRunLoopRun + 1360

4 CoreFoundation 0x22aee0354 CFRunLoopRunSpecific + 436

5 CFNetwork 0x22b4f974c -[__CoreSchedulingSetRunnable runForever] + 216

6 Foundation 0x22b9da6e4 __NSThread__start__ + 984

7 libsystem_pthread.dylib 0x22ab742c0 _pthread_body + 128

8 libsystem_pthread.dylib 0x22ab74220 _pthread_start + 44

9 libsystem_pthread.dylib 0x22ab77cdc thread_start + 4



#8. JavaScriptCore bmalloc scavenger

0 libsystem_kernel.dylib 0x22aaf1ee4 __psynch_cvwait + 8

1 libsystem_pthread.dylib 0x22ab714a4 _pthread_cond_wait$VARIANT$armv81 + 628

2 libc++.1.dylib 0x22a0c9090 std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 24

3 JavaScriptCore 0x2320b6238 void std::__1::condition_variable_any::wait<std::__1::unique_lock<bmalloc::Mutex> >(std::__1::unique_lock<bmalloc::Mutex>&) + 108

4 JavaScriptCore 0x2320ba22c bmalloc::Scavenger::threadRunLoop() + 176

5 JavaScriptCore 0x2320b99a4 bmalloc::Scavenger::Scavenger(std::__1::lock_guard<bmalloc::Mutex>&) + 10

6 JavaScriptCore 0x2320bb3e4 std::__1::__thread_specific_ptr<std::__1::__thread_struct>::set_pointer(std::__1::__thread_struct*) + 38

7 libsystem_pthread.dylib 0x22ab742c0 _pthread_body + 128

8 libsystem_pthread.dylib 0x22ab74220 _pthread_start + 44

9 libsystem_pthread.dylib 0x22ab77cdc thread_start + 4



#9. WebThread

0 libsystem_kernel.dylib 0x22aaf1ee4 __psynch_cvwait + 8

1 libsystem_pthread.dylib 0x22ab714a4 _pthread_cond_wait$VARIANT$armv81 + 628

2 JavaScriptCore 0x23207cce4 ***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 80

3 JavaScriptCore 0x23206396c ***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 2004

4 JavaScriptCore 0x232053c0c ***::LockAlgorithm<unsigned char, (unsigned char)1, (unsigned char)2, ***::EmptyLockHooks<unsigned char> >::lockSlow(***::Atomic<unsigned char>&) + 232

5 WebCore 0x233b37b28 _WebThreadLock() + 264

6 WebCore 0x233b382b4 SendDelegateMessage(NSInvocation*) + 852

7 CoreFoundation 0x22af59a5c ___forwarding___ + 636

8 CoreFoundation 0x22af5b9fc _CF_forwarding_prep_0 + 92

9 WebKitLegacy 0x2351fa658 WebFrameLoaderClient::dispatchDidReachLayoutMilestone(***::OptionSet<WebCore::LayoutMilestone>) + 160

10 WebCore 0x2348c4354 WebCore::FrameView::fireLayoutRelatedMilestonesIfNeeded() + 392

11 WebCore 0x2348c4ed8 WebCore::FrameView::performPostLayoutTasks() + 200

12 WebCore 0x2348ce500 WebCore::FrameViewLayoutContext::runOrScheduleAsynchronousTasks() + 144

13 WebCore 0x2348ce15c WebCore::FrameViewLayoutContext::layout() + 1512

14 WebCore 0x23443c660 WebCore::Document::updateLayoutIfDimensionsOutOfDate(WebCore::Element&, WebCore::DimensionsCheck) + 1012

15 WebCore 0x234471570 WebCore::Element::clientHeight() + 40

16 WebCore 0x233ad4784 WebCore::jsElementClientHeight(JSC::ExecState*, long long, JSC::PropertyName) + 16

17 JavaScriptCore 0x232822110 llint_slow_path_get_by_id + 3840

18 JavaScriptCore 0x232240880 llint_entry + 30592

19 JavaScriptCore 0x23224d3c4 llint_entry + 82628

20 JavaScriptCore 0x23224d3c4 llint_entry + 82628

21 JavaScriptCore 0x23224d3c4 llint_entry + 82628

22 JavaScriptCore 0x23224d3c4 llint_entry + 82628

23 JavaScriptCore 0x23224d3c4 llint_entry + 82628

24 JavaScriptCore 0x232238e6c vmEntryToJavaScript + 268

25 JavaScriptCore 0x232768c78 JSC::Interpreter::executeProgram(JSC::SourceCode const&, JSC::ExecState*, JSC::JSObject*) + 9816

26 JavaScriptCore 0x232972020 JSC::evaluate(JSC::ExecState*, JSC::SourceCode const&, JSC::JSValue, ***::NakedPtr<JSC::Exception>&) + 320

27 WebCore 0x23422a9fc WebCore::JSExecState::profiledEvaluate(JSC::ExecState*, JSC::ProfilingReason, JSC::SourceCode const&, JSC::JSValue, ***::NakedPtr<JSC::Exception>&) + 108

28 WebCore 0x23422a838 WebCore::ScriptController::evaluateInWorld(WebCore::ScriptSourceCode const&, WebCore::DOMWrapperWorld&, WebCore::ExceptionDetails*) + 232

29 WebCore 0x2344cc958 WebCore::ScriptElement::executeClassicScript(WebCore::ScriptSourceCode const&) + 640

30 WebCore 0x23449464c WebCore::LoadableClassicScript::execute(WebCore::ScriptElement&) + 140

31 WebCore 0x2344ccbfc WebCore::ScriptElement::executeScriptAndDispatchEvent(WebCore::LoadableScript&) + 224

32 WebCore 0x2347156f0 WebCore::HTMLScriptRunner::executeParsingBlockingScripts() + 344

33 WebCore 0x234708d10 non-virtual thunk to WebCore::HTMLDocumentParser::notifyFinished(WebCore::PendingScript&) + 84

34 WebCore 0x2344b0be0 WebCore::PendingScript::notifyFinished(WebCore::LoadableScript&) + 52

35 WebCore 0x23449455c WebCore::LoadableScript::notifyClientFinished() + 312

36 WebCore 0x2344943d8 WebCore::LoadableClassicScript::notifyFinished(WebCore::CachedResource&) + 1448

37 WebCore 0x234859ffc WebCore::CachedResource::checkNotify() + 288

38 WebCore 0x23482b888 WebCore::SubresourceLoader::didFinishLoading(WebCore::NetworkLoadMetrics const&) + 672

39 WebCore 0x234822f34 WebCore::ResourceLoader::didFinishLoading(WebCore::ResourceHandle*) + 152

40 WebCore 0x233c07574 ***::Function<void ()>::CallableWrapper<-[WebCoreResourceHandleAsOperationQueueDelegate connectionDidFinishLoading:]::$_8>::call() + 80

41 JavaScriptCore 0x232054aa4 ***::dispatchFunctionsFromMainThread() + 288

42 Foundation 0x22b9da8d4 __NSThreadPerformPerform + 336

43 CoreFoundation 0x22aee62bc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24

44 CoreFoundation 0x22aee623c __CFRunLoopDoSource0 + 88

45 CoreFoundation 0x22aee5b24 __CFRunLoopDoSources0 + 176

46 CoreFoundation 0x22aee0a60 __CFRunLoopRun + 1004

47 CoreFoundation 0x22aee0354 CFRunLoopRunSpecific + 436

48 WebCore 0x233b3a480 RunWebThread(void*) + 600

49 libsystem_pthread.dylib 0x22ab742c0 _pthread_body + 128

50 libsystem_pthread.dylib 0x22ab74220 _pthread_start + 44

51 libsystem_pthread.dylib 0x22ab77cdc thread_start + 4



#10. RLMRealm notification listener

0 libsystem_kernel.dylib 0x22aaf38f4 kevent + 8

1 Realm 0x104534ddc realm::_impl::ExternalCommitHelper::listen() (external_commit_helper.cpp:203)

2 Realm 0x104535e60 std::__1::__async_func<realm::_impl::ExternalCommitHelper::ExternalCommitHelper(realm::_impl::RealmCoordinator&)::$_0>::operator()() (future:2323)

3 Realm 0x104535de4 std::__1::__async_assoc_state<void, std::__1::__async_func<realm::_impl::ExternalCommitHelper::ExternalCommitHelper(realm::_impl::RealmCoordinator&)::$_0> >::__execute() (future:1041)

4 Realm 0x104535fa4 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (std::__1::__async_assoc_state<void, std::__1::__async_func<realm::_impl::ExternalCommitHelper::ExternalCommitHelper(realm::_impl::RealmCoordinator&)::$_0> >::*)(), std::__1::__async_assoc_state<void, std::__1::__async_func<realm::_impl::ExternalCommitHelper::ExternalCommitHelper(realm::_impl::RealmCoordinator&)::$_0> >*> >(void*) (memory:2595)

5 libsystem_pthread.dylib 0x22ab742c0 _pthread_body + 128

6 libsystem_pthread.dylib 0x22ab74220 _pthread_start + 44

7 libsystem_pthread.dylib 0x22ab77cdc thread_start + 4



#11. com.google.fira.worker

0 libsystem_kernel.dylib 0x22aaf29d4 __ulock_wait + 8

1 libdispatch.dylib 0x22a964c20 _dispatch_ulock_wait + 56

2 libdispatch.dylib 0x22a964d58 _dispatch_thread_event_wait_slow$VARIANT$armv81 + 48

3 libdispatch.dylib 0x22a9768a8 __DISPATCH_WAIT_FOR_QUEUE__ + 336

4 libdispatch.dylib 0x22a9764a8 _dispatch_sync_f_slow + 140

5 My App 0x1038f77b8 -[APMIdentity identifierForVendor] + 4386322360

6 My App 0x103902e98 -[APMMeasurement createRawEventMetadataWithUserAttributes:] + 4386369176

7 My App 0x103900070 __42-[APMMeasurement writeEventOnWorkerQueue:]_block_invoke.1066 + 4386357360

8 My App 0x103924eb0 -[APMSqliteStore performTransaction:] + 4386508464

9 My App 0x1038ff064 -[APMMeasurement writeEventOnWorkerQueue:] + 4386353252

10 My App 0x1038fe970 -[APMMeasurement handleEventOnWorkerQueue:] + 4386351472

11 My App 0x103911f60 __51-[APMScheduler scheduleOnWorkerQueueBlockID:block:]_block_invoke + 4386430816

12 libdispatch.dylib 0x22a994a38 _dispatch_call_block_and_release + 24

13 libdispatch.dylib 0x22a9957d4 _dispatch_client_callout + 16

14 libdispatch.dylib 0x22a970dec _dispatch_lane_serial_drain$VARIANT$armv81 + 548

15 libdispatch.dylib 0x22a97192c _dispatch_lane_invoke$VARIANT$armv81 + 408

16 libdispatch.dylib 0x22a979e08 _dispatch_workloop_worker_thread + 584

17 libsystem_pthread.dylib 0x22ab75114 _pthread_wqthread + 304

18 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#12. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab75138 _pthread_wqthread + 340

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#13. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab75138 _pthread_wqthread + 340

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#14. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab75138 _pthread_wqthread + 340

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#15. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab75138 _pthread_wqthread + 340

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4



#16. Thread

0 libsystem_kernel.dylib 0x22aaf2b74 __workq_kernreturn + 8

1 libsystem_pthread.dylib 0x22ab751f8 _pthread_wqthread + 532

2 libsystem_pthread.dylib 0x22ab77cd4 start_wqthread + 4

Alas, that’s not an Apple crash report, and I really need the info that such a report contains [1].

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] Specifically, this report is missing the Thread State and Binary Images section that is necessary for investigating problems like this. For more background on how to implement your own crash reporter, or evaluate someone else’s crash reporter, check out my Implementing Your Own Crash Reporter.

Accepted Answer

I really need the info that such a report contains

Someone else sent me an Apple crash report and that let me refresh my understanding of this issue. Sadly, this is a known bug in iOS (r. 23592459) that can potentially affect anyone who constructs an NSAttributedString from HTML.

There isn’t a good workaround for this other than to avoid this API altogether. My advice:

  • If you’re displaying large chunks of complex HTML, use a WKWebView.

  • If this HTML is highly constrained — perhaps you’re just using HTML as an easy way to transfer a constrained set of attributes, like bold and italics — create your own markup system that doesn’t relying on HTML. Or parse the HTML for just these attributes and use the result to create your attributed string.

  • Alternatively, consider using the Markdown support we added in macOS 12 and iOS 15. For the details, watch WWDC 2021 Session 10109 What’s new in Foundation.

IMPORTANT Foundation’s Markdown support is strongly integrated with SwiftUI but it’s not limited to SwiftUI, or even to Swift. It’s compatible with other UI frameworks and Objective-C.

I’m sorry I don’t have better news here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you for the confirmation of the issue.

May I know the "known bug" about it? Is this the race condition mentioned here: http://www.openradar.me/34021573

what is iOS (r. 23592459) in previous thread. is it ios 12?

Loilee wrote:

Is this the race condition mentioned here

No. It seems that the bug you mentioned (r. 34021573) is a separate issue, one that was fixed in iOS 11.3

sanjaykbharadwaz wrote:

what is iOS (r. 23592459)

Sorry about the confusion here. You should that part of my post as two separate points:

  • This is a known bug in iOS.

  • The bug number tracking this is 23592459.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
I am having the same issue. What was the outcome of this issue?
I am getting same issue in iOS 14 beta version. It's keep crashing all the time.
Any solution?

What was the outcome of this issue?

Things have not change since my earlier response. The bug I mentioned (r. 23592459) remains unfixed.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
I faced a similar issue after enabled DarkMode - yes we've only enabled it recently. Turned out when the App went to background it was trying to take a snapshot of the top view, which I suspect is required by DarkMode, and will layout the views again, hence calling the NSAttributedString initWithData in my cell constructing method. I could see that this crash happened during App is backgrounded in Crashlytics, even tho it's still on main thread. I could also reproduce it easily once I figured it out.

The fix is pretty straightforward: to use a lazy var to init that attributedStr to prevent it from being called again when the App goes to background.
These 2 methods :

"data(from:documentAttributes:)" and "init(data:options:documentAttributes:)"

must be executed on main thread otherwise you have a crash or sometimes random data are inserted in you result (Data or NSAttributedString) (random bug)
For macOS too (not only for ios as the apple employee wrongly said).
The documentation said execution on main thread must be done for documenType .html.
The execution must be done on main thread for plaintext, rtf and html (all documentType) not only html as it is wrongly written.

I spent enough time on that issue.

This is a bug (10.15.7 catalina, xcode 12.2)

is this issue fixed now or still happened ?

This is still an issue.

Having said that, in iOS 15 we’ve added Markdown support, which addresses the second (“highly constrained”) use case I described in my answer.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I am preparing a static library that does not use NSAttributedString(data:options:documentAttributes) API at all. Unfortunately, my client, who is using this static library sees a crash in their app wherever they have used this API and this happens only after my static library is added to the project.

When I create a sample project which uses this API and adds the static library discussed above, I see no crash.

Note: My client is using this API in the main thread.

Summary:

My client's app with NSAttributedString(data:options:documentAttributes) API code = No crash

My client's app with NSAttributedString(data:options:documentAttributes) API code + My static library = crash

Build settings of my static library delivered to the client:

Configuration: Release
Apple Clang - Code Generation > Optimization Level: Fastest, Smallest [-Os]
Swift compiler - Code Generation > Optimization Level: Optimize for Speed [-O]

@eskimo: Do you have a hint of what could be going wrong here?

NSMutableAttributedString initialisation to prevent crash
 
 
Q