Cannot remove an observer <TUINSCursorUIController 0x600001844340> for the key path "visible" from <NSSavePanel 0x10ff05200> because it is not registered as an observer.

I have two NSTextField as an accessoryView of NSSavePanel. When I try to change focus in between them with a caps lock on the whole panel will crash. This will also happen when NSTextField if focused with caps lock on and i press cmd+tab (app switching).

This happens on Sonoma + Sequoia beta. On top I have noticed editing NSTextField in accessoryView is completely broken on Sonoma and I can only edit it ONLY when the textfiled is using bindings.

I am trying to find a workaround for the caps lock indicator being displayed. The only idea I have ATM is to observe NSApp.windows and look for TUINSWindow and force close it when it's visible.

Is there any other workaround to prevent this crash?

https://youtu.be/BCVjZH7684U

Sample code:

import Cocoa

class ViewController: NSViewController {

    let savePanel = NSSavePanel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let view = NSGridView(views: [[NSTextField(string: "111111")], [NSTextField(string: "22222222")]])
        savePanel.accessoryView = view
    }
    
    override func viewWillAppear() {
        savePanel.runModal()
    }
}

Crash report:

Cannot remove an observer <TUINSCursorUIController 0x600001844340> for the key path "visible" from <NSSavePanel 0x10ff05200> because it is not registered as an observer.
(
	0   CoreFoundation                      0x000000019a2522ec __exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x0000000199d36158 objc_exception_throw + 60
	2   Foundation                          0x000000019b30436c -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 628
	3   Foundation                          0x000000019b3040a4 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 136
	4   TextInputUIMacHelper                0x0000000253d9e598 -[TUINSCursorUIController deactivate:] + 416
	5   AppKit                              0x000000019dbda3e4 -[NSTextInputContext deactivate] + 288
	6   AppKit                              0x000000019da3fff4 +[NSTextInputContext currentInputContext_withFirstResponderSync:] + 228
	7   AppKit                              0x000000019da4f084 -[NSView _setWindow:] + 692
	8   AppKit                              0x000000019db7d880 -[NSTextView(NSPrivate) _setWindow:] + 216
	9   AppKit                              0x000000019e4da778 __21-[NSView _setWindow:]_block_invoke.146 + 268
	10  CoreAutoLayout                      0x00000001a2aba588 -[NSISEngine withBehaviors:performModifications:] + 88
	11  AppKit                              0x000000019da4f4b4 -[NSView _setWindow:] + 1764
	12  AppKit                              0x000000019da7712c -[NSView removeFromSuperview] + 168
	13  AppKit                              0x000000019dc7c0e8 -[_NSKeyboardFocusClipView removeFromSuperview] + 56
	14  AppKit                              0x000000019dbc5474 -[NSWindow endEditingFor:] + 368
	15  AppKit                              0x000000019da770d0 -[NSView removeFromSuperview] + 76
	16  AppKit                              0x000000019dc7c0e8 -[_NSKeyboardFocusClipView removeFromSuperview] + 56
	17  AppKit                              0x000000019dc7be00 -[NSCell endEditing:] + 452
	18  AppKit                              0x000000019dc7b994 -[NSTextField textDidEndEditing:] + 264
	19  CoreFoundation                      0x000000019a1d2144 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 148
	20  CoreFoundation                      0x000000019a2663d8 ___CFXRegistrationPost_block_invoke + 88
	21  CoreFoundation                      0x000000019a266320 _CFXRegistrationPost + 440
	22  CoreFoundation                      0x000000019a1a0678 _CFXNotificationPost + 768
	23  Foundation                          0x000000019b2bd2c4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 88
	24  AppKit                              0x000000019dc7b5fc -[NSTextView(NSSharing) resignFirstResponder] + 668
	25  AppKit                              0x000000019db2ca80 -[NSWindow _realMakeFirstResponder:] + 196
	26  AppKit                              0x000000019dcc1764 -[NSWindow _makeParentWindowHaveFirstResponder:] + 76
	27  ViewBridge                          0x00000001a296c8c0 -[NSAccessoryViewWindow makeFirstResponder:] + 80
	28  AppKit                              0x000000019dbdbb9c -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 3148
	29  AppKit                              0x000000019db67504 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 288
	30  AppKit                              0x000000019db67210 -[NSWindow(NSEventRouting) sendEvent:] + 284
	31  ViewBridge                          0x00000001a296cecc -[NSAccessoryViewWindow sendEvent:] + 64
	32  AppKit                              0x000000019e2304f0 -[NSApplication(NSEventRouting) sendEvent:] + 1604
	33  AppKit                              0x000000019dc6df6c -[NSApplication _doModalLoop:peek:] + 276
	34  AppKit                              0x000000019dc6ce38 __35-[NSApplication runModalForWindow:]_block_invoke_2 + 56
	35  AppKit                              0x000000019dc6cde4 __35-[NSApplication runModalForWindow:]_block_invoke + 108
	36  AppKit                              0x000000019dc6c6b0 _NSTryRunModal + 100
	37  AppKit                              0x000000019dc6c570 -[NSApplication runModalForWindow:] + 292
	38  AppKit                              0x000000019e690490 -[NSSavePanel runModal] + 340
	39  SavePanelAccessory                  0x0000000100435ad4 $s18SavePanelAccessory14ViewControllerC14viewWillAppearyyF + 60
	40  SavePanelAccessory                  0x0000000100435b0c $s18SavePanelAccessory14ViewControllerC14viewWillAppearyyFTo + 36
	41  AppKit                              0x000000019db387f4 -[NSViewController _sendViewWillAppear] + 32
	42  AppKit                              0x000000019db386bc -[NSViewController _windowWillOrderOnScreen] + 80
	43  AppKit                              0x000000019e4e7b38 -[NSView _windowWillOrderOnScreen] + 56
	44  AppKit                              0x000000019e4e7ba4 -[NSView _windowWillOrderOnScreen] + 164
	45  AppKit                              0x000000019db38570 -[NSWindow _doWindowWillBeVisibleAsSheet:] + 40
	46  AppKit                              0x000000019e4fc418 -[NSWindow _reallyDoOrderWindowAboveOrBelow:] + 1028
	47  AppKit                              0x000000019e4fcfec -[NSWindow _reallyDoOrderWindow:] + 64
SHORTENED

Cannot remove an observer &lt;TUINSCursorUIController 0x600001844340&gt; for the key path "visible" from &lt;NSSavePanel 0x10ff05200&gt; because it is not registered as an observer.
 
 
Q