-
Notifications
You must be signed in to change notification settings - Fork 710
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
Clipboard.GetContent() often crashes with a stowed exception (c000027b) #6754
Comments
I cannot reproduce that |
@castorix I have a slightly different use case. I changed the steps to reproduce. Can you reproduce it now? |
I still could not reproduce it |
@castorix Thanks for trying. That code is indeed where the crash occurs. It does not occur frequently and some users see it far more often than others. I have the feeling it is some timing issue / race condition and therefor can depend on the machine specs. |
@castorix If this issue can be reproduced or not: bottom line of these types of issues is that some low level exceptions crash the complete app without any clue on why this is happening. |
As Workaround, maybe you can test if Clipboard APIs crash too : |
That's what I do now. I don't see a crash in the low level win32 API's, so it must be located in the Clipboard implementation. |
Some reasons Clipboard.GetContent might throw:
|
@asklar If it just throws an exception I can handle, I would not complain. The clipboard crashes the app without getting the opportunity to catch the exception.
But again, this is not the issue I'm complaining about. Please read carefully. The issue is that I can't catch the exception and the app crashes right away |
@wbokkers I just tried to reproduce it myself, and I don't see the crash either. |
I don't see the issue myself if I try to reproduce. The crash occurs seldom. Maybe I should rephrase it: |
It just crashed again. I managed to save a crash dump. Where can I send the crash dump? The following message appears in the output window. I don't know if it is related to this issue:
|
Opened the dump file with WinDbg. I don't know what to do exactly, but I got these messages:
|
@asklar Can I send the dump-file to you or someone else at Microsoft? |
@asklar Here is the WinDbg analysis: |
@wbokkers Thanks for sharing that debugger output! What the stack shows is unsupported reentrancy, where the Clipboard.GetContent() call went into CoCreateInstance() to create an out-of-proc COM object, which created a COM message pump (combase!ModalLoop) which pumped a message which ended up back in XAML (Microsoft_ui_xaml!CXcpDispatcher::OnReentrancyProtectedWindowMessage). The complete stack isn't shown, but since the crash was in OnReentrancyProtectedWindowMessage that means that farther down the stack XAML was delivering an event (presumably the textbox paste event). XAML currently doesn't support reentrancy at certain times, including firing some events and when rendering (including layout passes at the start of the render process). UWP uses the ASTA threading model which helps prevent reentrancy. Without that, the general recommendation for potential reentrancy issues is to delay processing until after XAML is off the stack, such as via DispatcherQueue.TryEnqueue(). There might be a chance for this specific case that calling Clipboard.GetContent() once while XAML is not on the stack might allow it to work while XAML is on the stack, if the underlying COM object gets cached after it is first created. I don't know if it does, though, or if its lifetime is guaranteed after that point. |
@codendone Thank you for your response. Currently when I want to run something on the UI-thread I do the following: Will these type of issues be eventually solved in the XAML/WinRT layers? I think it is not ok to get in all kind of COM/threading-trouble by just calling higher level APIs. |
The code you mentioned is great if you need to ensure some code runs on the UI thread. Your suspicion is correct that for this Clipboard issue it is additionally necessary to ensure the UI thread stack doesn't have XAML in a "no reentrancy allowed" state, and therefore the safest bet for that is to always We do want to improve XAML and other parts of the platform to safely handle reentrancy. This is a sizeable challenge since so much code was written assuming ASTA and some parts need to be redesigned for this situation. We have some known work booked work for this over the next few months, some less known investigation booked, and there are likely some unknown unknowns. It isn't clear yet how long it will take. There may be a release during this process which includes a change to help find problematic reentrancy so at least such problems can be consistently found and addressed. |
Thank you @codendone ! Nice to know that this is on your radar and that XAML will be improved to better fit WinUI3. Do you think it is wise to always run TryEnqueue and never check for thread access first every time I want to run something on the UI to prevent these type of issues? |
If the code might use a COM object, then always doing a TryEnqueue, if possible, would be a good idea. If it is doing something simpler, such as setting some properties on elements, then there should be no need to TryEnqueue if already on the UI thread so having the thread access check in that case would be good for efficiency. |
Is this issue still active. I'm not sure about next steps. Thanks! |
@bpulliam If it is still an issue, it should be mentioned in the documentation that you can only access the clipboard in a TryEnqueue. But it would be better if TryEnqueue is not needed for clipboard access. |
@bpulliam What should be done is mentioned by @codendone : "We do want to improve XAML and other parts of the platform to safely handle reentrancy. " |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
Are there still plans to improve/fix this? |
Describe the bug
Calling Clipboard.GetContent() often result in a crash that cannot be handled.
The only message I get while debugging, is a c000027b exception (stowed exception), but there's no managed exception that I can catch.
My use case it to override the default paste behavior of a TextBox. When the user pastes a binary (e.g. an image), I need to handle this as well. This is needed for a chat-like experience to send either text or binary attachments.
Steps to reproduce the bug
Coding steps:
Usage steps:
Expected behavior
I can understand that the clipboard needs to throw an exception if the current application is not active.
But this should be a managed exception that I can handle in code.
Expected behavior:
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.0
Windows app type
Device form factor
Desktop
Windows version
May 2021 Update (19043)
Additional context
No response
The text was updated successfully, but these errors were encountered: