Repository navigation
fix(clipboard): stop caching denied clipboard reads on Win32 - #24357
Conversation
`OpenClipboard` is a global exclusive lock with exactly one winner, and it is deniable. The Win32 clipboard read took that lock with a single attempt and no retry, materialised *every* format under it, then cached the result - successes and failures alike - until the next `WM_CLIPBOARDUPDATE`. Since `AddClipboardFormatListener` broadcasts `WM_CLIPBOARDUPDATE` to every listener at once, N instances of an app all race for the lock on the same tick. One wins; the other N-1 cache an *empty* `DataPackage`, so `Contains(Text)` is false, `TextBox.CanPasteClipboardContent` is false, and the Paste menu item disappears until the clipboard changes again. Measured at exactly (N-1)/N: 21/24 dead with 8 instances, 0/24 after this change. A denied read was also indistinguishable from an empty clipboard even with a single instance - an un-retried `OpenClipboard` failed 3-13% of the time on an idle desktop. Availability no longer needs the lock at all: - Probe the known formats with `IsClipboardFormatAvailable`, which is documented lock-free and measured 100% correct while another process holds the lock - including the never-rendered delay-render case, where it answers without triggering a render. - Then make ONE non-blocking `EnumClipboardFormats` attempt for full fidelity, so app-specific format names still surface. If that attempt is denied the package is still correct for every format Uno can decode, and the read is simply not cached: the next call retries. Retry across calls, never inside one, so nothing on the paste-enablement path can block. - Key the cache on `GetClipboardSequenceNumber` (also lock-free) rather than on `WM_CLIPBOARDUPDATE`, so a cached package can never outlive the clipboard generation it was built from. - A jittered warm-up after `WM_CLIPBOARDUPDATE` (~60/150/400 ms) fills the complete list in the background. Jitter matters: processes woken by one broadcast back off in lockstep and collide again without it. Payloads are now fetched lazily via `SetDataProvider`, on demand, one format at a time, awaiting the lock instead of sleeping on it. This also fixes a pre-existing use-after-free: the old deferred providers captured an `HGLOBAL` from `GetClipboardData` and `GlobalLock`ed it inside the callback, i.e. after `CloseClipboard`, when the handle is no longer valid. Providers re-fetch by format id and verify the sequence number first. `SetContent` resolves all payloads *before* opening the clipboard, so it no longer pumps the event loop - dispatching arbitrary UI work, and possibly a re-entrant `WM_CLIPBOARDUPDATE` - while holding the global lock. Eager materialisation of 21 delay-rendered formats measured 976 ms, during which every other application's clipboard write fails; `Set-Clipboard` from PowerShell now succeeds first try against 12 running instances. `SetContent` and `Clear` retry the open 10 x 100 ms (WPF's cadence, which its `OleRetryCount`/`OleRetryDelay` share) and check that they actually hold the lock. Previously `ClipboardDisposable` logged the denial and continued with `_shouldClose == false`, so `SetContent` issued every `SetClipboardData` against a clipboard it had never opened and `Clear` silently no-oped. Drag/drop is deliberately untouched: `ReadContentIntoPackage` and drop's two decoder bypasses still share the old eager path, so a clipboard fix cannot regress drop. Unifying those decoders is left as a follow-up. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Move ReadContentIntoPackage, GetFileDropList and their private GetText/GetBitmap helpers -- the only Win32ClipboardExtension members consumed by Win32DragDropExtension -- into Win32ClipboardExtension.DragDrop.cs. Pure move, verified byte-identical; also drops the now-unused Windows.Storage and Windows.Win32.UI.Shell usings from the original file. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The BMP file-header wrap existed verbatim in both Win32ClipboardExtension and its drag-drop partial. Both now call a single ConvertDibToBmp helper. The SDL zlib notice moves from the file header onto that helper. SDL's bih_size/dib_size arithmetic had already been replaced by an independently sourced colorTableSize switch on biCompression, so only the BMP format spec remains shared - a file-wide notice overstated the derivation. Scoping it also restores attribution to the drag-drop copy, which lost it when the partial was split out, and follows the existing precedent in Win32WindowWrapper.SetIcon. Also adds the missing SDL entry to THIRD-PARTY-NOTICES.md. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The user32/kernel32 entry points were members of the Given_Clipboard partial, so its cross-platform sibling could reach them with nothing to flag the misuse: the Skia test assembly is a single generic binary that also runs on X11, macOS, wasm and mobile, where these throw DllNotFoundException. A `file` class is a compiler-enforced boundary, and only the members the test calls stay visible inside it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
It introduces a confirmed use-after-free in the drag/drop bitmap provider and a size/allocation bug in ConvertDibToBmp, plus a test reliability issue that can leave a background thread holding the clipboard.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes a Skia Win32 clipboard multi-instance contention issue by avoiding caching “denied” clipboard reads, keying caching to GetClipboardSequenceNumber, probing availability lock-free, and fetching payloads lazily with retry logic only on the user-initiated/read-payload path.
Changes:
- Reworked Win32 clipboard read/write flow to avoid blocking
OpenClipboardon the UI hot path, add sequence-keyed caching, and defer payload reads viaSetDataProvider. - Added a Skia Win32 runtime test that holds the clipboard lock from another thread and verifies
Clipboard.GetContent().Contains(Text)remains correct under contention. - Added SDL third-party notice and extended Win32 P/Invoke allowlist for newly used APIs.
File summaries
| File | Description |
|---|---|
| THIRD-PARTY-NOTICES.md | Adds SDL license notice for derived Win32 Skia host code portions. |
| src/Uno.UI.RuntimeTests/Tests/Windows_ApplicationModel/DataTransfer/Given_Clipboard.Win32.cs | Adds a Win32 clipboard contention regression test. |
| src/Uno.UI.Runtime.Skia.Win32/ApplicationMode/DataTransfer/Win32ClipboardExtension.DragDrop.cs | Introduces drag/drop HGLOBAL readers to reuse clipboard decoders for IDataObject payloads. |
| src/Uno.UI.Runtime.Skia.Win32/ApplicationMode/DataTransfer/Win32ClipboardExtension.cs | Main clipboard logic overhaul: lock-free probing, lazy payload fetch, retries, warm-up, sequence-number caching. |
| src/Uno.UI.Runtime.Skia.Win32.Support/NativeMethods.txt | Adds GetClipboardSequenceNumber and IsClipboardFormatAvailable to generated P/Invoke list. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24357/wasm-skia-net9/index.html |
A holder that never returns from Join is still owning the clipboard and would poison every test after this one. The Join result is captured in the finally and asserted after it, so a genuine failure inside the try still propagates first. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The new Win32 runtime test creates a message-only window using a managed WndProc delegate that is not rooted for the window’s lifetime, which can lead to GC-related crashes.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 1
- Review effort level: Lite
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24357/wasm-skia-net9/index.html |
|
The build 231644 found UI Test snapshots differences: Details
|
GC.KeepAlive only rooted the delegate to the end of CreateMessageOnlyWindow, while the registered class and the window both outlived it, so lpfnWndProc dangled from the moment it returned. Thread exit is the trigger: Windows destroys the thread's windows and dispatches WM_DESTROY through that pointer, which is precisely what holder.Join waits on. A built-in class already has a native window procedure, so there is no delegate to keep alive and no class to register - which also removes the class that was never unregistered and the CurrentManagedThreadId name a recycled id could collide with. The window is now destroyed on the thread that created it. Measured against the previous shape: OpenClipboard(hwnd) succeeds, a contending thread is denied with ERROR_ACCESS_DENIED, IsClipboardFormatAvailable still reports the text, and the payload survives destroying its owner. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
There are correctness regressions/bugs identified in the new Win32 clipboard path (notably CF_HDROP/StorageItems handling and payload sequence validation) plus a couple of test/build hygiene issues that should be resolved before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (5)
Previously missed (4) — in code that hasn't changed since the last review.
src/Uno.UI.Runtime.Skia.Win32/ApplicationMode/DataTransfer/Win32ClipboardExtension.cs:296
- GetContent’s lock-free availability probe omits CF_HDROP from _knownStandardFormats, so StorageItems will not be surfaced when EnumClipboardFormats can’t run under contention (even though Win32ClipboardExtension can decode CF_HDROP).
This issue also appears on line 428 of the same file.
src/Uno.UI.Runtime.Skia.Win32/ApplicationMode/DataTransfer/Win32ClipboardExtension.cs:523
- TryFetchPayload only validates the clipboard sequence number before attempting to open the clipboard. If the clipboard changes between that check and successfully acquiring the lock, this can return data from a different clipboard generation than the one the DataPackageView was built for.
src/Uno.UI.RuntimeTests/Tests/Windows_ApplicationModel/DataTransfer/Given_Clipboard.Win32.cs:146 - AllocUnicodeText assumes GlobalAlloc/GlobalLock succeed; if either returns IntPtr.Zero, Marshal.Copy/WriteInt16 will AV. Even in tests, this should fail gracefully rather than crashing the test process.
src/Uno.UI.Runtime.Skia.Win32/ApplicationMode/DataTransfer/Win32ClipboardExtension.DragDrop.cs:15 - Unused using directive
Windows.Win32.System.Ole(this file doesn’t reference any OLE types), which can trigger IDE0005/style failures in analyzer-enforced builds.
src/Uno.UI.Runtime.Skia.Win32/ApplicationMode/DataTransfer/Win32ClipboardExtension.cs:433
- ResolveFormat currently drops CF_HDROP entirely, which prevents Clipboard.GetContent() from advertising/serving StandardDataFormats.StorageItems (file copy/paste) even when the format is available.
// synthesized meta-file formats
CLIPBOARD_FORMAT.CF_METAFILEPICT => null,
CLIPBOARD_FORMAT.CF_ENHMETAFILE => null,
CLIPBOARD_FORMAT.CF_HDROP => null,
- Files reviewed: 5/5 changed files
- Comments generated: 1
- Review effort level: Lite
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24357/wasm-skia-net9/index.html |
EmptyClipboard and SetClipboardData were unchecked, so a failed setup surfaced as the Contains(StandardDataFormats.Text) assertion further down, reading as a product regression rather than a broken premise. Each step now records which call failed and with what error, asserted as soon as the holder reports back. The flag gating CloseClipboard stays strictly "we own the lock and have to release it": a holder that took the clipboard and then failed still has to release it, or it poisons every later test. The UI thread no longer reads it, so it moved into the lambda as a local, leaving no cross-thread flag to overload. SetClipboardData only takes ownership of the HGLOBAL on success, so the very failure path being surfaced here leaked the allocation. Allocation and free are now paired in Win32.TrySetUnicodeText, which also lets SetClipboardData and CF_UNICODETEXT go private. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The new Win32 runtime test’s interop helper doesn’t handle GlobalAlloc/GlobalLock failure paths, which can crash or leak in the test runner.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 1
- Review effort level: Lite
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24357/wasm-skia-net9/index.html |
GlobalAlloc and GlobalLock were unchecked, so a failure there reached Marshal.Copy and threw on the holder thread - which has a finally and no catch, so it would take the test runner down and produce no results XML rather than failing the test. Both are now checked and report the Win32 error, freeing the handle when the lock is what failed. Neither import set SetLastError, so the error they reported would have been a stale one from an unrelated call; both set it now. Defensive hardening, not a root-cause fix: a 56-byte GlobalAlloc failing is not a condition that has been observed. The value is the missing SetLastError and a helper whose every step reports instead of throwing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
It substantially changes Win32 clipboard interop and caching/async behavior on a core user-facing path, which warrants final human review despite the added targeted runtime test.
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0 new
- Review effort level: Lite
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24357/wasm-skia-net9/index.html |
|
The build 231950 found UI Test snapshots differences: Details
|
|
@Mergifyio backport release/stable/6.7 |
✅ Backports have been createdDetails
|
|
@Mergifyio backport servicing/6.8 |
✅ Backports have been createdDetails
|
|
Tick the box to add this pull request to the merge queue (same as
|
…r-24357 fix(clipboard): stop caching denied clipboard reads on Win32 (backport #24357)
…6.7/pr-24357 fix(clipboard): stop caching denied clipboard reads on Win32 (backport #24357)
GitHub Issue: tracked internally — no public issue to link.
PR Type:
🐞 Bugfix
What changed? 🚀
Current behavior. Run N instances of a Skia Win32 app, copy text in any one of them, and only one instance can paste. In the other N−1,
Clipboard.GetContent().Contains(Text)returnsfalse,TextBox.CanPasteClipboardContentisfalse, and the Paste item vanishes from the context menu — until the clipboard changes again, when a different instance wins and a different set breaks.Cause.
OpenClipboardis a global exclusive lock with exactly one winner, and it is deniable.Win32ClipboardExtensiontook that lock with a single attempt and no retry, materialised every format under it, then cached the result — successes and failures alike — until the nextWM_CLIPBOARDUPDATE.Because
AddClipboardFormatListenerbroadcastsWM_CLIPBOARDUPDATEto every listener at once, all N instances race for the lock on the same tick. One wins; the rest getERROR_ACCESS_DENIED, which the old code could not distinguish from an empty clipboard, and then cached that emptyDataPackage. Three compounding defects: a denied read looked like an empty one, the failure was cached, and the read ran synchronously fromWndProc.This is not only a multi-instance bug — with no competitor at all, a single un-retried
OpenClipboardstill failed 3–13 % of the time on an idle desktop.Availability no longer takes the lock
IsClipboardFormatAvailable, which is documented lock-free and measured 100 % correct while another process holds the lock — including the Excel-style never-rendered delay-render case, where it answers without triggering a render.EnumClipboardFormatsattempt for full fidelity, so app-specific format names still surface. If that attempt is denied, the package is still correct for every format Uno can decode and is simply not cached — the next call retries. Retry across calls, never inside one, so nothing on the paste-enablement path can block.GetClipboardSequenceNumber(also lock-free) rather than onWM_CLIPBOARDUPDATE, so a cached package can never outlive the clipboard generation it was built from. Verified that the sequence number bumps onEmptyClipboard/SetClipboardDataand is visible to other processes while the clipboard is still held.WM_CLIPBOARDUPDATE(~60/150/400 ms) fills the complete list in the background. The jitter is not cosmetic: processes woken by one broadcast back off in lockstep and collide again without it.Payloads are fetched lazily, on demand
Via
SetDataProvider, one format at a time,awaiting the lock instead of sleeping on it. This also fixes a pre-existing use-after-free: the old deferred providers captured anHGLOBALfromGetClipboardDataandGlobalLocked it inside the callback — i.e. afterCloseClipboard, when the clipboard-owned handle is no longer valid. Providers now re-fetch by format id and verify the sequence number first.Writes stop holding the lock across arbitrary work
SetContentresolves all payloads before opening the clipboard, so it no longer pumps the event loop — dispatching arbitrary UI work, and possibly a re-entrantWM_CLIPBOARDUPDATE— while holding the global lock. Eager materialisation of 21 delay-rendered formats measured 976 ms, during which every other application's clipboard write fails.SetContentandClearretry the open 10 × 100 ms (WPF's cadence, shared by itsOleRetryCount/OleRetryDelay) and now check that they actually hold the lock. PreviouslyClipboardDisposablelogged the denial and continued with_shouldClose == false, soSetContentissued everySetClipboardDataagainst a clipboard it had never opened, andClearsilently no-oped.The retry budget is deliberately split: one cheap attempt on the read hot path, the full budget only on payload fetch / write. A 10 × 100 ms loop inside
GetContent()would block the dispatcher for up to 1 s per call, per TextBox, on every clipboard change — puttingCanPasteClipboardContentback on a global exclusive lock.Measured
8 instances × 3 copies, then 12 × 5, driving a real Uno app and publishing from a third-party writer (PowerShell
Set-Clipboard):Set-Clipboardretries needed87.5 % is exactly (N−1)/N. Format fidelity is preserved, not just paste: after the fix every instance reports the complete list including
DataObjectandOle Private Data— app-specific names with no table entry, learnable only fromEnumClipboardFormats.Chose user32 over OLE, deliberately
OleGetClipboarddoes give lock-free enumeration, which user32 cannot. But measurement showed itsproxy.GetDatapayload path is 0 % against raw-published content, so an OLE rewrite would have needed the same retry logic and would not have fixed this bug.The drag/drop boundary, and what did get cleaned up
Drop's decode path is deliberately not merged into the clipboard's:
ReadContentIntoPackageand drop's two decoder bypasses still use the old eager path, so a clipboard fix cannot regress drop. Those readers now live in their own partial,Win32ClipboardExtension.DragDrop.cs, so the boundary is visible in the file layout instead of only in a comment.The one thing that was unified is the pure pixel work: a single
ConvertDibToBmp(void* dib, uint dibSize)now serves both paths, since the DIB→BMP header rewrite has nothing to do with where the handle came from. That conversion is derived from SDL, so this also adds the corresponding zlib-license notice toTHIRD-PARTY-NOTICES.md. The remaining duplication —GetText×2 and drop's decoder dispatch — is left for a follow-up.Commits
fix(clipboard)refactor(clipboard)refactor(clipboard)test(clipboard)file static classso a sibling partial cannot reach themPR Checklist ✅
Screenshots Compare Test Runresults.Given_Clipboard.Win32.csasserts the contention is real before asserting the fix: a background thread takesOpenClipboardfrom a message-only window and holds it, and the test fails unlessOpenClipboardis genuinely denied withERROR_ACCESS_DENIED. Confirmed to discriminate — reverting only the clipboard file makes it fail with the bug's exact log signature.🤖 Generated with Claude Code