From e5a1398849e32444fd5b959bc7c596a9a06e27e5 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Wed, 29 Jan 2025 08:17:58 +0100 Subject: [PATCH 1/2] Backport use of thread delay over sleep and handle dispose in FileSystemMainDomLock (from PRs #18119 and #18147) --- .../Runtime/FileSystemMainDomLock.cs | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs b/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs index 6dcd3ef9b0b3..d7f7985c3835 100644 --- a/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs +++ b/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs @@ -15,6 +15,7 @@ internal class FileSystemMainDomLock : IMainDomLock private readonly string _lockFilePath; private readonly ILogger _logger; private readonly string _releaseSignalFilePath; + private bool _disposed; private Task? _listenForReleaseSignalFileTask; private FileStream? _lockFileStream; @@ -88,16 +89,14 @@ public Task ListenAsync() ListeningLoop, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning, - TaskScheduler.Default); + TaskScheduler.Default) + .Unwrap(); // Because ListeningLoop is an async method, we need to use Unwrap to return the inner task. return _listenForReleaseSignalFileTask; } - public void Dispose() - { - _lockFileStream?.Close(); - _lockFileStream = null; - } + /// Releases the resources used by this . + public void Dispose() => Dispose(true); public void CreateLockReleaseSignalFile() => File.Open(_releaseSignalFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, @@ -107,7 +106,27 @@ public void CreateLockReleaseSignalFile() => public void DeleteLockReleaseSignalFile() => File.Delete(_releaseSignalFilePath); - private void ListeningLoop() + /// Releases the resources used by this . + /// true to release both managed resources. + protected virtual void Dispose(bool disposing) + { + if (disposing && !_disposed) + { + _logger.LogInformation($"{nameof(FileSystemMainDomLock)} Disposing..."); + _cancellationTokenSource.Cancel(); + _cancellationTokenSource.Dispose(); + ReleaseLock(); + _disposed = true; + } + } + + private void ReleaseLock() + { + _lockFileStream?.Close(); + _lockFileStream = null; + } + + private async Task ListeningLoop() { while (true) { @@ -131,7 +150,7 @@ private void ListeningLoop() break; } - Thread.Sleep(_globalSettings.CurrentValue.MainDomReleaseSignalPollingInterval); + await Task.Delay(_globalSettings.CurrentValue.MainDomReleaseSignalPollingInterval, _cancellationTokenSource.Token); } } } From e87e83c1f5bcab91762cbd8c877d5ccb28f2d6fe Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Wed, 12 Feb 2025 06:41:14 +0100 Subject: [PATCH 2/2] Applied suggestion from code review. --- src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs b/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs index d7f7985c3835..27662f979a1b 100644 --- a/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs +++ b/src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs @@ -145,8 +145,8 @@ private async Task ListeningLoop() { _logger.LogDebug("Found lock release signal file, releasing lock on {lockFilePath}", _lockFilePath); } - _lockFileStream?.Close(); - _lockFileStream = null; + + ReleaseLock(); break; }