From 29a158df5b94a2ffd2e4581e729a22d8793fbab3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 23 Sep 2025 18:41:38 -0500 Subject: docs, move preliminary checks outside of try/catch --- src/slskd/Transfers/Uploads/UploadService.cs | 103 +++++++++++++++------------ 1 file changed, 58 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 6e6e7ee7..84152b59 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -71,6 +71,11 @@ namespace slskd.Transfers.Uploads /// /// The transfer to upload. /// The operation context. + /// Thrown if the specified Transfer is null. + /// Thrown if the specified Transfer ID can't be found in the database. + /// Thrown if the specified Transfer is not in the Queued | Locally state. + /// Thrown if an upload matching the username and filename is already tracked by Soulseek.NET. + /// Thrown if the specified file can't be found on disk. Task UploadAsync(Transfer transfer); /// @@ -208,6 +213,11 @@ namespace slskd.Transfers.Uploads /// /// The transfer to upload. /// The operation context. + /// Thrown if the specified Transfer is null. + /// Thrown if the specified Transfer ID can't be found in the database. + /// Thrown if the specified Transfer is not in the Queued | Locally state. + /// Thrown if an upload matching the username and filename is already tracked by Soulseek.NET. + /// Thrown if the specified file can't be found on disk. public async Task UploadAsync(Transfer transfer) { var lockName = $"{nameof(UploadAsync)}:{transfer.Username}:{transfer.Filename}"; @@ -218,6 +228,47 @@ namespace slskd.Transfers.Uploads return null; } + Transfer t = transfer; + + /* + fetch an updated copy of the transfer record from the database; now that we are locked, we *should* + have exclusive access to this record + + we're not using DbContext and tracked changes here because we don't want to hold a connection + open for the duration of the upload + */ + t = Find(t => t.Id == transfer.Id) + ?? throw new TransferNotFoundException($"Transfer with ID {transfer.Id} not found"); + + if (t.State != (TransferStates.Queued | TransferStates.Locally)) + { + throw new InvalidOperationException($"Invalid starting state for upload; expected {TransferStates.Queued | TransferStates.Locally}, encountered {t.State}"); + } + + /* + Soulseek.NET keeps an internal dictionary of all transfers for the duration of the transfer logic; + check that list to see if there's already an instance of this upload in it, just in case we've gotten + out of sync somehow + */ + if (Client.Uploads.Any(u => u.Username == t.Username && u.Filename == t.Filename)) + { + throw new DuplicateTransferException("A duplicate upload of the same file to the same user is already registered"); + } + + // users with uploads must be watched so that we can keep informed of their online status, privileges, and + // statistics. this is so that we can accurately determine their effective group. + try + { + if (!Users.IsWatched(t.Username)) + { + await Users.WatchAsync(t.Username); + } + } + catch (Exception ex) + { + Log.Warning(ex, "Failed to watch user {Username}", t.Username); + } + var cts = new CancellationTokenSource(); var syncRoot = new SemaphoreSlim(1, 1); @@ -239,52 +290,19 @@ namespace slskd.Transfers.Uploads string localFilename = default; long localFileLength = default; - Transfer t = transfer; + /* + from this point forward, any exit from this method MUST result in an update to the Transfer record + in the database that sets EndedAt and ensures that the State property includes the Completed flag + + failure to do so means records will get "stuck" in the database/on the UI + additionally, any acquired locks/semaphores must be released, including (and most importantly) the queue slot + */ try { Log.Debug("Acquired lock {LockName}", lockName); Log.Information("Initializing upload {Filename} to {Username}", t.Filename, t.Username); - // users with uploads must be watched so that we can keep informed of their online status, privileges, and - // statistics. this is so that we can accurately determine their effective group. - try - { - if (!Users.IsWatched(t.Username)) - { - await Users.WatchAsync(t.Username); - } - } - catch (Exception ex) - { - Log.Warning(ex, "Failed to watch user {Username}", t.Username); - } - - /* - fetch an updated copy of the transfer record from the database; now that we are locked, we *should* - have exclusive access to this record - - we're not using DbContext and tracked changes here because we don't want to hold a connection - open for the duration of the upload - */ - t = Find(t => t.Id == transfer.Id) - ?? throw new TransferNotFoundException($"Transfer with ID {transfer.Id} not found"); - - if (t.State != (TransferStates.Queued | TransferStates.Locally)) - { - throw new TransferException($"Invalid starting state for upload; expected {TransferStates.Queued | TransferStates.Locally}, encountered {t.State}"); - } - - /* - Soulseek.NET keeps an internal dictionary of all transfers for the duration of the transfer logic; - check that list to see if there's already an instance of this upload in it, just in case we've gotten - out of sync somehow - */ - if (Client.Uploads.Any(u => u.Username == t.Username && u.Filename == t.Filename)) - { - throw new TransferException("A duplicate upload of the same file to the same user is already registered"); - } - // locate the file on disk. we checked this once already when enqueueing, but it may have moved since // can throw NotFoundException (host, localFilename, localFileLength) = await ResolveFileInfoAsync(remoteFilename: t.Filename); @@ -386,11 +404,6 @@ namespace slskd.Transfers.Uploads return transfer; } - catch (TransferNotFoundException ex) - { - Log.Error(ex, "Attempted to upload non-existent transfer {Id}", t.Id); - throw; - } catch (NotFoundException) { transfer.EndedAt = DateTime.Now; -- cgit v1.2.3