Clean up synchronization (#11458)

This commit is contained in:
Mark Cilia Vincenti 2024-05-06 03:21:54 +02:00 committed by GitHub
parent 688a734895
commit af74aa35d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 18 deletions

View File

@ -247,7 +247,7 @@ namespace MediaBrowser.MediaEncoding.Attachments
MediaSourceInfo mediaSource,
CancellationToken cancellationToken)
{
var outputFileLocks = new List<AsyncKeyedLockReleaser<string>>();
var outputFileLocks = new List<IDisposable>();
var extractableAttachmentIds = new List<int>();
try
@ -256,16 +256,15 @@ namespace MediaBrowser.MediaEncoding.Attachments
{
var outputPath = GetAttachmentCachePath(mediaPath, mediaSource, attachment.Index);
var @outputFileLock = _semaphoreLocks.GetOrAdd(outputPath);
await @outputFileLock.SemaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);
var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false);
if (File.Exists(outputPath))
{
@outputFileLock.Dispose();
releaser.Dispose();
continue;
}
outputFileLocks.Add(@outputFileLock);
outputFileLocks.Add(releaser);
extractableAttachmentIds.Add(attachment.Index);
}
@ -280,10 +279,7 @@ namespace MediaBrowser.MediaEncoding.Attachments
}
finally
{
foreach (var @outputFileLock in outputFileLocks)
{
@outputFileLock.Dispose();
}
outputFileLocks.ForEach(x => x.Dispose());
}
}

View File

@ -457,7 +457,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
/// <returns>Task.</returns>
private async Task ExtractAllTextSubtitles(MediaSourceInfo mediaSource, CancellationToken cancellationToken)
{
var locks = new List<AsyncKeyedLockReleaser<string>>();
var locks = new List<IDisposable>();
var extractableStreams = new List<MediaStream>();
try
@ -469,16 +469,15 @@ namespace MediaBrowser.MediaEncoding.Subtitles
{
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetTextSubtitleFormat(subtitleStream));
var @lock = _semaphoreLocks.GetOrAdd(outputPath);
await @lock.SemaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);
var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false);
if (File.Exists(outputPath))
{
@lock.Dispose();
releaser.Dispose();
continue;
}
locks.Add(@lock);
locks.Add(releaser);
extractableStreams.Add(subtitleStream);
}
@ -493,10 +492,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
}
finally
{
foreach (var @lock in locks)
{
@lock.Dispose();
}
locks.ForEach(x => x.Dispose());
}
}