From b267938b5dee056e92670c54f4afcf6c008d705c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 12 Jul 2025 17:39:54 -0500 Subject: set canceled state --- src/slskd/Search/SearchService.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index ac99ee2e..98001396 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -311,11 +311,10 @@ namespace slskd.Search { try { - // 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) + catch (OperationCanceledException) + { + search.State = SearchStates.Completed | SearchStates.Cancelled; + } { 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; -- cgit v1.2.3 From aedb3da5b707f0c611120918e01e017523aaa018 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 12 Jul 2025 17:40:29 -0500 Subject: eliminate ContinueWith --- 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 98001396..9181dfb3 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -342,7 +342,7 @@ namespace slskd.Search rateLimiter.Dispose(); CancellationTokens.TryRemove(id, out _); } - }); + }, cancellationToken: cancellationTokenSource.Token); // broadcast and return the _newly created_ search; it will continue to be updated in the background await SearchHub.BroadcastUpdateAsync(search); -- cgit v1.2.3 From 97a5431783e5f78be812baabadaaa65a4f15cd7d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 12 Jul 2025 17:41:20 -0500 Subject: update db before broadcast --- src/slskd/Search/SearchService.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 9181dfb3..53dec091 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -267,9 +267,10 @@ namespace slskd.Search // 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); + SearchHub.BroadcastUpdateAsync(search); + Log.Debug("Search for '{Query}' state changed: {State} (id: {Id})", query, search.State, id); }, responseReceived: (args) => rateLimiter.Invoke(() => @@ -281,6 +282,8 @@ namespace slskd.Search search.FileCount = args.Search.FileCount; search.LockedFileCount = args.Search.LockedFileCount; + Update(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); -- cgit v1.2.3 From 7f5ff0371bf920957096b18ce92a81992c0f3446 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 12 Jul 2025 17:41:38 -0500 Subject: move catch block --- src/slskd/Search/SearchService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 53dec091..7d8fcf41 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -318,8 +318,9 @@ namespace slskd.Search { search.State = SearchStates.Completed | SearchStates.Cancelled; } + catch (Exception ex) { - 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"); + Log.Error(ex, "Failed to execute search for '{Query}' (id: {Id}): {Message}", query, id, ex.Message); search.State = SearchStates.Completed | SearchStates.Errored; } -- cgit v1.2.3 From 69d039a015e2177d2713eea74b6d79fde1147ce7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 12 Jul 2025 17:41:58 -0500 Subject: add diagnostics --- src/slskd/Search/SearchService.cs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 7d8fcf41..81f37640 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -257,6 +257,7 @@ namespace slskd.Search // initialize the list of responses that we'll use to accumulate them // populated by the responseHandler we pass to SearchAsync List responses = new(); + List<(SearchStates State, DateTime Timestamp, string Source)> stateTransitions = new(); options ??= new SearchOptions(); options = options.WithActions( @@ -269,6 +270,8 @@ namespace slskd.Search search = search.WithSoulseekSearch(args.Search); Update(search); + stateTransitions.Add((args.Search.State, DateTime.UtcNow, "StateChangedDelegate")); + SearchHub.BroadcastUpdateAsync(search); Log.Debug("Search for '{Query}' state changed: {State} (id: {Id})", query, search.State, id); @@ -287,7 +290,6 @@ 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); - Update(search); })); // initiate the search. this can throw at invocation if there's a problem with @@ -304,16 +306,18 @@ namespace slskd.Search // 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 { + try + { + var soulseekSearch = await soulseekSearchTask; + stateTransitions.Add((SearchStates.None, DateTime.UtcNow, "Callsite")); // bogus entry to try and capture when it returned + + search = search.WithSoulseekSearch(soulseekSearch); + + Log.Debug("Search for '{Query}' ended normally (id: {Id})", query, id); + } catch (OperationCanceledException) { search.State = SearchStates.Completed | SearchStates.Cancelled; @@ -324,6 +328,12 @@ namespace slskd.Search search.State = SearchStates.Completed | SearchStates.Errored; } + if (!search.State.HasFlag(SearchStates.Completed)) + { + Log.Warning("Gotcha! Search for '{Search}' concluded without the Completed flag. Time is now {Timestamp}. States: {States}", query.SearchText, DateTime.UtcNow, stateTransitions); + search.State |= SearchStates.Completed; + } + search.EndedAt = DateTime.UtcNow; search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); -- cgit v1.2.3 From d4c631f853073c2eab91e428d912d931a293fd9a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 12 Jul 2025 18:15:52 -0500 Subject: docs --- src/slskd/Search/SearchService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 81f37640..a828cfa6 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -266,7 +266,7 @@ namespace slskd.Search // 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 + // faulted and we'll set the Completed flag manually in the catch block search = search.WithSoulseekSearch(args.Search); Update(search); @@ -328,6 +328,7 @@ namespace slskd.Search search.State = SearchStates.Completed | SearchStates.Errored; } + // todo: remove this once we're sure the problem is fixed if (!search.State.HasFlag(SearchStates.Completed)) { Log.Warning("Gotcha! Search for '{Search}' concluded without the Completed flag. Time is now {Timestamp}. States: {States}", query.SearchText, DateTime.UtcNow, stateTransitions); -- cgit v1.2.3 From ffb43e18f389a85a7dd4d4c3dee5faca40b90977 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 13 Jul 2025 17:20:02 -0500 Subject: refine handling of OperationCanceledException --- src/slskd/Search/SearchService.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index a828cfa6..d542b65a 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -318,14 +318,20 @@ namespace slskd.Search Log.Debug("Search for '{Query}' ended normally (id: {Id})", query, id); } - catch (OperationCanceledException) - { - search.State = SearchStates.Completed | SearchStates.Cancelled; - } catch (Exception ex) { - Log.Error(ex, "Failed to execute search for '{Query}' (id: {Id}): {Message}", query, id, ex.Message); - search.State = SearchStates.Completed | SearchStates.Errored; + // OperationCanceledException might be thrown somewhere deeper, and we don't want that to count. + // a search that was actually cancelled by the user will meet the criteria below. + if (ex is OperationCanceledException && cancellationTokenSource.Token.IsCancellationRequested) + { + Log.Information("Search for '{Query}' was cancelled"); + search.State = SearchStates.Completed | SearchStates.Cancelled; + } + else + { + Log.Error(ex, "Failed to execute search for '{Query}' (id: {Id}): {Message}", query, id, ex.Message); + search.State = SearchStates.Completed | SearchStates.Errored; + } } // todo: remove this once we're sure the problem is fixed -- cgit v1.2.3 From 2596869a91f6c9348f87bfbc6dce13eb73521a93 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 13 Jul 2025 17:21:46 -0500 Subject: docs --- src/slskd/Search/SearchService.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index d542b65a..186267d0 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -334,10 +334,12 @@ namespace slskd.Search } } - // todo: remove this once we're sure the problem is fixed + // it shouldn't be possible for this to happen, as the StateChanged callback is called before the + // SearchAsync() method returns, and we will have already updated the record at that time. if for + // some extremely odd reason that doesn't happen, make sure the record is persisted the final time + // with the Completed flag set to avoid it getting "stuck" if (!search.State.HasFlag(SearchStates.Completed)) { - Log.Warning("Gotcha! Search for '{Search}' concluded without the Completed flag. Time is now {Timestamp}. States: {States}", query.SearchText, DateTime.UtcNow, stateTransitions); search.State |= SearchStates.Completed; } -- cgit v1.2.3 From 798d2f9ff88426953c32fb29dfc72b736ddb83c4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 19 Jul 2025 15:32:43 -0500 Subject: remove state transitions, tweak logging --- src/slskd/Search/SearchService.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 186267d0..920f7e54 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -257,7 +257,6 @@ namespace slskd.Search // initialize the list of responses that we'll use to accumulate them // populated by the responseHandler we pass to SearchAsync List responses = new(); - List<(SearchStates State, DateTime Timestamp, string Source)> stateTransitions = new(); options ??= new SearchOptions(); options = options.WithActions( @@ -270,8 +269,6 @@ namespace slskd.Search search = search.WithSoulseekSearch(args.Search); Update(search); - stateTransitions.Add((args.Search.State, DateTime.UtcNow, "StateChangedDelegate")); - SearchHub.BroadcastUpdateAsync(search); Log.Debug("Search for '{Query}' state changed: {State} (id: {Id})", query, search.State, id); @@ -312,14 +309,14 @@ namespace slskd.Search try { var soulseekSearch = await soulseekSearchTask; - stateTransitions.Add((SearchStates.None, DateTime.UtcNow, "Callsite")); // bogus entry to try and capture when it returned - search = search.WithSoulseekSearch(soulseekSearch); Log.Debug("Search for '{Query}' ended normally (id: {Id})", query, id); } catch (Exception ex) { + Log.Debug(ex, "Search for '{Query}' threw {Exception}: {Message} (id: {Id})", query, ex.GetType(), ex.Message, id); + // OperationCanceledException might be thrown somewhere deeper, and we don't want that to count. // a search that was actually cancelled by the user will meet the criteria below. if (ex is OperationCanceledException && cancellationTokenSource.Token.IsCancellationRequested) @@ -329,7 +326,7 @@ namespace slskd.Search } else { - Log.Error(ex, "Failed to execute search for '{Query}' (id: {Id}): {Message}", query, id, ex.Message); + Log.Error(ex, "Failed to execute search for '{Query}': {Message}", query, ex.Message); search.State = SearchStates.Completed | SearchStates.Errored; } } @@ -353,12 +350,12 @@ namespace slskd.Search await SearchHub.BroadcastUpdateAsync(search with { Responses = [] }); Log.Debug("Search for '{Query}' finalized (id: {Id}): {Search}", query, id, search with { Responses = [] }); + Log.Information("Search for '{Query}' completed with {Responses} responses", search.ResponseCount); } catch (Exception ex) { // 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; + Log.Error(ex, "Failed to finalize search for '{Query}': {Message}", query, ex.Message); } finally { -- cgit v1.2.3