Skip to content

Prevent clipboard hangs during concurrent data-store access - #15319

Merged
Shyam-Gupta merged 1 commit into
dotnet:mainfrom
nareshjo:dev/nareshjo/fix-clipboard-store-hang
Oct 28, 2026
Merged

Shyam-Gupta merged 1 commit into
dotnet:mainfrom
nareshjo:dev/nareshjo/fix-clipboard-store-hang

Conversation

@nareshjo

@nareshjo nareshjo commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes 3084829

Root Cause

The clipboard data store reads, writes, and enumerates its format dictionary without synchronization. Concurrent mutation can corrupt dictionary state; on .NET Framework, a corrupted bucket chain can cause a later lookup to loop indefinitely.

Proposed changes

  • Protect the clipboard data store's format dictionary against overlapping managed calls and COM callbacks.
  • Capture complete entries and coherent format snapshots while keeping validation, deserialization, and format mapping outside the per-store gate.
  • Cover concurrent insertion and overwrites, callback reentrancy, format compatibility, and native storage-medium ownership with regression tests.

Customer Impact

  • The synchronization gap was identified while investigating customer-reported clipboard hangs. This change addresses a dictionary-corruption race that can cause clipboard operations to fail or hang.
  • Existing format mapping and native drag-and-drop ownership behavior are preserved; no public API changes.

Regression?

  • Unconfirmed. The unsynchronized dictionary predates this change; an introducing release has not been established.

Risk

Low expected compatibility risk. The per-store gate protects dictionary access and snapshot creation; validation, deserialization, format mapping, and explicit COM calls stay outside locked sections. Payload ownership is unchanged. Lock contention and larger snapshots add potential performance costs.

Test methodology

  • Exercise colliding concurrent insertions, concurrent reads and overwrites, complete data/flag publication, and coherent format enumeration.
  • Verify validation and deserialization callbacks can wait for another thread to access the store. Check format mapping/order, validation failures, serializable first-entry behavior, and native-medium ownership.
  • Rebuild the affected test project with the repository's Arcade workflow in Debug and Release. Run the final DataStore regression tests on both targets: 10 Framework cases and 11 modern .NET cases per configuration, totaling 42 passes with no failures or skips.
  • Exercise the exact timeout-cleanup block in an isolated Framework harness with completed, cancellable, and delayed workers. All three checks passed; this checks cleanup behavior, not product COM contention.
  • Basic copy/paste and undo smoke checks with a consumer using the same production fix completed without observed failures. The original intermittent hang has not been reproduced and verified fixed.

Test environment(s)

  • Windows x64, Debug and Release.
  • .NET Framework 4.8.1 target (net481).
  • Modern target: net11.0-windows7.0, executed using the repository-selected .NET runtime 12.0.0-alpha.1.26519.114.
  • Repository-local .NET SDK: 12.0.100-alpha.1.26480.102.
Microsoft Reviewers: Open in CodeFlow

@nareshjo
nareshjo requested a review from a team as a code owner October 28, 2026 01:50
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.30648%. Comparing base (c37f09d) to head (e69a766).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #15319         +/-   ##
===================================================
+ Coverage   37.84861%   39.30648%   +1.45786%     
===================================================
  Files            246         246                 
  Lines           9789        9805         +16     
  Branches        1031        1032          +1     
===================================================
+ Hits            3705        3854        +149     
+ Misses          5920        5775        -145     
- Partials         164         176         +12     
Flag Coverage Δ
Debug 39.30648% <100.00000%> (+1.45786%) ⬆️
production 41.68583% <100.00000%> (+1.63974%) ⬆️
test 20.64923% <ø> (ø)
unit 41.68583% <100.00000%> (+1.63974%) ⬆️

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.

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.

🟢 Approval recommended

No active review issues are reported for the clipboard data-store synchronization change.

No open issues 🎉

What changed in this PR

This PR addresses a dictionary-corruption race in the clipboard data store shared by WinForms and WPF, which can cause concurrent clipboard operations to fail or hang.

Changes:

  • Adds per-store synchronization for dictionary access and coherent entry snapshots, keeping validation, deserialization, and format mapping outside the lock.
  • Adds regression tests for concurrent access, callback reentrancy, format compatibility, and native storage-medium ownership.
  • Documents the synchronization boundary and unchanged payload-ownership rules.
File Description
src/​System.Private.Windows.Core/​tests/​System.Private.Windows.Core.Tests/​System/​Private/​Windows/​Ole/​DataStoreTests.cs Adds concurrency, reentrancy, compatibility, and native ownership regression tests.
src/​System.Private.Windows.Core/​src/​System/​Private/​Windows/​Ole/​DataStore.cs Synchronizes dictionary operations and preserves first-entry insertion after validation.
docs/​shared-wpf-infrastructure.md Documents the store gate, callback restrictions, and payload-lifetime boundary.

Review effort: Balanced

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

@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.

Thanks @nareshjo

@Shyam-Gupta
Shyam-Gupta merged commit 069fde5 into dotnet:main Oct 28, 2026
10 checks passed
@Shyam-Gupta

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

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

@Shyam-Gupta

Copy link
Copy Markdown
Member

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

@Shyam-Gupta backporting to release/10.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Prevent clipboard hangs during concurrent access
Using index info to reconstruct a base tree...
A	docs/shared-wpf-infrastructure.md
M	src/System.Private.Windows.Core/src/System/Private/Windows/Ole/DataStore.cs
Falling back to patching base and 3-way merge...
CONFLICT (modify/delete): docs/shared-wpf-infrastructure.md deleted in HEAD and modified in Prevent clipboard hangs during concurrent access.  Version Prevent clipboard hangs during concurrent access of docs/shared-wpf-infrastructure.md left in tree.
Auto-merging src/System.Private.Windows.Core/src/System/Private/Windows/Ole/DataStore.cs
CONFLICT (content): Merge conflict in src/System.Private.Windows.Core/src/System/Private/Windows/Ole/DataStore.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Prevent clipboard hangs during concurrent access
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

