From 606bb019f7d8e1e14eea09d3e42704ca78904e89 Mon Sep 17 00:00:00 2001 From: rafael-aero Date: Fri, 5 Nov 2021 11:01:32 +0100 Subject: [PATCH] fix race condition causing unobserved exception --- ElectronNET.API/BridgeConnector.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ElectronNET.API/BridgeConnector.cs b/ElectronNET.API/BridgeConnector.cs index b0ad6e0..776c1df 100644 --- a/ElectronNET.API/BridgeConnector.cs +++ b/ElectronNET.API/BridgeConnector.cs @@ -458,8 +458,23 @@ namespace ElectronNET.API Task.Run(async () => { - await socket.ConnectAsync(); - _connectedSocketTask.TrySetResult(socket); //Probably was already on the OnConnected call + try + { + if (!socket.Connected) + { + await socket.ConnectAsync(); + _connectedSocketTask.TrySetResult(socket); //Probably was already on the OnConnected call + } + return; + } + catch (Exception e) + { + await Task.Delay(1000); + if (!socket.Connected) + { + LogError(e, "Failed to connect"); + } + } }); _socket = socket;