Skip to content

Commit 55b5d7e

Browse files
authored
Fix task return and apply correct disposal pattern for FileSystemMainDomLock (#18147)
1 parent 1395d49 commit 55b5d7e

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/Umbraco.Infrastructure/Runtime/FileSystemMainDomLock.cs

+29-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class FileSystemMainDomLock : IMainDomLock
1515
private readonly string _lockFilePath;
1616
private readonly ILogger<FileSystemMainDomLock> _logger;
1717
private readonly string _releaseSignalFilePath;
18+
private bool _disposed;
1819
private Task? _listenForReleaseSignalFileTask;
1920

2021
private FileStream? _lockFileStream;
@@ -89,16 +90,14 @@ public Task ListenAsync()
8990
ListeningLoop,
9091
_cancellationTokenSource.Token,
9192
TaskCreationOptions.LongRunning,
92-
TaskScheduler.Default);
93+
TaskScheduler.Default)
94+
.Unwrap(); // Because ListeningLoop is an async method, we need to use Unwrap to return the inner task.
9395

9496
return _listenForReleaseSignalFileTask;
9597
}
9698

97-
public void Dispose()
98-
{
99-
_lockFileStream?.Close();
100-
_lockFileStream = null;
101-
}
99+
/// <summary>Releases the resources used by this <see cref="FileSystemMainDomLock" />.</summary>
100+
public void Dispose() => Dispose(true);
102101

103102
public void CreateLockReleaseSignalFile() =>
104103
File.Open(_releaseSignalFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite,
@@ -108,6 +107,26 @@ public void CreateLockReleaseSignalFile() =>
108107
public void DeleteLockReleaseSignalFile() =>
109108
File.Delete(_releaseSignalFilePath);
110109

110+
/// <summary>Releases the resources used by this <see cref="FileSystemMainDomLock" />.</summary>
111+
/// <param name="disposing">true to release both managed resources.</param>
112+
protected virtual void Dispose(bool disposing)
113+
{
114+
if (disposing && !_disposed)
115+
{
116+
_logger.LogDebug($"{nameof(FileSystemMainDomLock)} Disposing...");
117+
_cancellationTokenSource.Cancel();
118+
_cancellationTokenSource.Dispose();
119+
ReleaseLock();
120+
_disposed = true;
121+
}
122+
}
123+
124+
private void ReleaseLock()
125+
{
126+
_lockFileStream?.Close();
127+
_lockFileStream = null;
128+
}
129+
111130
private async Task ListeningLoop()
112131
{
113132
while (true)
@@ -118,6 +137,7 @@ private async Task ListeningLoop()
118137
{
119138
_logger.LogDebug("ListenAsync Task canceled, exiting loop");
120139
}
140+
121141
return;
122142
}
123143

@@ -127,12 +147,12 @@ private async Task ListeningLoop()
127147
{
128148
_logger.LogDebug("Found lock release signal file, releasing lock on {lockFilePath}", _lockFilePath);
129149
}
130-
_lockFileStream?.Close();
131-
_lockFileStream = null;
150+
151+
ReleaseLock();
132152
break;
133153
}
134154

135-
await Task.Delay(_globalSettings.CurrentValue.MainDomReleaseSignalPollingInterval);
155+
await Task.Delay(_globalSettings.CurrentValue.MainDomReleaseSignalPollingInterval, _cancellationTokenSource.Token);
136156
}
137157
}
138158
}

0 commit comments

Comments
 (0)