From 8dddf66d1372d913ac81374af0a0e0b47e49b157 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 12 Mar 2025 18:40:15 -0500 Subject: unimportant tweak --- src/slskd/Search/API/Controllers/SearchesController.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index 58e1fe37..1d00b482 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -82,6 +82,7 @@ namespace slskd.Search.API try { search = await Searches.StartAsync(id, SearchQuery.FromText(request.SearchText), SearchScope.Network, request.ToSearchOptions()); + return Ok(search); } catch (Exception ex) when (ex is ArgumentException || ex is Soulseek.DuplicateTokenException) { @@ -98,8 +99,6 @@ namespace slskd.Search.API Log.Error(ex, "Failed to execute search {Search}: {Message}", request, ex.Message); return StatusCode(500, ex.Message); } - - return Ok(search); } /// -- cgit v1.2.3 From 3fa9c6f1a0ae11a45475e7681372154a23a844f3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 Mar 2025 17:46:36 -0500 Subject: move setup into try --- src/slskd/Search/SearchService.cs | 64 +++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index b4864918..e0466043 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -239,41 +239,41 @@ namespace slskd.Search StartedAt = DateTime.UtcNow, }; - using var context = ContextFactory.CreateDbContext(); - context.Add(search); - context.SaveChanges(); + try + { + using var context = ContextFactory.CreateDbContext(); + context.Add(search); + context.SaveChanges(); - await SearchHub.BroadcastCreateAsync(search); + await SearchHub.BroadcastCreateAsync(search); - // initialize the list of responses that we'll use to accumulate them - // populated by the responseHandler we pass to SearchAsync - List responses = new(); + // initialize the list of responses that we'll use to accumulate them + // populated by the responseHandler we pass to SearchAsync + List responses = new(); - options ??= new SearchOptions(); - options = options.WithActions( - stateChanged: (args) => - { - search = search.WithSoulseekSearch(args.Search); - SearchHub.BroadcastUpdateAsync(search); - Update(search); - }, - responseReceived: (args) => rateLimiter.Invoke(() => - { - // note: this is rate limited, but has the potential to update the database every 250ms (or whatever the - // interval is set to) for the duration of the search. any issues with disk i/o or performance while searches - // are running should investigate this as a cause - search.ResponseCount = args.Search.ResponseCount; - search.FileCount = args.Search.FileCount; - search.LockedFileCount = args.Search.LockedFileCount; - - // 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); - Update(search); - })); + options ??= new SearchOptions(); + options = options.WithActions( + stateChanged: (args) => + { + search = search.WithSoulseekSearch(args.Search); + SearchHub.BroadcastUpdateAsync(search); + Update(search); + }, + responseReceived: (args) => rateLimiter.Invoke(() => + { + // note: this is rate limited, but has the potential to update the database every 250ms (or whatever the + // interval is set to) for the duration of the search. any issues with disk i/o or performance while searches + // are running should investigate this as a cause + search.ResponseCount = args.Search.ResponseCount; + search.FileCount = args.Search.FileCount; + search.LockedFileCount = args.Search.LockedFileCount; + + // 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); + Update(search); + })); - try - { // initiate the search. this can throw at invocation if there's a problem with // the client state (e.g. disconnected) or a problem with the search (e.g. no terms) var soulseekSearchTask = Client.SearchAsync( -- cgit v1.2.3 From 45c5bd50b0f3d406ea0fb535ab3ff488e6ea8c2f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 Mar 2025 17:50:13 -0500 Subject: track progress, undo properly --- src/slskd/Search/SearchService.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index e0466043..20f9255f 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -239,14 +239,21 @@ namespace slskd.Search StartedAt = DateTime.UtcNow, }; + bool searchCreated = false; + bool searchBroadcasted = false; + try { using var context = ContextFactory.CreateDbContext(); context.Add(search); context.SaveChanges(); + searchCreated = true; + await SearchHub.BroadcastCreateAsync(search); + searchBroadcasted = true; + // initialize the list of responses that we'll use to accumulate them // populated by the responseHandler we pass to SearchAsync List responses = new(); @@ -333,11 +340,18 @@ namespace slskd.Search // the app isn't connected, and a few other straightforward issues that arise before even requesting the search Log.Error(ex, "Failed to execute search {Search}: {Message}", new { query, scope, options }, ex.Message); - search.State = SearchStates.Completed | SearchStates.Errored; - search.EndedAt = search.StartedAt; - Update(search); + // selectively 'undo' whatever actions we were able to take successfully + if (searchCreated) + { + search.State = SearchStates.Completed | SearchStates.Errored; + search.EndedAt = search.StartedAt; + Update(search); - await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); + if (searchBroadcasted) + { + await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); + } + } throw; } -- cgit v1.2.3 From 58bf88cd6acd402b7ffd9ef25cf1cc4ccaf50bbd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 Mar 2025 17:50:34 -0500 Subject: clarify final broadcast --- src/slskd/Search/SearchService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 20f9255f..8668d684 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -330,8 +330,8 @@ namespace slskd.Search } }); + // broadcast and return the _newly created_ search; it will continue to be updated in the background await SearchHub.BroadcastUpdateAsync(search); - return search; } catch (Exception ex) -- cgit v1.2.3 From fd1f0c094a130325fd901aae91297d6d3bad78ca Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 25 Mar 2025 18:34:01 -0500 Subject: bump Soulseek.NET --- src/slskd/slskd.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 0a282645..fa127429 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -67,7 +67,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive -- cgit v1.2.3 From 87323146bbca590834a72016e285c86f9a1d29b7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 25 Mar 2025 18:35:44 -0500 Subject: refactor error handling for searches --- src/slskd/Search/SearchService.cs | 61 ++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 8668d684..ac99ee2e 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -262,9 +262,15 @@ namespace slskd.Search options = options.WithActions( stateChanged: (args) => { + // this logic is executed by Soulseek.NET upon each transition of the searcn, including the final + // transition to Completed. if for some reason something goes wrong, we won't see the final Completed + // transition, we should instead see the task below throw, in which case it will become + // faulted and we'll set the Completed flag manually in the ContinueWith() block search = search.WithSoulseekSearch(args.Search); SearchHub.BroadcastUpdateAsync(search); Update(search); + + Log.Debug("Search for '{Query}' state changed: {State} (id: {Id})", query, search.State, id); }, responseReceived: (args) => rateLimiter.Invoke(() => { @@ -291,42 +297,51 @@ namespace slskd.Search options, cancellationToken: cancellationTokenSource.Token); - // seach looks ok so far; let the rest of the logic run asynchronously + // search looks ok so far; let the rest of the logic run asynchronously // on a background thread. this logic needs to clean up after itself and // update the search record to accurately reflect the final state _ = Task.Run(async () => + { + var soulseekSearch = await soulseekSearchTask; + search = search.WithSoulseekSearch(soulseekSearch); + + Log.Debug("Search for '{Query}' ended normally (id: {Id})", query, id); + }, cancellationToken: cancellationTokenSource.Token) + .ContinueWith(async task => { try { - var soulseekSearch = await soulseekSearchTask; - search = search.WithSoulseekSearch(soulseekSearch); + // the only way we should ever get to this point is if we encounter an error sending the search + // request to the server (timeout, cancelled) or hit some highly improbable error within Soulseek.NET, + // potentially OOM or something. in these cases the task will throw and we need to force the + // search record into Errored state. + if (task.IsFaulted) + { + Log.Error(task.Exception, "Failed to execute search for '{Query}' (id: {Id}): {Message}", query, id, task.Exception?.Message ?? "Task completed in Faulted state, but there is no Exception"); + search.State = SearchStates.Completed | SearchStates.Errored; + } + + search.EndedAt = DateTime.UtcNow; + search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); + + Update(search); + + // zero responses before broadcasting, as we don't want to blast this + // data out over the SignalR socket + await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); + + Log.Debug("Search for '{Query}' finalized (id: {Id}): {Search}", query, id, search with { Responses = [] }); } catch (Exception ex) { - Log.Error(ex, "Failed to execute search {Search}: {Message}", new { query, scope, options }, ex.Message); - search.State = SearchStates.Completed | SearchStates.Errored; + // record may be left 'hanging' and will need to be cleaned up at the next boot; we tried to update but failed + Log.Error(ex, "Failed to finalize search for '{Query}' (id: {Id}): {Message}", query, id, ex.Message); + throw; } finally { rateLimiter.Dispose(); CancellationTokens.TryRemove(id, out _); - - try - { - search.EndedAt = DateTime.UtcNow; - search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); - - Update(search); - - // zero responses before broadcasting, as we don't want to blast this - // data out over the SignalR socket - await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); - } - catch (Exception ex) - { - // record may be left 'hanging' and will need to be cleaned up at the next boot - Log.Error(ex, "Failed to persist search for {SearchQuery} ({Id})", query, id); - } } }); -- cgit v1.2.3 From fcc0499ca6846f42820e952663fc1b4067e11634 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 26 Mar 2025 14:59:08 -0500 Subject: fix directory contents resolver --- src/slskd/Application.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 791cf245..e5681442 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1258,18 +1258,18 @@ namespace slskd /// The unique token for the request, supplied by the requesting user. /// The requested directory. /// A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory. - private async Task DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory) + private async Task> DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory) { if (Users.IsBlacklisted(username, endpoint.Address)) { Log.Information("Returned empty directory listing for blacklisted user {Username} ({IP})", username, endpoint.Address); - return new Soulseek.Directory(directory); + return [new Soulseek.Directory(directory)]; } try { var dir = await Shares.ListDirectoryAsync(directory); - return dir; + return [dir]; } catch (Exception ex) { -- cgit v1.2.3 From 03f4e86500350ba3a939afd8e1e4e8274aac4314 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Mar 2025 18:00:25 -0500 Subject: bump Soulseek.NET to 7.0.1 --- src/slskd/slskd.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index fa127429..0a5d0683 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -67,7 +67,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive -- cgit v1.2.3 From 891d221e2e9833e2798a294bbdc2e7bdee6fd14a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Mar 2025 18:00:37 -0500 Subject: add Queued state --- src/web/src/components/Search/SearchStatusIcon.jsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/web/src/components/Search/SearchStatusIcon.jsx b/src/web/src/components/Search/SearchStatusIcon.jsx index 9658b626..0a3d4a5d 100644 --- a/src/web/src/components/Search/SearchStatusIcon.jsx +++ b/src/web/src/components/Search/SearchStatusIcon.jsx @@ -1,9 +1,9 @@ import React from 'react'; import { Icon, Popup } from 'semantic-ui-react'; -// as of 4.5.2, states are: +// as of 3/26/25 states are: // transient: -// None, Requested, InProgress +// None, Queued, Requested, InProgress // terminal: // good: Completed, [TimedOut | ResponseLimitReached | FileLimitReached] // bad: Completed, [Errored | Cancelled] @@ -11,6 +11,7 @@ import { Icon, Popup } from 'semantic-ui-react'; const getIcon = ({ state, ...props }) => { switch (state) { case 'None': + case 'Queued': case 'Requested': return (