Skip to content

Fix clipboard stalls during delayed rendering - #15346

Merged
Shyam-Gupta merged 2 commits into
dotnet:mainfrom
davkean:dev/davkean/fix-clipboard-retry-delay
Nov 6, 2026
Merged

Shyam-Gupta merged 2 commits into
dotnet:mainfrom
davkean:dev/davkean/fix-clipboard-retry-delay

Conversation

@davkean

@davkean davkean commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

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:

CClipDataObject::GetData
  -> GetClipboardData
    -> xxxGetRenderData
      -> xxxInterSendMsgEx

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

Cut - Before

After

Cut - After

Test environment(s)

Windows x64; .NET Framework 4.8.1 (x64 and x86) and .NET 12 (x64).

Microsoft Reviewers: Open in CodeFlow

Comment thread docs/shared-wpf-infrastructure.md Resolved
@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.48649% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 39.79260%. Comparing base (05a8326) to head (62cb17f).
⚠️ Report is 6 commits behind head on main.

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     
Flag Coverage Δ
Debug 39.79260% <86.48649%> (+0.45953%) ⬆️
production 42.31695% <86.48649%> (+0.50943%) ⬆️
test 19.92786% <ø> (ø)
unit 42.31695% <86.48649%> (+0.50943%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@davkean
davkean force-pushed the dev/davkean/fix-clipboard-retry-delay branch from afd0943 to f2108de Compare November 2, 2026 02:07
@davkean
davkean requested a balanced review from Copilot November 5, 2026 06:43
@davkean
davkean marked this pull request as ready for review November 5, 2026 06:43
@davkean
davkean requested a review from a team as a code owner November 5, 2026 06:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

@dotnet-policy-service dotnet-policy-service Bot removed the draft draft PR label Nov 5, 2026
@davkean
davkean force-pushed the dev/davkean/fix-clipboard-retry-delay branch 3 times, most recently from b81f10b to 2a15bf2 Compare November 5, 2026 11:31
davkean and others added 2 commits November 5, 2026 22:39
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]>
@davkean
davkean force-pushed the dev/davkean/fix-clipboard-retry-delay branch from 2a15bf2 to 62cb17f Compare November 5, 2026 11:39
@davkean
davkean requested a review from JeremyKuhne November 5, 2026 12:48
@JeremyKuhne
JeremyKuhne requested review from Shyam-Gupta and a balanced review from Copilot November 5, 2026 16:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 Shyam-Gupta left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @davkean.

@Shyam-Gupta Shyam-Gupta added area-Clipboard Issues related to Clipboard servicing-consider .NET Shiproom label indicating a PR seeks to enter into a branch under Tell-Mode criteria labels Nov 5, 2026
@davkean

davkean commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

@Shyam-Gupta Could you merge this? I don't have permissions.

@Shyam-Gupta

Copy link
Copy Markdown
Member

@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 JeremyKuhne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for your help and patience. :)

@davkean

davkean commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

No worries. @Shyam-Gupta could you merge this?

@Shyam-Gupta
Shyam-Gupta merged commit 8c48f24 into dotnet:main Nov 6, 2026
8 checks passed
@Shyam-Gupta

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 6, 2026
Shyam-Gupta added a commit that referenced this pull request Nov 7, 2026
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)
Shyam-Gupta added a commit that referenced this pull request Nov 9, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Clipboard Issues related to Clipboard servicing-consider .NET Shiproom label indicating a PR seeks to enter into a branch under Tell-Mode criteria

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants