From b262497083ccef7d72b9c838d37bf13706ee4faf Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sun, 28 Apr 2024 19:07:39 +0800 Subject: [PATCH 1/5] Check realm file can be written to before attempting further initialisation Rather than creating a "corrupt" realm file in such cases, the game will now refuse to start. This behaviour is usually what we want. In most cases a second click on the game will start it successfully (the previous instance's file handles are still doing stuff, or windows defender is being silly). Closes https://github.com/ppy/osu/issues/28018. --- osu.Game/Database/RealmAccess.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/osu.Game/Database/RealmAccess.cs b/osu.Game/Database/RealmAccess.cs index 167d170c81..4bc7ec4979 100644 --- a/osu.Game/Database/RealmAccess.cs +++ b/osu.Game/Database/RealmAccess.cs @@ -300,6 +300,21 @@ namespace osu.Game.Database private Realm prepareFirstRealmAccess() { + // Before attempting to initialise realm, make sure the realm file isn't locked and has correct permissions. + // + // This is to avoid failures like: + // Realms.Exceptions.RealmException: SetEndOfFile() failed: unknown error (1224) + // + // which can occur due to file handles still being open by a previous instance. + if (storage.Exists(Filename)) + { + // If this fails we allow it to block game startup. + // It's better than any alternative we can offer. + using (var _ = storage.GetStream(Filename, FileAccess.ReadWrite)) + { + } + } + string newerVersionFilename = $"{Filename.Replace(realm_extension, string.Empty)}_newer_version{realm_extension}"; // Attempt to recover a newer database version if available. @@ -321,7 +336,7 @@ namespace osu.Game.Database { Logger.Error(e, "Your local database is too new to work with this version of osu!. Please close osu! and install the latest release to recover your data."); - // If a newer version database already exists, don't backup again. We can presume that the first backup is the one we care about. + // If a newer version database already exists, don't create another backup. We can presume that the first backup is the one we care about. if (!storage.Exists(newerVersionFilename)) createBackup(newerVersionFilename); } From a4bc5a8fc9059e2f13d736965e8cc52eff95f7ea Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 29 Apr 2024 10:35:37 +0800 Subject: [PATCH 2/5] Use helper method for backup retry attempts --- osu.Game/Database/RealmAccess.cs | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/osu.Game/Database/RealmAccess.cs b/osu.Game/Database/RealmAccess.cs index 4bc7ec4979..b5faa898e7 100644 --- a/osu.Game/Database/RealmAccess.cs +++ b/osu.Game/Database/RealmAccess.cs @@ -35,6 +35,7 @@ using osu.Game.Rulesets.Mods; using osu.Game.Scoring; using osu.Game.Scoring.Legacy; using osu.Game.Skinning; +using osu.Game.Utils; using osuTK.Input; using Realms; using Realms.Exceptions; @@ -1157,33 +1158,18 @@ namespace osu.Game.Database { Logger.Log($"Creating full realm database backup at {backupFilename}", LoggingTarget.Database); - int attempts = 10; - - while (true) + FileUtils.AttemptOperation(() => { - try + using (var source = storage.GetStream(Filename, mode: FileMode.Open)) { - using (var source = storage.GetStream(Filename, mode: FileMode.Open)) - { - // source may not exist. - if (source == null) - return; + // source may not exist. + if (source == null) + return; - using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew)) - source.CopyTo(destination); - } - - return; + using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew)) + source.CopyTo(destination); } - catch (IOException) - { - if (attempts-- <= 0) - throw; - - // file may be locked during use. - Thread.Sleep(500); - } - } + }, 20); } /// From 1c1ee22aa70cfa3e92786fba7a208f2af08a2e69 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 29 Apr 2024 10:36:49 +0800 Subject: [PATCH 3/5] Add retry attempts to hopefully fix windows tests runs --- osu.Game/Database/RealmAccess.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/osu.Game/Database/RealmAccess.cs b/osu.Game/Database/RealmAccess.cs index b5faa898e7..31ae22178f 100644 --- a/osu.Game/Database/RealmAccess.cs +++ b/osu.Game/Database/RealmAccess.cs @@ -311,9 +311,12 @@ namespace osu.Game.Database { // If this fails we allow it to block game startup. // It's better than any alternative we can offer. - using (var _ = storage.GetStream(Filename, FileAccess.ReadWrite)) + FileUtils.AttemptOperation(() => { - } + using (var _ = storage.GetStream(Filename, FileAccess.ReadWrite)) + { + } + }); } string newerVersionFilename = $"{Filename.Replace(realm_extension, string.Empty)}_newer_version{realm_extension}"; From a8416f3572bfa143c6019322365211fb0df1837b Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 30 Apr 2024 12:39:18 +0800 Subject: [PATCH 4/5] Move `exists` check inside retry operation Might help? --- osu.Game/Database/RealmAccess.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/osu.Game/Database/RealmAccess.cs b/osu.Game/Database/RealmAccess.cs index 31ae22178f..465d7b15a7 100644 --- a/osu.Game/Database/RealmAccess.cs +++ b/osu.Game/Database/RealmAccess.cs @@ -307,17 +307,17 @@ namespace osu.Game.Database // Realms.Exceptions.RealmException: SetEndOfFile() failed: unknown error (1224) // // which can occur due to file handles still being open by a previous instance. - if (storage.Exists(Filename)) + // + // If this fails we allow it to block game startup. It's better than any alternative we can offer. + FileUtils.AttemptOperation(() => { - // If this fails we allow it to block game startup. - // It's better than any alternative we can offer. - FileUtils.AttemptOperation(() => + if (storage.Exists(Filename)) { using (var _ = storage.GetStream(Filename, FileAccess.ReadWrite)) { } - }); - } + } + }); string newerVersionFilename = $"{Filename.Replace(realm_extension, string.Empty)}_newer_version{realm_extension}"; From 0bfad74907be51a9755e43cad6f44db372b57f21 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 30 Apr 2024 14:09:29 +0800 Subject: [PATCH 5/5] Move realm error handling to avoid triggering in test scenarios --- osu.Game/Database/RealmAccess.cs | 38 +++++++++++++++++--------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/osu.Game/Database/RealmAccess.cs b/osu.Game/Database/RealmAccess.cs index 465d7b15a7..057bbe02e6 100644 --- a/osu.Game/Database/RealmAccess.cs +++ b/osu.Game/Database/RealmAccess.cs @@ -301,24 +301,6 @@ namespace osu.Game.Database private Realm prepareFirstRealmAccess() { - // Before attempting to initialise realm, make sure the realm file isn't locked and has correct permissions. - // - // This is to avoid failures like: - // Realms.Exceptions.RealmException: SetEndOfFile() failed: unknown error (1224) - // - // which can occur due to file handles still being open by a previous instance. - // - // If this fails we allow it to block game startup. It's better than any alternative we can offer. - FileUtils.AttemptOperation(() => - { - if (storage.Exists(Filename)) - { - using (var _ = storage.GetStream(Filename, FileAccess.ReadWrite)) - { - } - } - }); - string newerVersionFilename = $"{Filename.Replace(realm_extension, string.Empty)}_newer_version{realm_extension}"; // Attempt to recover a newer database version if available. @@ -346,6 +328,26 @@ namespace osu.Game.Database } else { + // This error can occur due to file handles still being open by a previous instance. + // If this is the case, rather than assuming the realm file is corrupt, block game startup. + if (e.Message.StartsWith("SetEndOfFile() failed", StringComparison.Ordinal)) + { + // This will throw if the realm file is not available for write access after 5 seconds. + FileUtils.AttemptOperation(() => + { + if (storage.Exists(Filename)) + { + using (var _ = storage.GetStream(Filename, FileAccess.ReadWrite)) + { + } + } + }, 20); + + // If the above eventually succeeds, try and continue startup as per normal. + // This may throw again but let's allow it to, and block startup. + return getRealmInstance(); + } + Logger.Error(e, "Realm startup failed with unrecoverable error; starting with a fresh database. A backup of your database has been made."); createBackup($"{Filename.Replace(realm_extension, string.Empty)}_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}_corrupt{realm_extension}"); }