Skip to content

Commit eff11fe

Browse files
mitchdennyCopilot
andauthored
Error handling for GetCapabilitiesAsync connection issues (#8614)
* Error out when GetCapabiltiesAsync is missing or unexpected exception occurs. * Update src/Aspire.Cli/DotNetCliRunner.cs Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent e5d5aa9 commit eff11fe

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/Aspire.Cli/Backchannel/AppHostBackchannel.cs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,38 +88,49 @@ await rpc.InvokeWithCancellationAsync(
8888

8989
public async Task ConnectAsync(Process process, string socketPath, CancellationToken cancellationToken)
9090
{
91-
using var activity = _activitySource.StartActivity();
92-
93-
_process = process;
94-
95-
if (_rpcTaskCompletionSource.Task.IsCompleted)
91+
try
9692
{
97-
throw new InvalidOperationException("Already connected to AppHost backchannel.");
93+
using var activity = _activitySource.StartActivity();
94+
95+
_process = process;
96+
97+
if (_rpcTaskCompletionSource.Task.IsCompleted)
98+
{
99+
throw new InvalidOperationException("Already connected to AppHost backchannel.");
100+
}
101+
102+
logger.LogDebug("Connecting to AppHost backchannel at {SocketPath}", socketPath);
103+
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
104+
var endpoint = new UnixDomainSocketEndPoint(socketPath);
105+
await socket.ConnectAsync(endpoint, cancellationToken);
106+
logger.LogDebug("Connected to AppHost backchannel at {SocketPath}", socketPath);
107+
108+
var stream = new NetworkStream(socket, true);
109+
var rpc = JsonRpc.Attach(stream, target);
110+
111+
var capabilities = await rpc.InvokeWithCancellationAsync<string[]>(
112+
"GetCapabilitiesAsync",
113+
Array.Empty<object>(),
114+
cancellationToken);
115+
116+
if (!capabilities.Any(s => s == "baseline.v0"))
117+
{
118+
throw new AppHostIncompatibleException(
119+
$"AppHost is incompatible with the CLI. The AppHost must be updated to a version that supports the baseline.v0 capability.",
120+
"baseline.v0"
121+
);
122+
}
123+
124+
_rpcTaskCompletionSource.SetResult(rpc);
98125
}
99-
100-
logger.LogDebug("Connecting to AppHost backchannel at {SocketPath}", socketPath);
101-
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
102-
var endpoint = new UnixDomainSocketEndPoint(socketPath);
103-
await socket.ConnectAsync(endpoint, cancellationToken);
104-
logger.LogDebug("Connected to AppHost backchannel at {SocketPath}", socketPath);
105-
106-
var stream = new NetworkStream(socket, true);
107-
var rpc = JsonRpc.Attach(stream, target);
108-
109-
var capabilities = await rpc.InvokeWithCancellationAsync<string[]>(
110-
"GetCapabilitiesAsync",
111-
Array.Empty<object>(),
112-
cancellationToken);
113-
114-
if (!capabilities.Any(s => s == "baseline.v0"))
126+
catch (RemoteMethodNotFoundException ex)
115127
{
128+
logger.LogError(ex, "Failed to connect to AppHost backchannel. The AppHost must be updated to a version that supports the baseline.v0 capability.");
116129
throw new AppHostIncompatibleException(
117130
$"AppHost is incompatible with the CLI. The AppHost must be updated to a version that supports the baseline.v0 capability.",
118131
"baseline.v0"
119132
);
120133
}
121-
122-
_rpcTaskCompletionSource.SetResult(rpc);
123134
}
124135

125136
public async Task<string[]> GetPublishersAsync(CancellationToken cancellationToken)

src/Aspire.Cli/DotNetCliRunner.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ private async Task StartBackchannelAsync(Process process, string socketPath, Tas
486486

487487
throw;
488488
}
489+
catch (Exception ex)
490+
{
491+
logger.LogError(ex, "An unexpected error occurred while trying to connect to the backchannel.");
492+
backchannelCompletionSource.SetException(ex);
493+
throw;
494+
}
489495

490496
} while (await timer.WaitForNextTickAsync(cancellationToken));
491497
}

0 commit comments

Comments
 (0)