Skip to content

fix(vs): Marshal dispose to the UI thread - #24321

Merged
ajpinedam merged 1 commit into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.vs.dispose.ui.thread
Sep 14, 2026
Merged

ajpinedam merged 1 commit into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.vs.dispose.ui.thread

Conversation

@ajpinedam

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #24149

PR Type:

🐞 Bugfix

What changed? 🚀

Uno.UI.RemoteControl.VS.EntryPoint can be disposed from any thread — for example, the Studio VSIX (DevServerLauncher) disposes the reflection-loaded entry point from a thread-pool task while the solution is closing. The teardown, however, touches UI-thread-affine COM objects (DTE BuildEvents unsubscription, IVsInfoBar* cleanup, menu command disposal), which throws COMException when invoked off the UI thread.

Because Dispose wraps the teardown in a try/catch and _isDisposed is set up-front, the exception was swallowed and the teardown was silently aborted partway through: _unoMenuCommand, _appLaunchIdeBridge, and _appLaunchStateConsumer leaked, and open InfoBars were never removed.

This PR marshals the teardown to the UI thread:

  • EntryPoint.Dispose now takes a ThreadHelper.CheckAccess() fast path when already on the UI thread, and otherwise runs the teardown via ThreadHelper.JoinableTaskFactory.Run + SwitchToMainThreadAsync. The teardown body is extracted into DisposeCore, which keeps ThrowIfNotOnUIThread as a guard. Marshaling once at the EntryPoint level covers all child disposables.
  • InfoBarFactory.Dispose gets the same treatment (it is also reachable independently), replacing its former ThrowIfNotOnUIThread hard requirement.

PR Checklist ✅

  • 🧪 Added Runtime tests, UI tests, or a manual test sample (for bug fixes / features, if applicable) — not applicable: VS extension teardown path; validation requires a devenv + VSIX debugging session (manual verification with the reporter's scenario).
  • 📚 Docs have been added/updated following the documentation template (for bug fixes / features) — not applicable, no user-facing behavior change.
  • 🖼️ Validated PR Screenshots Compare Test Run results — not applicable, VS tooling only.
  • ❗ Contains NO breaking changes
  • 👀 Reviewed 2 other open pull requests (optional but appreciated!)

@unodevops

Copy link
Copy Markdown
Contributor

@ajpinedam
ajpinedam marked this pull request as ready for review September 1, 2026 19:33
Copilot AI lite review requested due to automatic review settings September 1, 2026 19:33

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.

🟡 Changes recommended

EntryPoint.Dispose() is now explicitly callable from any thread, but the _isDisposed guard remains non-atomic and can allow concurrent dispose execution.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes Visual Studio extension teardown failures by ensuring disposal of UI-thread-affine COM objects is marshaled onto the VS main (UI) thread, preventing COMException during solution close.

Changes:

  • Marshal InfoBarFactory.Dispose() to the UI thread when invoked from a background thread.
  • Marshal EntryPoint.Dispose() teardown to the UI thread via DisposeCore() + JoinableTaskFactory.Run/SwitchToMainThreadAsync().
File summaries
File Description
src/Uno.UI.RemoteControl.VS/Notifications/InfoBar.cs Updates InfoBarFactory.Dispose to switch to the main thread before closing/removing InfoBars.
src/Uno.UI.RemoteControl.VS/EntryPoint.cs Wraps teardown in a UI-thread marshal and extracts COM-affine cleanup into DisposeCore().
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@unodevops

Copy link
Copy Markdown
Contributor

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

Marshaling the entire COM-affine teardown with JoinableTaskFactory, with a UI-thread fast path, addresses the actual failure rather than merely removing ThrowIfNotOnUIThread. DisposeCore retains its thread guard, and InfoBarFactory's independently reachable disposal receives the same protection. The existing non-atomic disposed flag remains a separate concurrent-disposal hardening concern, not proof that this thread-affinity fix is wrong. This is Visual Studio threading, not microsoft-ui-xaml runtime behavior.

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

Approving since this will not cause any issue, but not the best fix possible

@ajpinedam
ajpinedam merged commit 06690db into unoplatform:master Sep 14, 2026
66 checks passed
@mergify

mergify Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@ajpinedam
ajpinedam deleted the dev/anpi/fix.vs.dispose.ui.thread branch September 14, 2026 03:02
@agneszitte

Copy link
Copy Markdown
Member

@Mergifyio backport servicing/6.8 release/stable/6.7

@mergify

mergify Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

backport servicing/6.8 release/stable/6.7

✅ Backports have been created

Details

agneszitte added a commit that referenced this pull request Oct 27, 2026
…r-24321

fix(vs): Marshal dispose to the UI thread (backport #24321)
agneszitte added a commit that referenced this pull request Oct 27, 2026
…6.7/pr-24321

fix(vs): Marshal dispose to the UI thread (backport #24321)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[VS] InfoBarFactory.Dispose throws COMException: Dispose must be called on the UI thread

6 participants