Repository navigation
Fix clipboard stalls during delayed rendering - #15346
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #15346 +/- ##
===================================================
+ Coverage 39.33306% 39.79260% +0.45953%
===================================================
Files 246 246
Lines 9806 9836 +30
Branches 1032 1036 +4
===================================================
+ Hits 3857 3914 +57
+ Misses 5769 5741 -28
- Partials 180 181 +1
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
afd0943 to
f2108de
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The new wait permits arbitrary sent-message reentrancy across shared WinForms and WPF clipboard retry paths, requiring maintainer compatibility assessment and Windows runtime validation on .NET Framework and .NET beyond static review.
No open issues 🎉
What changed in this PR
This PR addresses delayed-rendering clipboard stalls in the shared Windows core by allowing retry waits to dispatch sent messages without processing posted messages or queued input.
Changes:
- Replaces sleep-based waits across all five clipboard retry paths while retaining retry intervals and attempt limits.
- Adds native interop declarations for the message-aware wait.
- Adds mock-driven tests for sent-message handling, reentrancy, retry exhaustion, and posted-message preservation.
| File | Description |
|---|---|
| src/System.Private.Windows.Core/tests/System.Private.Windows.Core.Tests/System/Private/Windows/Ole/MockOleServices.cs | Adds configurable OLE result callbacks for retry tests. |
| src/System.Private.Windows.Core/tests/System.Private.Windows.Core.Tests/System/Private/Windows/Ole/ClipboardCoreTests.cs | Adds retry and reentrancy tests using private windows. |
| src/System.Private.Windows.Core/src/System/Private/Windows/Ole/ClipboardCore.cs | Introduces message-aware waits across clipboard retry paths. |
| src/System.Private.Windows.Core/src/NativeMethods.txt | Adds native wait API and result constants. |
Review effort: Balanced
b81f10b to
2a15bf2
Compare
Clipboard readers can hold the clipboard open while requesting delayed rendering from its owner's UI thread. Sleeping between retries prevents that thread from answering, leaving the reader blocked until retries fail. Service sent messages during bounded retry waits without dequeuing posted messages or queued input. Preserve retry intervals, attempt limits, and HRESULT-based failures. Co-authored-by: Copilot <[email protected]>
Make the sent-message reentrancy surface explicit and characterize it with unrelated messages and nested clipboard operations. Verify that outer and nested retry counts and HRESULTs remain independent and that posted messages remain queued. Co-authored-by: Copilot <[email protected]>
2a15bf2 to
62cb17f
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The shared WinForms/WPF retry loop dispatches arbitrary synchronous callbacks, requiring maintainer review and Windows integration validation of reentrancy, OLE delayed rendering, and window teardown beyond mocked coverage.
No open issues 🎉
Review effort: Balanced
|
@Shyam-Gupta Could you merge this? I don't have permissions. |
@JeremyKuhne told me that he will also review this change. I will merge this PR as soon as he is done. |
JeremyKuhne
left a comment
There was a problem hiding this comment.
Looks good, thanks for your help and patience. :)
|
No worries. @Shyam-Gupta could you merge this? |
|
/backport to release/11.0 |
|
Started backporting to |
Backport of #15346 to release/11.0 /cc @Shyam-Gupta @davkean ## Issue Clipboard operations can stall and fail with `CLIPBRD_E_CANT_OPEN`. In a Visual Studio trace, Windows Clipboard User Service (`cbdhsvc`) was reading data through OLE and blocked in the delayed-rendering path: ```text CClipDataObject::GetData -> GetClipboardData -> xxxGetRenderData -> xxxInterSendMsgEx ``` ## Root Cause Windows was synchronously requesting clipboard data from the editor's UI thread (`WM_RENDERFORMAT`), while the service held the clipboard open. That UI thread was trying to replace the clipboard contents and sleeping between failed open attempts, so it could not answer the render request until retries were exhausted. ## Fix The retry wait now lets the UI thread answer sent messages, breaking that dependency without processing posted messages or queued input. Retry intervals and attempt limits remain unchanged. ## Customer Impact Clipboard operations in WinForms and WPF can recover from this contention instead of exhausting retries. ## Regression? Since .NET 11, WinForms runtime repo has started shipping an internal NuGet package, targeting .NET Framework, which includes the shared Clipboard and the new NRBF serializer/deserializer (the BinaryFormatter replacement). This package is consumed by VS AppLocal WPF which is shipped with VS and handles the Clipboard operations. This bug got introduced as part of that new package. Till Dev17, the Clipboard was handled by the old WPF code which didn’t have this issue. So, it can be considered as a regression. ## Risk All pending sent messages can run, not just clipboard-render requests. Their handlers can reenter application code and change clipboard or UI state. The retry deadline does not bound time spent inside a handler. ## Test methodology Mock OLE operations and private windows cover all five retry paths, delayed rendering, unrelated sent messages, nested calls, retry exhaustion, and posted-message preservation without touching the system clipboard. Also manually tested this with Visual Studio by continually holding down CTRL+X. ### Before <img width="1117" height="793" alt="Cut - Before" src="https://github.com/user-attachments/assets/4360c415-be8b-412e-a0fe-59b4c5177038" /> ### After <img width="1117" height="793" alt="Cut - After" src="https://github.com/user-attachments/assets/9ff6a444-dd47-41f8-be17-96dc0f1f1f13" /> ## Test environment(s) Windows x64; .NET Framework 4.8.1 (x64 and x86) and .NET 12 (x64). ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/15371)
Related to #15319 and #15346. ## Proposed changes - Add real cross-process clipboard lock exhaustion and recovery tests, including the original failure HRESULT. - Exercise delayed Unicode rendering during replacement, checking clipboard ownership and preservation of posted messages. - Extend negative retry argument tests to both copy modes and `int.MinValue`. ## Customer Impact - Protects clipboard replacement and delayed reading from contention regressions. - Verifies expected exceptions, recovery, and queued-message preservation. ## Regression? - No; adds coverage for existing behavior. ## Risk - Low. Test-only changes using the existing sequential collection, STA attributes, and RemoteExecutor ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/15384)
Proposed changes
Clipboard operations can stall and fail with
CLIPBRD_E_CANT_OPEN. In a Visual Studio trace, Windows Clipboard User Service (cbdhsvc) was reading data through OLE and blocked in the delayed-rendering path:Windows was synchronously requesting clipboard data from the editor's UI thread (
WM_RENDERFORMAT), while the service held the clipboard open. That UI thread was trying to replace the clipboard contents and sleeping between failed open attempts, so it could not answer the render request until retries were exhausted.The retry wait now lets the UI thread answer sent messages, breaking that dependency without processing posted messages or queued input. Retry intervals and attempt limits remain unchanged.
Customer Impact
Clipboard operations in WinForms and WPF can recover from this contention instead of exhausting retries.
Regression?
Not established; no introducing change has been identified.
Risk
All pending sent messages can run, not just clipboard-render requests. Their handlers can reenter application code and change clipboard or UI state. The retry deadline does not bound time spent inside a handler.
Test methodology
Mock OLE operations and private windows cover all five retry paths, delayed rendering, unrelated sent messages, nested calls, retry exhaustion, and posted-message preservation without touching the system clipboard.
Also manually tested this with Visual Studio by continually holding down CTRL+X.
Before
After
Test environment(s)
Windows x64; .NET Framework 4.8.1 (x64 and x86) and .NET 12 (x64).
Microsoft Reviewers: Open in CodeFlow