Keyboard crashes during typing and updating selection on TextInput

Hello,

We are using TextField with a TextEditingController for controlling newline cursor position and textfield selection updates.

While using this setup we are seeing our Flutter iOS app causes a user's entire iOS device (iPhone or iPad) to freeze and the keyboard becomes unusable in any application even outside of ours. The issue requires a hardware restart to clear it.

We have already verified with Flutter that this seems to be an iOS platform specific bug, as while debugging we havent seen any deadlocks on flutter core code.

Some basic actions which can repro this crash are follows:

  1. If we type in lot of newlines and text in the app, and switch between text and emoji keyboard, after sometime the keyboard freezes.
  2. If on an iPad sometime switching between a physical vs software keyboard while in the app, causes this freeze as well.
  3. While typing in our app for some time and then switching over to some other app like spotlight, we see the keyboard freezing as well.

Attached are the crash logs for all the above scenarios, and they all seem to point to some area of keyboard process:

Auto-Correction - ON on Keyboard

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   TextInputCore                 	0x00000001cdee8680 KB::String::iterator::operator--+ 747136 () + 132
1   TextInputCore                 	0x00000001cdee8614 KB::String::iterator::operator--+ 747028 () + 24
2   TextInputCore                 	0x00000001cdee8c38 KB::String::find_last_of+ 748600 (KB::String const&, KB::String::iterator const&) const + 184
3   TextInputCore                 	0x00000001cdeaa914 KB::String::find_last_of+ 493844 (KB::String const&) const + 96
4   TextInputCore                 	0x00000001cdea8d30 TIInputManager::should_generate_candidates+ 486704 (KB::String const&, KB::String const&) const + 152

Predictive - ON on Keyboard

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   TextInputCore                 	       0x1ad3104e4 KB::String::iterator::operator--+ 62692 () + 132
1   TextInputCore                 	       0x1ad310478 KB::String::iterator::operator--+ 62584 () + 24
2   TextInputCore                 	       0x1ad31160c KB::String::last+ 67084 () const + 84
3   TextInputCore                 	       0x1ad3afcdc TIInputManager::filter_raw_candidates+ 715996 (KB::CandidateCollection const&, KB::String const&, KB::String const&, KB::LookupType, __CFString*) const + 204
4   TextInputCore                 	       0x1ad3af1bc TIInputManager::lookup_static_dynamic_candidates(KB::CandidateCollection&, KB::LookupType, KB::String const&, void + 713148 (KB::CandidateCollection&, __CFString*) block_pointer, unsigned int) const + 304
5   TextInputCore                 	       0x1ad3ae608 TIInputManager::perform_lookup(KB::LookupType, unsigned int, void + 710152 (KB::CandidateCollection&, __CFString*) block_pointer) + 1196
6   TextInputCore                 	       0x1ad3adf7c TIInputManager::lookup+ 708476 (KB::LookupType, unsigned int, TIShiftContext) + 340
7   TextInputCore                 	       0x1ad410a6c -[TIKeyboardInputManager predictionCandidates:predictionType:] + 300
8   TextInputCore                 	       0x1ad40f0b8 -[TIKeyboardInputManager autocorrectionListForEmptyInputWithDesiredCandidateCount:] + 820
9   TextInputCore                 	       0x1ad429528 -[TIKeyboardInputManager+ 1213736 (ResultSpecializations) autocorrectionListWithCandidateCount:] + 712
10  TextInputCore                 	       0x1ad421068 -[TIKeyboardInputManager generateAutocorrectionsWithKeyboardState:candidateRange:candidateHandler:] + 564
11  TextInputCore                 	       0x1ad364bf8 -[TIKeyboardInputManagerWrapper generateAutocorrectionsWithKeyboardState:candidateRange:requestToken:completionHandler:] + 656
12  CoreFoundation                	       0x180f2d304 __invoking___ + 148

Auto-Correct and Predictive OFF on Keyboard

This time it freezes the keyboard when switch to some other app.

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: FRONTBOARD; [2343432205]
<RBSTerminateContext| domain:10 code:0x8BADF00D explanation:scene-update watchdog transgression: application<com.apple.Spotlight>:265 exhausted real (wall clock) time allowance of 5.00 seconds; Spotlight is unresponsive
ProcessVisibility: Foreground
ProcessState: Running
WatchdogEvent: scene-update
WatchdogVisibility: Foreground
WatchdogCPUStatistics: (
"Elapsed total CPU time (seconds): 11.090 (user 8.890, system 2.200), 33% CPU",
"Elapsed application CPU time (seconds): 0.002, 0% CPU"
) reportType:CrashLog maxTerminationResistance:Interactive>

