aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Application.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-01-20 21:06:52 -0600
committerGitHub <noreply@github.com>2025-01-20 21:06:52 -0600
commita5da8ff17f352eea5ce29cde51d6448c5e2a2f4c (patch)
tree86e25f9271fd74e2020804ad5a87b9a64255797c /src/slskd/Application.cs
parentb18f7b00bc1d438bfa78ff16cb1badf6fe255e97 (diff)
parent301890c323e2248a97c7340d403288d2e6cc4d35 (diff)
downloadslskd-a5da8ff17f352eea5ce29cde51d6448c5e2a2f4c.tar.gz
Merge pull request #1281 from slskd/search-cleanup
Clean up dangling Search records on startup
Diffstat (limited to 'src/slskd/Application.cs')
-rw-r--r--src/slskd/Application.cs28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 05f62902..8453386b 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -307,12 +307,14 @@ namespace slskd
{
Log.Information("Application started");
+ Log.Debug("Cleaning up any dangling records");
+
// if the application shut down "uncleanly", transfers may need to be cleaned up. we deliberately don't allow these
// records to be updated if the application has started to shut down so that we can do this cleanup and properly
// disposition them as having failed due to an application shutdown, instead of some random exception thrown while
// things are being disposed.
var activeUploads = Transfers.Uploads.List(t => !t.State.HasFlag(TransferStates.Completed), includeRemoved: false)
- .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/dotnet/efcore/issues/10434
+ .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/slskd/slskd/issues/1280
.ToList();
foreach (var upload in activeUploads)
@@ -324,7 +326,7 @@ namespace slskd
}
var activeDownloads = Transfers.Downloads.List(t => !t.State.HasFlag(TransferStates.Completed) && !t.Removed)
- .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/dotnet/efcore/issues/10434
+ .Where(t => !t.State.HasFlag(TransferStates.Completed)) // https://github.com/slskd/slskd/issues/1280
.ToList();
foreach (var download in activeDownloads)
@@ -335,6 +337,28 @@ namespace slskd
Transfers.Downloads.Update(download);
}
+ /*
+ search records can be left 'dangling' as well. responses are held in memory and saved to the database
+ when the search is complete, so when a search 'dangles' all responses have been lost. to avoid discrepancies,
+ we need to zero the response and file counts as well as set the state and EndedAt.
+ */
+ var activeSearches = (await Search.ListAsync(s => s.EndedAt == null || !s.State.HasFlag(SearchStates.Completed)))
+ .Where(s => s.EndedAt == null || !s.State.HasFlag(SearchStates.Completed)) // https://github.com/slskd/slskd/issues/1280
+ .ToList();
+
+ foreach (var search in activeSearches)
+ {
+ Log.Debug("Cleaning up dangling search {Query} started {StartedAt}", search.SearchText, search.StartedAt);
+ search.Responses = [];
+ search.ResponseCount = 0;
+ search.FileCount = 0;
+ search.LockedFileCount = 0;
+ search.State = SearchStates.Completed | SearchStates.TimedOut;
+ search.EndedAt = DateTime.UtcNow;
+
+ Search.Update(search);
+ }
+
// save the ids of any downloads that were active, so we can re-enqueue them after we've connected and logged in.
// we need to check the database before we re-request to make sure the user didn't remove them from the UI while
// the application was running, but before it was logged in. so just save the ids.