Fixed issue with null reference when obtaining a lock using a detached scope.

This commit is contained in:
Andy Butland
2022-05-07 12:06:33 +02:00
parent 109d2f191e
commit 02cd139770
2 changed files with 21 additions and 3 deletions

View File

@@ -74,7 +74,9 @@ public interface IScopeProvider : ICoreScopeProvider
/// <para>A detached scope is not ambient and has no parent.</para>
/// <para>It is meant to be attached by <see cref="AttachScope"/>.</para>
/// </remarks>
// TODO: This is not actually used apart from unit tests - I'm assuming it's maybe used by Deploy?
/// <remarks>
/// This is not used by CMS but is used by Umbraco Deploy.
/// </remarks>
IScope CreateDetachedScope(
IsolationLevel isolationLevel = IsolationLevel.Unspecified,
RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,

View File

@@ -130,6 +130,8 @@ namespace Umbraco.Cms.Infrastructure.Scoping
_fscope = fileSystems.Shadow();
}
_acquiredLocks = new Queue<IDistributedLock>();
return;
}
@@ -1204,7 +1206,14 @@ namespace Umbraco.Cms.Infrastructure.Scoping
/// <param name="lockId">Lock object identifier to lock.</param>
/// <param name="timeout">TimeSpan specifying the timout period.</param>
private void ObtainReadLock(int lockId, TimeSpan? timeout)
=> _acquiredLocks!.Enqueue(_scopeProvider.DistributedLockingMechanismFactory.DistributedLockingMechanism.ReadLock(lockId, timeout));
{
if (_acquiredLocks == null)
{
throw new InvalidOperationException($"Cannot obtain a read lock as the {nameof(_acquiredLocks)} queue is null.");
}
_acquiredLocks.Enqueue(_scopeProvider.DistributedLockingMechanismFactory.DistributedLockingMechanism.ReadLock(lockId, timeout));
}
/// <summary>
/// Obtains a write lock with a custom timeout.
@@ -1212,6 +1221,13 @@ namespace Umbraco.Cms.Infrastructure.Scoping
/// <param name="lockId">Lock object identifier to lock.</param>
/// <param name="timeout">TimeSpan specifying the timout period.</param>
private void ObtainWriteLock(int lockId, TimeSpan? timeout)
=> _acquiredLocks!.Enqueue(_scopeProvider.DistributedLockingMechanismFactory.DistributedLockingMechanism.WriteLock(lockId, timeout));
{
if (_acquiredLocks == null)
{
throw new InvalidOperationException($"Cannot obtain a write lock as the {nameof(_acquiredLocks)} queue is null.");
}
_acquiredLocks.Enqueue(_scopeProvider.DistributedLockingMechanismFactory.DistributedLockingMechanism.WriteLock(lockId, timeout));
}
}
}