From 65dee618a61399dca965ba5cb17cddbf2baff75d Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Sat, 21 Jan 2023 20:20:41 -0500 Subject: [PATCH] fix(remotecontrol): Explicitly await all connection tasks to avoid unobserved tasks exceptions --- .../RemoteControlClient.cs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Uno.UI.RemoteControl/RemoteControlClient.cs b/src/Uno.UI.RemoteControl/RemoteControlClient.cs index feed5904822c..dce88b571135 100644 --- a/src/Uno.UI.RemoteControl/RemoteControlClient.cs +++ b/src/Uno.UI.RemoteControl/RemoteControlClient.cs @@ -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(); @@ -190,6 +190,8 @@ Uri BuildServerUri() } } + CleanupConnections(connections.Select(c => c.task)); + if (completed == timeout) { if (this.Log().IsEnabled(LogLevel.Error)) @@ -234,6 +236,25 @@ Uri BuildServerUri() } } + /// + /// Cleanup connections to avoid tasks raising UnobservedTaskException. + /// + private static void CleanupConnections(IEnumerable connections) + => _ = Task.Run(async () => + { + foreach (var connection in connections) + { + try + { + await connection; + } + catch + { + // Exceptions are not used here. + } + } + }); + private async Task ProcessMessages() { _ = InitializeServerProcessors();