From c98451a7bf2a75af6dab4ed82818c80f72c994c8 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 29 Nov 2021 16:18:57 +0900 Subject: [PATCH] Fix potential deadlock on nested context creation requests --- osu.Game/Database/RealmContextFactory.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/osu.Game/Database/RealmContextFactory.cs b/osu.Game/Database/RealmContextFactory.cs index 2bc77934a8..08d7580db7 100644 --- a/osu.Game/Database/RealmContextFactory.cs +++ b/osu.Game/Database/RealmContextFactory.cs @@ -52,6 +52,8 @@ namespace osu.Game.Database /// private readonly SemaphoreSlim contextCreationLock = new SemaphoreSlim(1); + private bool canCreateContexts; + private static readonly GlobalStatistic refreshes = GlobalStatistics.Get(@"Realm", @"Dirty Refreshes"); private static readonly GlobalStatistic contexts_created = GlobalStatistics.Get(@"Realm", @"Contexts (Created)"); @@ -153,7 +155,13 @@ namespace osu.Game.Database try { - contextCreationLock.Wait(); + if (!canCreateContexts) + contextCreationLock.Wait(); + + // the semaphore is used to stop all context creation. + // once the semaphore has been taken by this code section, it is safe to create further contexts. + // this can happen if a realm subscription is active and triggers a callback which has user code that calls `CreateContext`. + canCreateContexts = true; contexts_created.Value++; @@ -162,6 +170,7 @@ namespace osu.Game.Database finally { contextCreationLock.Release(); + canCreateContexts = false; } }