Triggered by Thread:  0

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib        	       0x1b80deff0 __psynch_cvwait + 8
1   libsystem_pthread.dylib       	       0x1f1ad42a0 _pthread_cond_wait + 1272
2   Foundation                    	       0x182eb358c -[NSCondition waitUntilDate:] + 148
3   Foundation                    	       0x182ea4fd0 -[NSConditionLock lockWhenCondition:beforeDate:] + 104
4   UIKitCore                     	       0x184054684 -[UIKeyboardTaskQueue lockWhenReadyForMainThread] + 564
5   UIKitCore                     	       0x183e19f4c -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 68
6   UIKitCore                     	       0x184803254 -[UIKeyboardImpl prepareForSelectionChange] + 152
7   UIKitCore                     	       0x184183e5c -[UIKeyboardImpl selectionWillChange:] + 88
8   UIKitCore                     	       0x1841860a8 -[UITextInputController beginSelectionChange] + 48
9   UIKitCore                     	       0x184187604 -[UIFieldEditor beginSelectionChange] + 72
10  UIKitCore                     	       0x184c22498 -[UITextField beginSelectionChange] + 52
11  UIKitCore                     	       0x184068580 -[UITextInteractionAssistant+ 6026624 (UITextInteractionAssistant_Internal) selectAll:] + 68
12  UIKitCore                     	       0x183f96be0 -[UITextField selectAll:] + 84
13  SpotlightUIInternal           	       0x1ab3733d0 -[SPUITextField selectAll:] + 52

We suspect something with keyboard process which it doesnt like if we touch text selection and cursor position in our app. Any advise on where we can look into fixing this ?

Also please find complete crash logs for the above snippets.

