question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.Windows.ContextLayoutManager.UpdateLayout() throws KeyNotFoundException

See original GitHub issue

Description

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:open
  • Created 7 months ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tripleacodercommented, May 31, 2023

We also have this problem. Our WPF application crashes at random. We tried updating .NET Framework from 4.7.2 to 4.8 but it didn’t help. Our application also extensively uses DispatchTimer, Dispatcher.Invoke() and we also rely heavily on async/await for I/O operations.

Unfortunately, I can’t provide reliable reproduction at this time. Our application crashes roughly once per day which makes it impossible to ship, yet very hard to diagnose.

Any help or suggestions would be much appreciated.

@tripleacoder, did you have any success with this issue?

I never solved it. Instead I have made a workaround that simply ignores the exception in my global exception handling code:

private void HandleUnhandledExceptions(DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = false; // true; // program lives, false = windows will close it

            Exception exception = e.Exception;

            if (exception is System.Collections.Generic.KeyNotFoundException)
            {
                if (exception.StackTrace.Contains("System.Windows.Automation.Peers.ItemPeersStorage"))
                {
                    // This exception is described here:
                    // https://dev.azure.com/REDACTED/_workitems/edit/4094
                    // Cause: unknown.
                    // It does not affect the user in any way, so we can just ignore it. Don't send any emails to the dev either.
                    e.Handled = true;
                }
                else
                {
                    this.vm.HandleUserException(exception, false);
                }
            }
            else
            {
                this.vm.HandleUserException(exception, true);
            }
        }
2reactions
miloushcommented, Feb 18, 2023

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~

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found