From 23fded4a3ac22be9eb0f92a38dc173402feed386 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 29 Nov 2021 18:26:37 +0900 Subject: [PATCH] Fix potential oversight in semaphore release logic --- osu.Game/Database/RealmContextFactory.cs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/osu.Game/Database/RealmContextFactory.cs b/osu.Game/Database/RealmContextFactory.cs index b1e97a45d0..6948918fe7 100644 --- a/osu.Game/Database/RealmContextFactory.cs +++ b/osu.Game/Database/RealmContextFactory.cs @@ -153,15 +153,22 @@ namespace osu.Game.Database if (isDisposed) throw new ObjectDisposedException(nameof(RealmContextFactory)); + bool tookSemaphoreLock = false; + try { if (!currentThreadCanCreateContexts.Value) + { 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`. - currentThreadCanCreateContexts.Value = true; + tookSemaphoreLock = true; + } + else + { + // the semaphore is used to handle blocking of all context creation during certain periods. + // once the semaphore has been taken by this code section, it is safe to create further contexts on the same thread. + // this can happen if a realm subscription is active and triggers a callback which has user code that calls `CreateContext`. + currentThreadCanCreateContexts.Value = true; + } contexts_created.Value++; @@ -169,8 +176,11 @@ namespace osu.Game.Database } finally { - contextCreationLock.Release(); - currentThreadCanCreateContexts.Value = false; + if (tookSemaphoreLock) + { + contextCreationLock.Release(); + currentThreadCanCreateContexts.Value = false; + } } }