Fix potential oversight in semaphore release logic

This commit is contained in:
Dean Herbert 2021-11-29 18:26:37 +09:00
parent f19cfcc82e
commit 23fded4a3a

View File

@ -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;
}
}
}