From ebc8c1674bf1628e9fee885398ea02209f387fa2 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 27 Jun 2016 19:05:24 +0200 Subject: [PATCH] Prefer fully synchronous code during saving We must ensure no race condition during lock statement and exiting lock clause to run async code - Thread.Sleep() is bad, but we never expect to reach that point in normal conditions, and in faulty ones, it improves the chances of things succeeding in next try --- ArchiSteamFarm/BotDatabase.cs | 20 ++++++++++---------- ArchiSteamFarm/GlobalDatabase.cs | 16 ++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index e7f7e0a9f..b4b449a05 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -26,7 +26,7 @@ using Newtonsoft.Json; using System; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Threading.Tasks; +using System.Threading; namespace ArchiSteamFarm { internal sealed class BotDatabase { @@ -43,7 +43,7 @@ namespace ArchiSteamFarm { } _LoginKey = value; - Save().Wait(); + Save(); } } @@ -60,7 +60,7 @@ namespace ArchiSteamFarm { } _MobileAuthenticator = value; - Save().Wait(); + Save(); } } @@ -78,7 +78,7 @@ namespace ArchiSteamFarm { } _SteamGuardAccount = value; - Save().Wait(); + Save(); } } @@ -119,31 +119,31 @@ namespace ArchiSteamFarm { } FilePath = filePath; - Save().Wait(); + Save(); } // This constructor is used only by deserializer [SuppressMessage("ReSharper", "UnusedMember.Local")] private BotDatabase() { } - internal async Task Save() { + internal void Save() { string json = JsonConvert.SerializeObject(this); if (string.IsNullOrEmpty(json)) { Logging.LogNullError(nameof(json)); return; } - for (byte i = 0; i < 5; i++) { - lock (FilePath) { + lock (FilePath) { + for (byte i = 0; i < 5; i++) { try { File.WriteAllText(FilePath, json); break; } catch (Exception e) { Logging.LogGenericException(e); } - } - await Task.Delay(1000).ConfigureAwait(false); + Thread.Sleep(1000); + } } } } diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index 5c1ab5c6d..322bc0f54 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -26,7 +26,7 @@ using Newtonsoft.Json; using System; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Threading.Tasks; +using System.Threading; namespace ArchiSteamFarm { internal sealed class GlobalDatabase { @@ -40,7 +40,7 @@ namespace ArchiSteamFarm { } _CellID = value; - Save().Wait(); + Save(); } } @@ -84,31 +84,31 @@ namespace ArchiSteamFarm { } FilePath = filePath; - Save().Wait(); + Save(); } // This constructor is used only by deserializer [SuppressMessage("ReSharper", "UnusedMember.Local")] private GlobalDatabase() { } - private async Task Save() { + private void Save() { string json = JsonConvert.SerializeObject(this); if (string.IsNullOrEmpty(json)) { Logging.LogNullError(nameof(json)); return; } - for (byte i = 0; i < 5; i++) { - lock (FilePath) { + lock (FilePath) { + for (byte i = 0; i < 5; i++) { try { File.WriteAllText(FilePath, json); break; } catch (Exception e) { Logging.LogGenericException(e); } - } - await Task.Delay(1000).ConfigureAwait(false); + Thread.Sleep(1000); + } } } }