Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception "Collection was modified" in System.Windows.Input.StylusWisp on remote desktop #7222

Open
Lumoryel opened this issue Oct 21, 2022 · 10 comments
Labels
needs more information Not enough information has been provided. Please share more detail as requested

Comments

@Lumoryel
Copy link

Hi there,
got this exception today on a remote machine directly after connecting via windows remote desktop:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator.MoveNext()
at System.Windows.Input.StylusWisp.WispLogic.OnTabletAdded(UInt32 wisptisIndex)
at System.Windows.Input.StylusWisp.WispLogic.HandleMessage(WindowMessage msg, IntPtr wParam, IntPtr lParam)
at System.Windows.SystemResources.InvalidateTabletDevices(WindowMessage msg, IntPtr wParam, IntPtr lParam)
at System.Windows.SystemResources.SystemThemeFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
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)

We have a .NET Framework 4.8 WPF Program running in the background, the window was not visible at that moment.
Running on Windows 11 Pro.

@dipeshmsft
Copy link
Member

Can you provide us a sample repro application ?

@dipeshmsft dipeshmsft added needs more information Not enough information has been provided. Please share more detail as requested 📭 waiting-author-feedback To request more information from author. labels Oct 25, 2022
@Lumoryel
Copy link
Author

Unfortunately not, it happened just one time when I connected via remote desktop.
Could it be a (threading?)-Problem in WispLogic.OnTabletAdded ?

@ghost ghost removed the 📭 waiting-author-feedback To request more information from author. label Oct 25, 2022
@lindexi
Copy link
Member

lindexi commented Oct 25, 2022

@Lumoryel I review the code and I find all the __penContextsMap in the lock by __penContextsLock. This at least shows that this code is thread safe.

foreach (PenContexts contexts in __penContextsMap.Values)
{
contexts.AddContext(tabletIndex);
}

@Lumoryel
Copy link
Author

Lumoryel commented Oct 25, 2022

@lindexi You're right. I can't find code that modifies __penContextsMap without a lock.

By the way I saw that not all methods are in lock(__penContextsLock) when accessing __penContextsMap e.g. via TryGetValue().
Additionally, RefreshTablets() is enumerating the __penContextsMap without locking (e.g. called from PostProcessInput() ).

But this shouldn't be the cause for the exception.

Sure that this source code also applies for .net framework 4.8 ?
When I search in referencesource, I can't find the Stylus classes.

@miloush
Copy link
Contributor

miloush commented Oct 25, 2022

How about this one

@Lumoryel is correct that this enumerates the collection without locking from PostProcessInput. This would cause the exception when you try to modify it elsewhere. EDIT: Nevermind, I see what you are saying now, competing foreach would not cause this stack, however, it would still throw if PostProcessInput calls RefreshTablets() and during that the collection is modified (even if under lock). I don't see any out-of-lock modifications either.

You can use ILSpy to check whether the assemblies you are using on that machine contain the code you are looking for, both 3.0 and latest version of PresentationCore do seem to include these locks though.

If you can reproduce, time travel would be helpful.

@singhashish-wpf
Copy link
Member

@Lumoryel Do we have any more inputs on this? A TTD trace or some reliable sample repro?

@Lumoryel
Copy link
Author

@singhashish-wpf I never saw this bug again.
What I noticed meanwhile:
On Windows 11, when you run a .NET Framework 4.8 targeted program, it actually runs Framework 4.8.1 code while on windows 10 it runs 4.8 code.

Actually I'm confused about where to look for 4.8 and 4.8.1 code?

(I had this problem here with a 4.8 framework application: #6319)

@GSonofNun
Copy link

Just had this happen with our application. The user RDP'd from their laptop (with touch/pen support) to their desktop (without pen support) where our aplication was already running. Our app is using .NET 6, and both PCs are running Windows 11.

Unfortunately we don't have any good repro steps for it. Just wanted to mention another sighting

@GSonofNun
Copy link

I do have a minidump of the crash if it would be helpful.

@Tennyleaz
Copy link

Tennyleaz commented Jun 27, 2023

Had this issue occurred once on my app, when a DisplayLink touch screen was removed from USB.
Using WPF and .net 4.7.2 on Windows 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more information Not enough information has been provided. Please share more detail as requested
Projects
None yet
Development

No branches or pull requests

7 participants