System.Windows.ContextLayoutManager.UpdateLayout() throws KeyNotFoundException
See original GitHub issueDescription
I get this exception quite often in my .NET 4.8 WPF app. The error is sent from clients automatically and I do not know how to reproduce it, or from what part of the code it originates.
I found a similar issue saying it had been fixed in 4.8: [https://github.com/dotnet/wpf/issues/2152]
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at MS.Internal.WeakDictionary`2.get_Item(TKey key)
at System.Windows.Automation.Peers.ItemPeersStorage`1.get_Item(Object item)
at System.Windows.Automation.Peers.ItemsControlAutomationPeer.GetChildrenCore()
at System.Windows.Automation.Peers.ListViewAutomationPeer.GetChildrenCore()
at System.Windows.Automation.Peers.AutomationPeer.EnsureChildren()
at System.Windows.Automation.Peers.AutomationPeer.UpdateChildrenInternal(Int32 invalidateLimit)
at System.Windows.Automation.Peers.ItemsControlAutomationPeer.UpdateChildren()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.ContextLayoutManager.fireAutomationEvents()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
Reproduction Steps
Don’t know.
Expected behavior
No exception
Actual behavior
Program crashes?
Regression?
No response
Known Workarounds
No response
Impact
No response
Configuration
.NET 4.8 Windows 10 x64
Other information
No response
Issue Analytics
- State:
- Created 7 months ago
- Reactions:1
- Comments:13 (4 by maintainers)
Top Results From Across the Web
How can ItemPeersStorage throw exception when getting ...
The System.Windows.Automation.Peers.ItemPeersStorage1.get_Item(Object item) method from From .NET Framework 4.8 Reference Source i'm not seeing ...
Read more >KeyNotFoundException Class (System.Collections.Generic)
A KeyNotFoundException is thrown when an operation attempts to retrieve an element from a collection using a key that does not exist in...
Read more >Launchbox 13.6 crashing, 13.5 works fine - Troubleshooting
ContextLayoutManager.UpdateLayout() at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) at System.Windows.Media.
Read more >Untitled
ContextLayoutManager.UpdateLayout () throws KeyNotFoundException Needs minimal repro project #7542 opened on … WebNov 8, 2022 · With a commitment to ensure WPF ...
Read more >Untitled
DictionaryEntry Struct (System. ... Windows.ContextLayoutManager.UpdateLayout () throws KeyNotFoundException Needs minimal repro project #7542 opened on Feb ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I never solved it. Instead I have made a workaround that simply ignores the exception in my global exception handling code:
I can imagine this happening when the item is removed between the check and usage, classic race condition:
https://github.com/dotnet/wpf/blob/b148c5c887e11f5bc1e40c01fa319ab24852e4b1/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs#L603-L606
That’s why TryGet methods were introduced. However, looking at
WeakDictionary
, this is not doing much better either:https://github.com/dotnet/wpf/blob/b148c5c887e11f5bc1e40c01fa319ab24852e4b1/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WeakDictionary.cs#L225-L229
~Fix seems straightforward, but will need to touch
WeakDictionary
~