Skip to content

TextStore: initialize callback state before RegisterTextStore#11457

Open
etvorun wants to merge 1 commit intodotnet:mainfrom
etvorun:fix_textstore_crash
Open

TextStore: initialize callback state before RegisterTextStore#11457
etvorun wants to merge 1 commit intodotnet:mainfrom
etvorun:fix_textstore_crash

Conversation

@etvorun
Copy link

@etvorun etvorun commented Feb 17, 2026

Summary

This fixes a TextStore attach-time reentrancy window where TSF callbacks can arrive during TextStore.OnAttach() before attach-time state is fully safe.

What changed

In TextStore.OnAttach():

  • Initialize callback-facing state (_textservicesproperty) before RegisterTextStore.
  • Subscribe TextContainer.Change before RegisterTextStore so registration-time edits update _netCharCount immediately.

Why

RegisterTextStore can synchronously re-enter WPF with third-party IMEs/TIPs. If attach-time edits happen before text-change tracking is hooked, _netCharCount can drift from TextContainer.IMECharCount, causing later consistency failures.

Validation

  • Reproduced attach/focus-time callback reentrancy with a custom native TIP test DLL.
  • Verified _netCharCount remains synchronized in this path.

Fixes #11456

Microsoft Reviewers: Open in CodeFlow

@etvorun etvorun requested review from a team and Copilot February 17, 2026 20:54
@dotnet-policy-service dotnet-policy-service bot added the PR metadata: Label to tag PRs, to facilitate with triage label Feb 17, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical reentrancy bug in the WPF TextStore's TSF (Text Services Framework) integration where third-party IMEs can trigger callbacks during RegisterTextStore, before all initialization state is ready. The fix reorders initialization in OnAttach() to ensure callback-facing state is fully initialized before TSF registration.

Changes:

  • Initialize _textservicesproperty before RegisterTextStore to prevent null dereference in TSF callbacks
  • Subscribe to TextContainer.Change before RegisterTextStore to keep _netCharCount synchronized during registration-time edits
  • Add detailed comments explaining the reentrancy scenario and specific IME that exhibits this behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@miloush
Copy link
Contributor

miloush commented Feb 17, 2026

LGTM

@h3xds1nz
Copy link
Member

Should be all these #9829 #3315 #7304 #10083 #9805

@etvorun
Copy link
Author

etvorun commented Feb 18, 2026

Should be all these #9829 #3315 #7304 #10083 #9805

#7304 does not look like the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextStore: attach-time TSF reentrancy can desync _netCharCount before consistency checks

4 participants