From e7c2b42bd22962889983359460826b202c0ca8db Mon Sep 17 00:00:00 2001 From: Cucumberrbob <128094686+Cucumberrbob@users.noreply.github.com> Date: Wed, 14 May 2025 14:05:33 +0100 Subject: [PATCH] lock RealDebridUpdateLock when dequeuing, not when adding to the queue --- server/RdtClient.Service/Services/Torrents.cs | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index 1e200bf..59ce8e8 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -227,15 +227,24 @@ public class Torrents( logger.LogDebug("Adding {hash} to debrid provider {torrentInfo}", torrent.Hash, torrent.ToLog()); - var id = torrent.IsFile - ? await TorrentClient.AddFile(Convert.FromBase64String(torrent.FileOrMagnet)) - : await TorrentClient.AddMagnet(torrent.FileOrMagnet); + await RealDebridUpdateLock.WaitAsync(); - await torrentData.UpdateRdId(torrent, id); + try + { + var id = torrent.IsFile + ? await TorrentClient.AddFile(Convert.FromBase64String(torrent.FileOrMagnet)) + : await TorrentClient.AddMagnet(torrent.FileOrMagnet); - await UpdateTorrentClientData(torrent); + await torrentData.UpdateRdId(torrent, id); - return torrent; + await UpdateTorrentClientData(torrent); + + return torrent; + } + finally + { + RealDebridUpdateLock.Release(); + } } public async Task> GetAvailableFiles(String hash) @@ -727,30 +736,21 @@ public class Torrents( Boolean isFile, Torrent torrent) { - await RealDebridUpdateLock.WaitAsync(); + var existingTorrent = await torrentData.GetByHash(infoHash); - try + if (existingTorrent != null) { - var existingTorrent = await torrentData.GetByHash(infoHash); - - if (existingTorrent != null) - { - return existingTorrent; - } - - var newTorrent = await torrentData.Add(null, - infoHash, - fileOrMagnetContents, - isFile, - torrent.DownloadClient, - torrent); - - return newTorrent; - } - finally - { - RealDebridUpdateLock.Release(); + return existingTorrent; } + + var newTorrent = await torrentData.Add(null, + infoHash, + fileOrMagnetContents, + isFile, + torrent.DownloadClient, + torrent); + + return newTorrent; } public async Task Update(Torrent torrent)