aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-07-19 15:45:58 -0500
committerGitHub <noreply@github.com>2025-07-19 15:45:58 -0500
commit37e7e01427f37a93a3768e95c9a1e6a73633c6da (patch)
treef02c49b549fac57365a674d21d81ae6ea007ca35 /src
parent367b2cee4d475277fb278e6537f1014436d5a09f (diff)
parent798d2f9ff88426953c32fb29dfc72b736ddb83c4 (diff)
downloadslskd-37e7e01427f37a93a3768e95c9a1e6a73633c6da.tar.gz
Merge pull request #1395 from slskd/stuck-search
Refactor handling of searches, attempt to trap un-Completed condition and correct it
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Search/SearchService.cs59
1 files changed, 39 insertions, 20 deletions
diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs
index ac99ee2e..920f7e54 100644
--- a/src/slskd/Search/SearchService.cs
+++ b/src/slskd/Search/SearchService.cs
@@ -265,11 +265,12 @@ 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);
- 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,10 +282,11 @@ 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);
- Update(search);
}));
// initiate the search. this can throw at invocation if there's a problem with
@@ -302,23 +304,40 @@ namespace slskd.Search
// 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
{
- // 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)
+ try
+ {
+ var soulseekSearch = await soulseekSearchTask;
+ 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)
+ {
+ Log.Information("Search for '{Query}' was cancelled");
+ search.State = SearchStates.Completed | SearchStates.Cancelled;
+ }
+ else
+ {
+ Log.Error(ex, "Failed to execute search for '{Query}': {Message}", query, ex.Message);
+ search.State = SearchStates.Completed | SearchStates.Errored;
+ }
+ }
+
+ // 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.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.State |= SearchStates.Completed;
}
search.EndedAt = DateTime.UtcNow;
@@ -331,19 +350,19 @@ 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
{
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);