Shyam-Gupta added a commit that referenced this pull request Oct 29, 2026
…ccess (#15334)

Fixes
[3084829](https://devdiv.visualstudio.com/DevDiv/_workitems/edit/3084829)

Backport of #15319 to release/10.0

/cc @Shyam-Gupta @nareshjo

## Root Cause

The clipboard data store reads, writes, and enumerates its format
dictionary without synchronization. Concurrent mutation can corrupt
dictionary state; on .NET Framework, a corrupted bucket chain can cause
a later lookup to loop indefinitely.

## Proposed changes

- Protect the clipboard data store's format dictionary against
overlapping managed calls and COM callbacks.
- Capture complete entries and coherent format snapshots while keeping
validation, deserialization, and format mapping outside the per-store
gate.
- Cover concurrent insertion and overwrites, callback reentrancy, format
compatibility, and native storage-medium ownership with regression
tests.

## Customer Impact

- The synchronization gap was identified while investigating
customer-reported clipboard hangs. This change addresses a
dictionary-corruption race that can cause clipboard operations to fail
or hang.
- Existing format mapping and native drag-and-drop ownership behavior
are preserved; no public API changes.

## Regression?

- Unconfirmed. The unsynchronized dictionary predates this change; an
introducing release has not been established.

## Risk

Low expected compatibility risk. The per-store gate protects dictionary
access and snapshot creation; validation, deserialization, format
mapping, and explicit COM calls stay outside locked sections. Payload
ownership is unchanged. Lock contention and larger snapshots add
potential performance costs.

## Test methodology

- Exercise colliding concurrent insertions, concurrent reads and
overwrites, complete data/flag publication, and coherent format
enumeration.
- Verify validation and deserialization callbacks can wait for another
thread to access the store. Check format mapping/order, validation
failures, serializable first-entry behavior, and native-medium
ownership.
- Rebuild the affected test project with the repository's Arcade
workflow in Debug and Release. Run the final DataStore regression tests
on both targets: 10 Framework cases and 11 modern .NET cases per
configuration, totaling 42 passes with no failures or skips.
- Exercise the exact timeout-cleanup block in an isolated Framework
harness with completed, cancellable, and delayed workers. All three
checks passed; this checks cleanup behavior, not product COM contention.
- Basic copy/paste and undo smoke checks with a consumer using the same
production fix completed without observed failures. The original
intermittent hang has not been reproduced and verified fixed.
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/15334)
Shyam-Gupta added a commit that referenced this pull request Oct 29, 2026
…ccess (#15329)

Fixes
[3084829](https://devdiv.visualstudio.com/DevDiv/_workitems/edit/3084829)

Backport of #15319 to release/11.0

/cc @Shyam-Gupta @nareshjo

## Root Cause

The clipboard data store reads, writes, and enumerates its format
dictionary without synchronization. Concurrent mutation can corrupt
dictionary state; on .NET Framework, a corrupted bucket chain can cause
a later lookup to loop indefinitely.

## Proposed changes

- Protect the clipboard data store's format dictionary against
overlapping managed calls and COM callbacks.
- Capture complete entries and coherent format snapshots while keeping
validation, deserialization, and format mapping outside the per-store
gate.
- Cover concurrent insertion and overwrites, callback reentrancy, format
compatibility, and native storage-medium ownership with regression
tests.

## Customer Impact

- The synchronization gap was identified while investigating
customer-reported clipboard hangs. This change addresses a
dictionary-corruption race that can cause clipboard operations to fail
or hang.
- Existing format mapping and native drag-and-drop ownership behavior
are preserved; no public API changes.

## Regression?

- Unconfirmed. The unsynchronized dictionary predates this change; an
introducing release has not been established.

## Risk

Low expected compatibility risk. The per-store gate protects dictionary
access and snapshot creation; validation, deserialization, format
mapping, and explicit COM calls stay outside locked sections. Payload
ownership is unchanged. Lock contention and larger snapshots add
potential performance costs.

## Test methodology

- Exercise colliding concurrent insertions, concurrent reads and
overwrites, complete data/flag publication, and coherent format
enumeration.
- Verify validation and deserialization callbacks can wait for another
thread to access the store. Check format mapping/order, validation
failures, serializable first-entry behavior, and native-medium
ownership.
- Rebuild the affected test project with the repository's Arcade
workflow in Debug and Release. Run the final DataStore regression tests
on both targets: 10 Framework cases and 11 modern .NET cases per
configuration, totaling 42 passes with no failures or skips.
- Exercise the exact timeout-cleanup block in an isolated Framework
harness with completed, cancellable, and delayed workers. All three
checks passed; this checks cleanup behavior, not product COM contention.
- Basic copy/paste and undo smoke checks with a consumer using the same
production fix completed without observed failures. The original
intermittent hang has not been reproduced and verified fixed.

## Test environment(s)

- Windows x64, Debug and Release.
- .NET Framework 4.8.1 target (`net481`).
- Modern target: `net11.0-windows7.0`, executed using the
repository-selected .NET runtime `12.0.0-alpha.1.26519.114`.
- Repository-local .NET SDK: `12.0.100-alpha.1.26480.102`.

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/15329)
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 30, 2026
nialanet pushed a commit to nialanet/winforms that referenced this pull request Nov 7, 2026
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.

3 participants