Skip to content

GetActiveTextViewAsync intermittently throws exceptions (ArgumentException and RemoteInvocationException) #550

@garrettpauls

Description

@garrettpauls

We have an in-house extension that adds a command which uses GetActiveTextViewAsync. This command works most of the time, however GetActiveTextViewAsync throws exceptions intermittently, typically after VS has been running for a while. We haven't been able to narrow down what specifically causes it to happen yet, however it has failed with two different exceptions so far:

System.ArgumentException: Document version is no longer available. Try again with newer version.
   at Microsoft.VisualStudio.Extensibility.EditorHostService.TextDocumentObserver.GetTextDocumentAsync(Int32 version, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.CreateTextDocumentAsync(Uri documentUri, Int32 version, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.CreateTextViewAsync(TextViewContract textView, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.CreateActiveTextViewAsync(IReadOnlyDictionary`2 clientContext, CancellationToken cancellationToken)
   at ExampleCommand.ExecuteCommandAsync(ClientContext context, CancellationToken cancellationToken)

and

StreamJsonRpc.RemoteInvocationException: Cannot subscribe to document, document is not open
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync[TResult](RequestId id, String targetName, IReadOnlyList`1 arguments, IReadOnlyList`1 positionalArgumentDeclaredTypes, IReadOnlyDictionary`2 namedArgumentDeclaredTypes, CancellationToken cancellationToken, Boolean isParameterObject)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.CreateDocumentObserverAsync(ITextEditorSynchronizationServiceContract syncService, Uri documentUri, Int32 version, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.GetOrCreateTextDocumentObserverAsync(Uri documentUri, Int32 version, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.CreateTextDocumentAsync(Uri documentUri, Int32 version, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.CreateTextViewAsync(TextViewContract textView, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Extensibility.EditorHostService.EditorHostServiceImpl.CreateActiveTextViewAsync(IReadOnlyDictionary`2 clientContext, CancellationToken cancellationToken)
   at ExampleCommand.ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
RPC server exception:
System.InvalidOperationException: Cannot subscribe to document, document is not open
      at Microsoft.VisualStudio.Editor.Implementation.TextEditorSynchronizationService.<SubscribeToDocumentChangesAsync>d__21.MoveNext()

I'm at a loss of how to troubleshoot these. If there's any additional details I can add to our logging to assist please let me know.

This is a simplified version of the command we're using. I've removed the logic it does for simplicity, as the error happens right when we get the active text view, however I can include more details if requested.

public class ExampleCommand : Command
{
	protected readonly TraceSource Log;

	public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
	{
		// this is exactly what the command does before the problem line, I have not removed anything here
		Log.TraceInformation("Example command starting");
		var stopwatch = Stopwatch.StartNew();

		try
		{
			var editor = context.Extensibility.Editor();

			// vvvvv this line throws exceptions periodically vvvvv
			var textView = await editor.GetActiveTextViewAsync(context, cancellationToken);
			// ^^^^^ this line throws exceptions periodically ^^^^^

			if (textView != null)
			{
				// this part works fine
			}
		}
		catch (Exception ex)
		{
			Log.TraceEvent( TraceEventType.Error, 1, "Unhandled exception: {0}", ex );
			await context.Extensibility.Shell()
				.ShowPromptAsync(
					$"Unhandled exception:\n{ex}",
					PromptOptions.OK,
					cancellationToken);
			throw;
		}
		finally
		{
			Log.TraceInformation( "Example command completed in {0}ms", stopwatch.ElapsedMilliseconds );
		}
	}

	public override CommandConfiguration CommandConfiguration { get; }
}

Both of these have occurred in VS 2026 version 18.2.1. The extension targets framework net8.0 and references:

  • Microsoft.VisualStudio.Extensibility.Build - 17.14.40608
  • Microsoft.VisualStudio.Extensibility.Sdk - 17.14.40608

Metadata

Metadata

Assignees

No one assigned

    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