Skip to content

Commit

Permalink
fix(remotecontrol): Explicitly await all connection tasks to avoid un…
Browse files Browse the repository at this point in the history
…observed tasks exceptions
  • Loading branch information
jeromelaban committed Jan 22, 2023
1 parent 5c3c085 commit 65dee61
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Uno.UI.RemoteControl/RemoteControlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Uri BuildServerUri()
var cts = new CancellationTokenSource();
var task = Connect(s.endpoint, s.port, cts.Token);

return (task, cts);
return (task: task, cts: cts);
})
.ToArray();

Expand Down Expand Up @@ -190,6 +190,8 @@ Uri BuildServerUri()
}
}

CleanupConnections(connections.Select(c => c.task));

if (completed == timeout)
{
if (this.Log().IsEnabled(LogLevel.Error))
Expand Down Expand Up @@ -234,6 +236,25 @@ Uri BuildServerUri()
}
}

/// <summary>
/// Cleanup connections to avoid tasks raising UnobservedTaskException.
/// </summary>
private static void CleanupConnections(IEnumerable<Task> connections)
=> _ = Task.Run(async () =>
{
foreach (var connection in connections)
{
try
{
await connection;
}
catch
{
// Exceptions are not used here.
}
}
});

private async Task ProcessMessages()
{
_ = InitializeServerProcessors();
Expand Down

0 comments on commit 65dee61

Please sign in to comment.