I'm seeing a very similar thing happening when running our app on an M1 Mac or using Catalyst: All you need is a WKWebView with a textarea element. When you use the keyboard arrow keys to quickly move up and down a few times, you get the beachball of death and a similar stack trace:

 17 main + 64 (AppDelegate.swift:17,7 in MyApp + 3103436) [0x1048a9acc]
 17 UIApplicationMain + 164 (UIKitCore + 11036) [0x1c4e95b1c]
 17 UINSApplicationMain + 1280 (UIKitMacHelper + 19992) [0x1b4f88e18]
 17 _NSApplicationMainWithInfoDictionary + 24 (AppKit + 3091152) [0x1a4afaad0]
 17 NSApplicationMain + 1064 (AppKit + 14456) [0x1a480b878]
 17 -[NSApplication run] + 596 (AppKit + 204364) [0x1a4839e4c]
 17 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1292 (AppKit + 262144) [0x1a4848000]
 17 _DPSNextEvent + 836 (AppKit + 267872) [0x1a4849660]
 17 _BlockUntilNextEventMatchingListInModeWithFilter + 72 (HIToolbox + 194164) [0x1a9f7c674]
 17 ReceiveNextEventCommon + 552 (HIToolbox + 194740) [0x1a9f7c8b4]
 17 RunCurrentEventLoopInMode + 292 (HIToolbox + 195392) [0x1a9f7cb40]
 17 CFRunLoopRunSpecific + 600 (CoreFoundation + 528808) [0x1a20571a8]
 17 __CFRunLoopRun + 820 (CoreFoundation + 531540) [0x1a2057c54]
 17 __CFRunLoopDoSources0 + 268 (CoreFoundation + 537292) [0x1a20592cc] 
 17 __CFRunLoopDoSource0 + 208 (CoreFoundation + 538080) [0x1a20595e0]
 17 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (CoreFoundation + 538260) [0x1a2059694]
 17 WTF::RunLoop::performWork(void*) + 36 (JavaScriptCore + 16183380) [0x1d2eb9054]
 17 WTF::RunLoop::performWork() + 484 (JavaScriptCore + 16179332) [0x1d2eb8084]
 17 WTF::Detail::CallableWrapper<IPC::Connection::SyncMessageState::processIncomingMessage(IPC::Connection&, std::__1::unique_ptr<IPC::Decoder, std::__1::default_delete<IPC::Decoder> >&)::$_1, void>::call() + 504 (WebKit + 392372) [0x1d379bcb4]
 17 IPC::Connection::dispatchMessage(std::__1::unique_ptr<IPC::Decoder, std::__1::default_delete<IPC::Decoder> >) + 760 (WebKit + 388268) [0x1d379acac]
 17 WebKit::WebProcessProxy::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::__1::unique_ptr<IPC::Encoder, std::__1::default_delete<IPC::Encoder> >&) + 44 (WebKit + 3635628) [0x1d3ab39ac]
 17 IPC::MessageReceiverMap::dispatchSyncMessage(IPC::Connection&, IPC::Decoder&, std::__1::unique_ptr<IPC::Encoder, std::__1::default_delete<IPC::Encoder> >&) + 132 (WebKit + 508152) [0x1d37b80f8]
 17 WebKit::WebPageProxy::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::__1::unique_ptr<IPC::Encoder, std::__1::default_delete<IPC::Encoder> >&) + 7644 (WebKit + 6020144) [0x1d3cf9c30]
 17 WebKit::WebPageProxy::interpretKeyEvent(WebKit::EditorState const&, bool, WTF::CompletionHandler<void (bool)>&&) + 364 (WebKit + 3887956) [0x1d3af1354]
 17 -[WKContentView(WKInteraction) _interpretKeyEvent:isCharEvent:] + 212 (WebKit + 6518580) [0x1d3d73734]
 17 -[UIKeyboardImpl handleKeyTextCommandForCurrentEvent] + 192 (UIKitCore + 11228716) [0x1c594862c]
 17 -[UIKeyboardImpl _handleKeyCommand:] + 28 (UIKitCore + 11312584) [0x1c595cdc8]
 17 -[UIKeyboardImpl _handleKeyCommandCommon:testOnly:] + 88 (UIKitCore + 11312688) [0x1c595ce30]
 17 -[UIKeyboardImpl handleKeyCommand:repeatOkay:beforePublicKeyCommands:testOnly:] + 4020 (UIKitCore + 11164260) [0x1c5938a64]
 17 -[UIKeyboardImpl handleVerticalArrow:shiftDown:beforePublicKeyCommands:testOnly:isVerticalCandidate:hasCandidatesForTypedInput:inputDelegateCommandEnabled:canHandleInputDelegateCommand:savedHistory:keyCommandTypeHandled:] + 316 (UIKitCore + 11224064) [0x1c5947400]
 17 __221-[UIKeyboardImpl handleVerticalArrow:shiftDown:beforePublicKeyCommands:testOnly:isVerticalCandidate:hasCandidatesForTypedInput:inputDelegateCommandEnabled:canHandleInputDelegateCommand:savedHistory:keyCommandTypeHandled:]_block_invoke + 80 (UIKitCore + 11224344) [0x1c5947518]
 17 -[WKContentView(WKInteraction) _moveDown:withHistory:] + 48 (WebKit + 6520204) [0x1d3d73d8c]
 17 -[WKContentView(WKInteraction) executeEditCommandWithCallback:] + 44 (WebKit + 6519572) [0x1d3d73b14]
 17 -[UIKeyboardImpl selectionWillChange:] + 84 (UIKitCore + 11187000) [0x1c593e338]
 17 -[UIKeyboardImpl prepareForSelectionChange] + 116 (UIKitCore + 11186468) [0x1c593e124]
 17 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 204 (UIKitCore + 11404068) [0x1c5973324]
 17 -[UIKeyboardTaskQueue lockWhenReadyForMainThread] + 120 (UIKitCore + 11402144) [0x1c5972ba0]
 17 -[NSConditionLock lockWhenCondition:beforeDate:] + 104 (Foundation + 257304) [0x1a2ddcd18]
 17 -[NSCondition waitUntilDate:] + 148 (Foundation + 247268) [0x1a2dda5e4]
 17 _pthread_cond_wait + 1292 (libsystem_pthread.dylib + 32216) [0x1a1f5bdd8]
 17 __psynch_cvwait + 8 (libsystem_kernel.dylib + 17736) [0x1a1f24548]
 *17 psynch_cvcontinue + 0 (pthread + 17576) [0xfffffe000a9c04a8]

I feel that it same something to do with this: https://bugs.webkit.org/attachment.cgi?id=375074&action=prettypatch

Setting autocapitalize="none" autocomplete="off" autocorrect="off" spellcheck="false" on the HTML textarea element makes no difference. Any ideas or workarounds?

It's exactly the same phenomenon. This problem is making us very troubled. It has become very frequent in recent months.

Keyboard crashes during typing and updating selection on TextInput
 
 
Q