diff --git a/src/Nexus/Services/MemoryTracker.cs b/src/Nexus/Services/MemoryTracker.cs index 7f3f45d0..d31ee53b 100644 --- a/src/Nexus/Services/MemoryTracker.cs +++ b/src/Nexus/Services/MemoryTracker.cs @@ -37,6 +37,7 @@ internal class MemoryTracker : IMemoryTracker private readonly DataOptions _dataOptions; private readonly List _retrySemaphores = []; private readonly ILogger _logger; + private readonly Lock _lock = new(); public MemoryTracker(IOptions dataOptions, ILogger logger) { @@ -59,7 +60,7 @@ public async Task RegisterAllocationAsync(long minimumBy while (true) { // get exclusive access to _consumedBytes and _retrySemaphores - lock (this) + lock (_lock) { var fractionOfRemainingBytes = _consumedBytes >= _dataOptions.TotalBufferMemoryConsumption ? 0 @@ -107,7 +108,7 @@ public async Task RegisterAllocationAsync(long minimumBy public void UnregisterAllocation(AllocationRegistration allocationRegistration) { // get exclusive access to _consumedBytes and _retrySemaphores - lock (this) + lock (_lock) { _logger.LogTrace("Release {ByteCount} bytes ({MegaByteCount} MB)", allocationRegistration.ActualByteCount, allocationRegistration.ActualByteCount / 1024 / 1024); SetConsumedBytesAndTriggerWaitingTasks(-allocationRegistration.ActualByteCount); diff --git a/tests/Nexus.Tests/Other/OptionsTests.cs b/tests/Nexus.Tests/Other/OptionsTests.cs index 7379fec2..910cab36 100644 --- a/tests/Nexus.Tests/Other/OptionsTests.cs +++ b/tests/Nexus.Tests/Other/OptionsTests.cs @@ -9,7 +9,7 @@ namespace Other; public class OptionsTests { - private static readonly object _lock = new(); + private static readonly Lock _lock = new(); [InlineData(GeneralOptions.Section, typeof(GeneralOptions))] [InlineData(DataOptions.Section, typeof(DataOptions))]