fix upload getting stuck

fix disposed semaphores
This commit is contained in:
rootdarkarchon
2023-11-18 17:36:17 +01:00
committed by Loporrit
parent be1edba5c2
commit 8121b8c1f6

View File

@@ -166,14 +166,26 @@ public class ServerFilesController : ControllerBase
var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash); var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
if (existingFile != null) return Ok(); if (existingFile != null) return Ok();
SemaphoreSlim fileLock; SemaphoreSlim? fileLock = null;
lock (_fileUploadLocks) bool successfullyWaited = false;
while (!successfullyWaited && !requestAborted.IsCancellationRequested)
{ {
if (!_fileUploadLocks.TryGetValue(hash, out fileLock)) lock (_fileUploadLocks)
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1); {
} if (!_fileUploadLocks.TryGetValue(hash, out fileLock))
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1);
}
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false); try
{
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false);
successfullyWaited = true;
}
catch (ObjectDisposedException)
{
_logger.LogWarning("Semaphore disposed for {hash}, recreating", hash);
}
}
try try
{ {
@@ -257,7 +269,19 @@ public class ServerFilesController : ControllerBase
} }
finally finally
{ {
fileLock.Release(); try
{
fileLock?.Release();
fileLock?.Dispose();
}
catch (ObjectDisposedException)
{
// it's disposed whatever
}
finally
{
_fileUploadLocks.TryRemove(hash, out _);
}
} }
} }
} }