Skip to content

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

Closed

Description

@xperiandri

Current behavior 🐛

Closing a solution in Visual Studio (or otherwise disposing the Remote Control VS entry point off the UI thread) throws:

System.Runtime.InteropServices.COMException: Dispose must be called on the UI thread.

The exception is thrown from ThreadHelper.ThrowIfNotOnUIThread() inside InfoBarFactory.Dispose().

IVsInfoBar* COM objects are UI-thread-affine. InfoBarFactory.CreateAsync already marshals with SwitchToMainThreadAsync(), but Dispose() / RemoveAllInfoBars() / InfoBar.Close() only assert the UI thread and do not marshal to it.

EntryPoint.Dispose() calls _infoBarFactory?.Dispose() (and DTE event unsubscription) with no main-thread switch. It is a synchronous IDisposable.Dispose() loaded by the Uno Platform Studio VSIX via reflection (DevServerLauncher), so callers can (and do) invoke it from a thread-pool task.

Observed stack (VS 2026 Insiders, solution closing):

Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread()
Uno.UI.RemoteControl.VS.Notifications.InfoBarFactory.Dispose()
Uno.UI.RemoteControl.VS.EntryPoint.Dispose()
Studio.Extensions.Tools.DevServerLauncher.DisposeEntryPointAsync()
Studio.Extensions.Tools.DevServerLauncher.DisposeAsync()
UnoSolutionTemplate.UnoServiceProvider.DisposeAsync()
UnoSolutionTemplate.UnoPlatformPackage.OnSolutionClosing.<AnonymousMethod>__0()
System.Threading.Tasks.Task.Execute()   // thread-pool, not UI thread

Throwing site:

https://github.com/unoplatform/uno/blob/master/src/Uno.UI.RemoteControl.VS/Notifications/InfoBar.cs

internal void Dispose()
{
    ThreadHelper.ThrowIfNotOnUIThread();
    RemoveAllInfoBars();
}

Caller:

https://github.com/unoplatform/uno/blob/master/src/Uno.UI.RemoteControl.VS/EntryPoint.cs

public void Dispose()
{
    // ...
    _dte.Events.BuildEvents.OnBuildBegin -= _onBuildBeginHandler;
    // ...
    _infoBarFactory?.Dispose();
}

EntryPoint.cs also has #pragma warning disable VSTHRD010 / VSTHRD109, which hides the analyzer that would have flagged this.

Expected behavior 🎯

Disposing EntryPoint / InfoBarFactory from any thread (including OnSolutionClosing on a thread-pool task) should marshal InfoBar / DTE teardown onto the UI thread and complete without throwing. Info bars should be closed; COM objects should not be touched off the main thread.

How to reproduce it (as minimally and precisely as possible) 🔬

  1. Install the Uno Platform Visual Studio extension (Studio VSIX) and open an Uno solution so Remote Control / DevServer is loaded (Uno.UI.RemoteControl.VS.EntryPoint).
  2. Have the debugger attached to devenv (or wait for the unhandled exception).
  3. Close the solution (or close VS).
  4. Observe COMException from InfoBarFactory.Dispose on a worker thread.

No Uno app-head change is required; this is IDE-extension teardown.

Workaround 🛠️

None for users. Avoid leaving InfoBars open does not help: ThrowIfNotOnUIThread() runs before RemoveAllInfoBars().

Renderer 🎨

N/A (Visual Studio extension, not an Uno app renderer).

Affected platforms 📱💻🖥️

  • Visual Studio (Windows) — Uno.UI.RemoteControl.VS / Uno Platform Studio VSIX
  • Not an application runtime issue

Uno.Sdk version (and other relevant versions) 📦

  • Code: unoplatform/uno src/Uno.UI.RemoteControl.VS
  • Loaded by Uno Platform Studio (Studio.Extensions.dll → DevServerLauncher)
  • Reproduced while debugging devenv.exe

IDE version 🧑‍💻

Visual Studio Enterprise 2026 18.10.0-insiders

Proposed solution

The VS add-in (EntryPoint) is documented as being constructed/disposed by DevServerLauncher via Assembly.LoadFrom + Activator.CreateInstance (see specs/001-fast-devserver-startup). That path does not guarantee the UI thread, so Dispose must marshal itself.

1. Marshal in InfoBarFactory.Dispose (required)

CreateAsync already uses SwitchToMainThreadAsync. Dispose should do the same instead of throwing:

internal void Dispose()
{
    if (ThreadHelper.CheckAccess())
    {
        RemoveAllInfoBars();
        return;
    }

    ThreadHelper.JoinableTaskFactory.Run(async () =>
    {
        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
        RemoveAllInfoBars();
    });
}

Keep ThrowIfNotOnUIThread() on RemoveAllInfoBars() / InfoBar.Close() — those stay UI-only; only the public dispose entry point marshals.

2. Marshal in EntryPoint.Dispose (strongly recommended)

The same method also unhooks _dte.Events.BuildEvents.*, which is COM and UI-thread-affine. Even after (1), DTE unsubscription can still throw off the UI thread.

public void Dispose()
{
    if (_isDisposed)
    {
        return;
    }
    _isDisposed = true;

    void DisposeCore()
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        try
        {
            _ct.Cancel(false);
            _dte.Events.BuildEvents.OnBuildBegin -= _onBuildBeginHandler;
            _dte.Events.BuildEvents.OnBuildDone -= _onBuildDoneHandler;
            _dte.Events.BuildEvents.OnBuildProjConfigBegin -= _onBuildProjConfigBeginHandler;
            _globalJsonObserver?.Dispose();
            _debuggerObserver?.Dispose();
            _infoBarFactory?.Dispose();
            _unoMenuCommand?.Dispose();
            _appLaunchIdeBridge?.Dispose();
            _appLaunchStateConsumer?.Dispose();
        }
        catch (Exception e)
        {
            _debugAction?.Invoke($"Failed to dispose Remote Control server: {e}");
        }
    }

    if (ThreadHelper.CheckAccess())
    {
        DisposeCore();
        return;
    }

    ThreadHelper.JoinableTaskFactory.Run(async () =>
    {
        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
        DisposeCore();
    });
}

Use JoinableTaskFactory (not Dispatcher.Invoke / ThreadHelper.Generic.Invoke) to avoid VS deadlocks.

3. Optional follow-up in uno.studio (caller)

UnoPlatformPackage.OnSolutionClosing currently fires DisposeEntryPointAsync as a bare Task (Task.Execute on the stack). That VSIX should also await JoinableTaskFactory.SwitchToMainThreadAsync() before disposing EntryPoint. That repo is not this one; uno still needs (1)+(2) because IDisposable.Dispose cannot assume the caller is on the UI thread.

4. Analyzer

Re-enable VSTHRD010 around the dispose/DTE paths (or suppress only the true false positives) so this class of bug is caught again.

Anything else we need to know? 💬

Related files:

unoplatform/studio (public) is plugins/docs only. The VSIX caller lives in the private uno.studio repo; the throwing code and the fix belong in this repository.

Activity

  1. changed the title [-][VS] InfoBarFactory.Dispose throws COMException: Dispose must be called on the UI thread[/-] [+][VS] `InfoBarFactory.Dispose` throws `COMException`: Dispose must be called on the UI thread[/+] on Aug 20, 2026
  2. self-assigned this
    on Sep 1, 2026
  3. added a commit that references this issue on Sep 14, 2026
    4a9c84b
  4. added 2 commits that reference this issue on Oct 28, 2026
    b5bdc56
    e3a3be3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions