Skip to content

Commit

Permalink
Merge pull request #17835 from heejaechang/remoteNullException
Browse files Browse the repository at this point in the history
check remote host being available.
heejaechang authored Mar 14, 2017
2 parents 5f1130c + d6939b0 commit 15ba5d4
Showing 13 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public async Task<DesignerAttributeResult> ScanDesignerAttributesAsync(Document

// same service run in both inproc and remote host, but remote host will not have RemoteHostClient service,
// so inproc one will always run
var client = await workspace.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var client = await workspace.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (client != null && !document.IsOpen())
{
// run designer attributes scanner on remote host
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ public async Task<DiagnosticAnalysisResultMap<DiagnosticAnalyzer, DiagnosticAnal
return await AnalyzeInProcAsync(analyzerDriver, project, cancellationToken).ConfigureAwait(false);
}

var remoteHostClient = await service.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var remoteHostClient = await service.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (remoteHostClient == null)
{
// remote host is not running. this can happen if remote host is disabled.
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ private static async Task<RemoteHostClient> GetRemoteHostClientAsync(Project pro
return null;
}

return await project.Solution.Workspace.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
return await project.Solution.Workspace.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ public async Task<IList<TodoComment>> GetTodoCommentsAsync(Document document, Im
{
// same service run in both inproc and remote host, but remote host will not have RemoteHostClient service,
// so inproc one will always run
var client = await document.Project.Solution.Workspace.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var client = await document.Project.Solution.Workspace.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (client != null && !document.IsOpen())
{
// run todo scanner on remote host.
Original file line number Diff line number Diff line change
@@ -130,6 +130,11 @@ public void Disable()
}

public Task<RemoteHostClient> GetRemoteHostClientAsync(CancellationToken cancellationToken)
{
return TryGetRemoteHostClientAsync(cancellationToken);
}

public Task<RemoteHostClient> TryGetRemoteHostClientAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public async Task<ReferenceCount> GetReferenceCountAsync(Solution solution, Docu
return null;
}

var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (remoteHostClient == null)
{
// remote host is not running. this can happen if remote host is disabled.
@@ -46,7 +46,7 @@ public async Task<IEnumerable<ReferenceLocationDescriptor>> FindReferenceLocatio
return null;
}

var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (remoteHostClient == null)
{
// remote host is not running. this can happen if remote host is disabled.
@@ -67,7 +67,7 @@ public async Task<IEnumerable<ReferenceMethodDescriptor>> FindReferenceMethodsAs
return null;
}

var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (remoteHostClient == null)
{
// remote host is not running. this can happen if remote host is disabled.
@@ -88,7 +88,7 @@ public async Task<string> GetFullyQualifiedName(Solution solution, DocumentId do
return null;
}

var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var remoteHostClient = await solution.Workspace.Services.GetService<IRemoteHostClientService>().TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (remoteHostClient == null)
{
// remote host is not running. this can happen if remote host is disabled.
Original file line number Diff line number Diff line change
@@ -145,7 +145,7 @@ void Method()
var asset = assetBuilder.Build(analyzerReference, CancellationToken.None);
snapshotService.AddGlobalAsset(analyzerReference, asset, CancellationToken.None);

var client = await workspace.Services.GetService<IRemoteHostClientService>().GetRemoteHostClientAsync(CancellationToken.None);
var client = await workspace.Services.GetService<IRemoteHostClientService>().TryGetRemoteHostClientAsync(CancellationToken.None);
await client.RunOnRemoteHostAsync(
WellKnownRemoteHostServices.RemoteHostService, workspace.CurrentSolution,
nameof(IRemoteHostService.SynchronizeGlobalAssetsAsync), (object)(new Checksum[] { asset.Checksum }), CancellationToken.None);
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ internal static class RazorLanguageServiceClientFactory
public static async Task<RazorLangaugeServiceClient> CreateAsync(Workspace workspace, CancellationToken cancellationToken = default(CancellationToken))
{
var clientFactory = workspace.Services.GetRequiredService<IRemoteHostClientService>();
var client = await clientFactory.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var client = await clientFactory.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
return client == null ? null : new RazorLangaugeServiceClient(client);
}
}
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ public static async Task<ISymbolSearchUpdateEngine> CreateEngineAsync(
var outOfProcessAllowed = workspace.Options.GetOption(SymbolSearchOptions.OutOfProcessAllowed);
if (outOfProcessAllowed)
{
var client = await workspace.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var client = await workspace.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (client != null)
{
return new RemoteUpdateEngine(workspace, client, logService, cancellationToken);
@@ -75,8 +75,12 @@ private async void OnConnectionChanged(object sender, bool connected)
_sessionDoNotAccessDirectly?.Dispose();
_sessionDoNotAccessDirectly = null;

_client = await _workspace.GetRemoteHostClientAsync(CancellationToken.None).ConfigureAwait(false);
_client.ConnectionChanged += OnConnectionChanged;
_client = await _workspace.TryGetRemoteHostClientAsync(CancellationToken.None).ConfigureAwait(false);
if (_client != null)
{
// client can be null if host is shutting down
_client.ConnectionChanged += OnConnectionChanged;
}
}
}

@@ -89,6 +93,12 @@ private async void OnConnectionChanged(object sender, bool connected)
return _sessionDoNotAccessDirectly;
}

if (_client == null)
{
// client can be null if host is shutting down
return null;
}

// We create a single session and use it for the entire lifetime of this process.
// That single session will be used to do all communication with the remote process.
// This is because each session will cause a new instance of the RemoteSymbolSearchUpdateEngine
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ private static async Task FindReferencesInServiceProcessAsync(
IImmutableSet<Document> documents,
CancellationToken cancellationToken)
{
var client = await solution.Workspace.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var client = await solution.Workspace.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (client == null)
{
await FindReferencesInCurrentProcessAsync(
@@ -133,7 +133,7 @@ private static async Task<bool> TryFindLiteralReferencesInServiceProcessAsync(
IStreamingFindLiteralReferencesProgress progress,
CancellationToken cancellationToken)
{
var client = await solution.Workspace.GetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
var client = await solution.Workspace.TryGetRemoteHostClientAsync(cancellationToken).ConfigureAwait(false);
if (client == null)
{
return false;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;
@@ -25,6 +26,11 @@ public RemoteHostClientService(Workspace workspace)
}

public Task<RemoteHostClient> GetRemoteHostClientAsync(CancellationToken cancellationToken)
{
return TryGetRemoteHostClientAsync(cancellationToken);
}

public Task<RemoteHostClient> TryGetRemoteHostClientAsync(CancellationToken cancellationToken)
{
if (_lazyInstance == null)
{
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
@@ -11,6 +12,8 @@ namespace Microsoft.CodeAnalysis.Remote
/// </summary>
internal interface IRemoteHostClientService : IWorkspaceService
{
[Obsolete("use TryGetRemoteHostClientAsync instead")]
Task<RemoteHostClient> GetRemoteHostClientAsync(CancellationToken cancellationToken);
Task<RemoteHostClient> TryGetRemoteHostClientAsync(CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -37,10 +37,10 @@ internal static class RemoteHostClientExtensions
WellKnownServiceHubServices.CodeAnalysisService, solution, callbackTarget, cancellationToken);
}

public static Task<RemoteHostClient> GetRemoteHostClientAsync(this Workspace workspace, CancellationToken cancellationToken)
public static Task<RemoteHostClient> TryGetRemoteHostClientAsync(this Workspace workspace, CancellationToken cancellationToken)
{
var clientService = workspace.Services.GetService<IRemoteHostClientService>();
return clientService?.GetRemoteHostClientAsync(cancellationToken);
return clientService?.TryGetRemoteHostClientAsync(cancellationToken);
}

public static Task RunOnRemoteHostAsync(

0 comments on commit 15ba5d4

Please sign in to comment.