From 2950b16e08e4c18edb03bba299baef1faf7cb6f3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Jan 2025 20:25:59 -0600 Subject: clean up dangling searches at startup --- src/slskd/Application.cs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 05f62902..82429b5f 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -312,7 +312,7 @@ namespace slskd // 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 +324,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 +335,26 @@ 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}", search.SearchText); + search.Responses = []; + search.ResponseCount = 0; + search.FileCount = 0; + search.LockedFileCount = 0; + search.State = SearchStates.Completed | SearchStates.TimedOut; + search.EndedAt = DateTime.UtcNow; + } + // 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. -- cgit v1.2.3 From 71d8c3d233458fde02d7b169380ad347aaf88342 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Jan 2025 20:40:12 -0600 Subject: add a public Update() method, refactor to use it internally --- src/slskd/Search/SearchService.cs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 809647b2..f91714f5 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -64,6 +64,15 @@ namespace slskd.Search /// The list of searches matching the specified expression, or all searches if no expression is specified. Task> ListAsync(Expression> expression = null); + /// + /// Updates the specified . + /// + /// + /// Round-trips the database; use accordingly. + /// + /// The search to update. + void Update(Search search); + /// /// Performs a search for the specified and . /// @@ -191,6 +200,17 @@ namespace slskd.Search .ToListAsync(); } + /// + /// Updates the specified . + /// + /// The search to update. + public void Update(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Update(search); + context.SaveChanges(); + } + /// /// Performs a search for the specified and . /// @@ -223,20 +243,13 @@ namespace slskd.Search List responses = new(); - void UpdateAndSaveChanges(Search search) - { - using var context = ContextFactory.CreateDbContext(); - context.Update(search); - context.SaveChanges(); - } - options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => { search = search.WithSoulseekSearch(args.Search); SearchHub.BroadcastUpdateAsync(search); - UpdateAndSaveChanges(search); + Update(search); }, responseReceived: (args) => rateLimiter.Invoke(() => { @@ -250,7 +263,7 @@ namespace slskd.Search // note that we're not actually doing anything with the response here, that's happening in the // response handler. we're only updating counts here. SearchHub.BroadcastUpdateAsync(search); - UpdateAndSaveChanges(search); + Update(search); })); var soulseekSearchTask = Client.SearchAsync( @@ -278,7 +291,7 @@ namespace slskd.Search search.EndedAt = DateTime.UtcNow; search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); - UpdateAndSaveChanges(search); + Update(search); // zero responses before broadcasting, as we don't want to blast this // data out over the SignalR socket -- cgit v1.2.3 From 301890c323e2248a97c7340d403288d2e6cc4d35 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Jan 2025 20:42:45 -0600 Subject: log when cleaning starts, save updated search --- src/slskd/Application.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 82429b5f..8453386b 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -307,6 +307,8 @@ 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 @@ -346,13 +348,15 @@ namespace slskd foreach (var search in activeSearches) { - Log.Debug("Cleaning up dangling search {Query}", search.SearchText); + 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. -- cgit v1.2.3