From e29256170c13c9740d08f5d0c8510b9cc361cdf7 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sun, 8 Feb 2026 10:41:41 -0600 Subject: [PATCH] fix(search): prevent UI freeze during search (#56) Frontend: yield to UI loop with tick() after setting search results before running background download status checks (fire-and-forget instead of blocking await). Backend: drop client mutex lock immediately after HTTP request completes so parsing and blacklist filtering don't hold the lock and block other commands. --- src-tauri/src/commands/search.rs | 73 ++++++++++++---------- src/lib/components/views/SearchView.svelte | 8 ++- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src-tauri/src/commands/search.rs b/src-tauri/src/commands/search.rs index fcc9012d9..0a10f2860 100644 --- a/src-tauri/src/commands/search.rs +++ b/src-tauri/src/commands/search.rs @@ -36,11 +36,13 @@ pub async fn search_albums( state: State<'_, AppState>, blacklist_state: State<'_, BlacklistState>, ) -> Result, String> { - let client = state.client.lock().await; - let mut results = client - .search_albums(&query, limit.unwrap_or(20), offset.unwrap_or(0), search_type.as_deref()) - .await - .map_err(|e| e.to_string())?; + let mut results = { + let client = state.client.lock().await; + client + .search_albums(&query, limit.unwrap_or(20), offset.unwrap_or(0), search_type.as_deref()) + .await + .map_err(|e| e.to_string())? + }; // Filter out albums from blacklisted artists let original_count = results.items.len(); @@ -64,11 +66,13 @@ pub async fn search_tracks( state: State<'_, AppState>, blacklist_state: State<'_, BlacklistState>, ) -> Result, String> { - let client = state.client.lock().await; - let mut results = client - .search_tracks(&query, limit.unwrap_or(20), offset.unwrap_or(0), search_type.as_deref()) - .await - .map_err(|e| e.to_string())?; + let mut results = { + let client = state.client.lock().await; + client + .search_tracks(&query, limit.unwrap_or(20), offset.unwrap_or(0), search_type.as_deref()) + .await + .map_err(|e| e.to_string())? + }; // Filter out tracks from blacklisted artists let original_count = results.items.len(); @@ -99,11 +103,13 @@ pub async fn search_artists( state: State<'_, AppState>, blacklist_state: State<'_, BlacklistState>, ) -> Result, String> { - let client = state.client.lock().await; - let mut results = client - .search_artists(&query, limit.unwrap_or(20), offset.unwrap_or(0), search_type.as_deref()) - .await - .map_err(|e| e.to_string())?; + let mut results = { + let client = state.client.lock().await; + client + .search_artists(&query, limit.unwrap_or(20), offset.unwrap_or(0), search_type.as_deref()) + .await + .map_err(|e| e.to_string())? + }; // Filter out blacklisted artists let original_count = results.items.len(); @@ -125,27 +131,30 @@ pub async fn search_all( blacklist_state: State<'_, BlacklistState>, ) -> Result { log::debug!("search_all called with query: {}", query); - let client = state.client.lock().await; // Use catalog/search endpoint which returns everything including most_popular let url = endpoints::build_url(paths::CATALOG_SEARCH); - let response: Value = client - .get_http() - .get(&url) - .header("X-App-Id", client.app_id().await.map_err(|e| e.to_string())?) - .header("X-User-Auth-Token", client.auth_token().await.map_err(|e| e.to_string())?) - .query(&[ - ("query", query.as_str()), - ("limit", "30"), - ("offset", "0"), - ]) - .send() - .await - .map_err(|e| format!("Request failed: {}", e))? - .json() - .await - .map_err(|e| format!("JSON parse failed: {}", e))?; + // Acquire lock only for HTTP request, drop before parsing + let response: Value = { + let client = state.client.lock().await; + client + .get_http() + .get(&url) + .header("X-App-Id", client.app_id().await.map_err(|e| e.to_string())?) + .header("X-User-Auth-Token", client.auth_token().await.map_err(|e| e.to_string())?) + .query(&[ + ("query", query.as_str()), + ("limit", "30"), + ("offset", "0"), + ]) + .send() + .await + .map_err(|e| format!("Request failed: {}", e))? + .json() + .await + .map_err(|e| format!("JSON parse failed: {}", e))? + }; // Parse albums let albums: SearchResultsPage = response diff --git a/src/lib/components/views/SearchView.svelte b/src/lib/components/views/SearchView.svelte index ba4d5da2c..512024deb 100644 --- a/src/lib/components/views/SearchView.svelte +++ b/src/lib/components/views/SearchView.svelte @@ -466,8 +466,9 @@ results.most_popular = allResults.most_popular; } allResults = results; + await tick(); // Yield to UI loop — render results before background work if (allResults && allResults.albums.items) { - await loadAllAlbumDownloadStatuses(allResults.albums.items); + loadAllAlbumDownloadStatuses(allResults.albums.items); // fire-and-forget } } else if (activeTab === 'albums') { const results = await invoke>('search_albums', { @@ -479,8 +480,9 @@ if (query.trim() !== searchQuery) return; albumResults = results; console.log('Album results:', albumResults); + await tick(); // Yield to UI loop if (albumResults && albumResults.items) { - await loadAllAlbumDownloadStatuses(albumResults.items); + loadAllAlbumDownloadStatuses(albumResults.items); // fire-and-forget } } else if (activeTab === 'tracks') { const results = await invoke>('search_tracks', { @@ -541,12 +543,12 @@ offset: newOffset, searchType: filterType }); - await loadAllAlbumDownloadStatuses(moreResults.items); albumResults = { ...moreResults, items: [...albumResults.items, ...moreResults.items], offset: 0 // Keep offset at 0 since we're accumulating }; + loadAllAlbumDownloadStatuses(moreResults.items); // fire-and-forget } else if (activeTab === 'tracks' && trackResults && hasMoreTracks) { const newOffset = trackResults.offset + trackResults.items.length; const moreResults = await invoke>('search_tracks', {