aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-03-27 18:44:58 -0500
committerGitHub <noreply@github.com>2025-03-27 18:44:58 -0500
commiteec6e0f098effcb9bfda297a441ca7ffd8d33934 (patch)
tree1f5a821c0b30b4f48dc4b3e8df9be8104a635179 /src/slskd
parentee0d568380cdcabee168668c2ba2737077c4d401 (diff)
parent891d221e2e9833e2798a294bbdc2e7bdee6fd14a (diff)
downloadslskd-eec6e0f098effcb9bfda297a441ca7ffd8d33934.tar.gz
Merge pull request #1310 from slskd/search-hanging-maybe
Refactor search logic to help prevent 'hanging' records
Diffstat (limited to 'src/slskd')
-rw-r--r--src/slskd/Application.cs6
-rw-r--r--src/slskd/Search/API/Controllers/SearchesController.cs3
-rw-r--r--src/slskd/Search/SearchService.cs145
-rw-r--r--src/slskd/slskd.csproj2
4 files changed, 92 insertions, 64 deletions
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
/// <param name="token">The unique token for the request, supplied by the requesting user.</param>
/// <param name="directory">The requested directory.</param>
/// <returns>A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory.</returns>
- private async Task<Soulseek.Directory> DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory)
+ private async Task<IEnumerable<Soulseek.Directory>> 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)
{
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);
}
/// <summary>
diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs
index b4864918..ac99ee2e 100644
--- a/src/slskd/Search/SearchService.cs
+++ b/src/slskd/Search/SearchService.cs
@@ -239,41 +239,54 @@ namespace slskd.Search
StartedAt = DateTime.UtcNow,
};
- using var context = ContextFactory.CreateDbContext();
- context.Add(search);
- context.SaveChanges();
+ bool searchCreated = false;
+ bool searchBroadcasted = false;
- await SearchHub.BroadcastCreateAsync(search);
+ try
+ {
+ using var context = ContextFactory.CreateDbContext();
+ context.Add(search);
+ context.SaveChanges();
- // initialize the list of responses that we'll use to accumulate them
- // populated by the responseHandler we pass to SearchAsync
- List<SearchResponse> responses = new();
+ searchCreated = true;
- 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);
- }));
+ 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<SearchResponse> responses = new();
+
+ options ??= new SearchOptions();
+ 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(() =>
+ {
+ // 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(
@@ -284,47 +297,56 @@ 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);
- }
}
});
+ // 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)
@@ -333,11 +355,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;
}
diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj
index 0a282645..0a5d0683 100644
--- a/src/slskd/slskd.csproj
+++ b/src/slskd/slskd.csproj
@@ -67,7 +67,7 @@
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="7.1.1" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />
- <PackageReference Include="Soulseek" Version="6.5.0" />
+ <PackageReference Include="Soulseek" Version="7.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>