Skip to content

Commit c9c85fa

Browse files
authored
ssh: refactor exception printing. (#427)
1 parent 75d40c3 commit c9c85fa

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
<PublishRepositoryUrl>true</PublishRepositoryUrl>
5-
<VersionPrefix>0.17.1</VersionPrefix>
5+
<VersionPrefix>0.17.2</VersionPrefix>
66
<DeterministicSourcePaths Condition="'$(Configuration)' == 'Release'">true</DeterministicSourcePaths>
77
</PropertyGroup>
88
</Project>

src/ssh/Program.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,26 @@ static async Task<int> ExecuteAsync(string destination, string[] command, bool f
132132
command.Length == 0 ? await client.ExecuteShellAsync(executeOptions)
133133
: await client.ExecuteAsync(string.Join(" ", command), executeOptions);
134134

135-
Task<int> exitCodeTask;
135+
Task<int> receiveTask = ReceiveLoop(process);
136+
Task sendTask = SendLoop(process);
136137

137-
Task[] tasks = new[]
138-
{
139-
exitCodeTask = PrintToConsole(process),
140-
ReadInputFromConsole(process)
141-
};
138+
Task.WaitAll([receiveTask, sendTask]);
142139

143-
Task.WaitAll(tasks);
144140
if (!quiet)
145141
{
146-
PrintExceptions(tasks);
142+
PrintExceptions(receiveTask, sendTask);
147143
}
148144

149-
return await exitCodeTask;
145+
if (receiveTask.IsFaulted)
146+
{
147+
return 134;
148+
}
149+
else
150+
{
151+
return await receiveTask;
152+
}
150153

151-
static async Task<int> PrintToConsole(RemoteProcess process)
154+
static async Task<int> ReceiveLoop(RemoteProcess process)
152155
{
153156
char[] buffer = new char[1024];
154157
while (true)
@@ -163,7 +166,7 @@ static async Task<int> PrintToConsole(RemoteProcess process)
163166
}
164167
}
165168

166-
static async Task ReadInputFromConsole(RemoteProcess process)
169+
static async Task SendLoop(RemoteProcess process)
167170
{
168171
using IStandardInputReader reader = CreateConsoleInReader(process.HasTerminal);
169172

@@ -198,16 +201,17 @@ static async Task ReadInputFromConsole(RemoteProcess process)
198201
{ }
199202
}
200203

201-
static void PrintExceptions(Task[] tasks)
204+
static void PrintExceptions(Task receiveTask, Task sendTask)
202205
{
203-
foreach (var task in tasks)
206+
if (receiveTask.IsFaulted)
204207
{
205-
Exception? innerException = task.Exception?.InnerException;
206-
if (innerException is not null)
207-
{
208-
Console.Error.WriteLine("Exception:");
209-
Console.Error.WriteLine(innerException);
210-
}
208+
Console.Error.WriteLine("Receive loop failed with exception:");
209+
Console.Error.WriteLine(receiveTask.Exception);
210+
}
211+
if (sendTask.IsFaulted)
212+
{
213+
Console.Error.WriteLine("Send loop failed with exception:");
214+
Console.Error.WriteLine(sendTask.Exception);
211215
}
212216
}
213217
}

0 commit comments

Comments
 (0)