diff --git a/crates/app/src/engine/drm_free/suggestions.rs b/crates/app/src/engine/drm_free/suggestions.rs index e2685374..f866c459 100644 --- a/crates/app/src/engine/drm_free/suggestions.rs +++ b/crates/app/src/engine/drm_free/suggestions.rs @@ -153,7 +153,7 @@ impl Engine { let api = self.shared.ports.games.clone(); self.shared.runtime.spawn(async move { loop { - let (fingerprint, candidate, cached_to_persist) = { + let (candidate, cached_to_persist) = { let mut state = engine.lock(); let valid = state.local_generation == generation && state @@ -196,30 +196,47 @@ impl Engine { .candidates .iter() .find(|candidate| candidate.fingerprint == fingerprint) - .cloned(); - (fingerprint, candidate, cached) + .cloned() + .filter(|candidate| !candidate.ignored); + // Claim the title in the same critical section that pops it. Marking it in + // flight later let a concurrent request queue the same title again and search + // it twice. + let candidate = match candidate { + Some(candidate) + if candidate.suggestion.is_some() + || candidate.likely_non_game + || state.local_suggestion_searched.contains(&fingerprint) => + { + state.local_suggestion_searched.insert(fingerprint.clone()); + None + } + Some(candidate) => { + let title_key = normalize_title(&candidate.title); + if title_key.is_empty() + || state.local_suggestion_in_flight.contains(&title_key) + || state.local_suggestion_titles.contains_key(&title_key) + { + // Another step owns or already answered this title; the next + // queue fill applies its cached answer. + None + } else { + state.local_suggestion_in_flight.insert(title_key.clone()); + Some((candidate, title_key)) + } + } + None => None, + }; + (candidate, cached) }; if !cached_to_persist.is_empty() { persist_suggestions(&engine, cached_to_persist); } - let Some(candidate) = candidate else { - // The view may have been replaced while the request was queued; a later - // refresh re-queues the candidate if it is still there. + let Some((candidate, title_key)) = candidate else { + // Removed, ignored, answered or owned by another step. The view may have been + // replaced while the request was queued; a later refresh re-queues it. continue; }; - if candidate.ignored { - continue; - } - if candidate.suggestion.is_some() || candidate.likely_non_game { - engine.lock().local_suggestion_searched.insert(fingerprint); - continue; - } let title = candidate.title.clone(); - let title_key = normalize_title(&title); - engine - .lock() - .local_suggestion_in_flight - .insert(title_key.clone()); let years = years_in(&format!("{} {}", candidate.title, candidate.root)); let result = api.search_page(&credential, &title).await; let rejected = matches!(result, Err(ApiError::Rejected)); diff --git a/crates/app/src/engine/tests.rs b/crates/app/src/engine/tests.rs index 9eca042c..ae40e3f2 100644 --- a/crates/app/src/engine/tests.rs +++ b/crates/app/src/engine/tests.rs @@ -865,6 +865,19 @@ impl Harness { }) } + /// Waits for the credential write that follows `SignedIn`; it runs on the storage worker + /// after the snapshot is published, so a direct read can race it on a loaded runner. + async fn stored_credential(&self, expected: &str) -> Option { + let deadline = tokio::time::Instant::now() + Duration::from_secs(45); + loop { + let stored = self.creds.stored(); + if stored.as_deref() == Some(expected) || tokio::time::Instant::now() >= deadline { + return stored; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + } + async fn signed_out(&mut self) { self.engine.start(); self.until("signed out", |s| s.auth == AuthView::SignedOut) @@ -945,13 +958,10 @@ async fn fixture_browser_flow_signs_in_stores_and_shows_the_user() { assert_eq!(user.display_name(), "Ana"); // The credential was verified with GameNode, then stored, exactly as received. assert_eq!(*h.api.verified.lock().unwrap(), ["signed.token+/=="]); - for _ in 0..50 { - if h.creds.stored().is_some() { - break; - } - tokio::time::sleep(Duration::from_millis(20)).await; - } - assert_eq!(h.creds.stored().as_deref(), Some("signed.token+/==")); + assert_eq!( + h.stored_credential("signed.token+/==").await.as_deref(), + Some("signed.token+/==") + ); assert_eq!(snapshot.notice, None); } @@ -1007,7 +1017,7 @@ async fn reopen_failure_keeps_the_pending_attempt_and_reports_it() { .until("signed in", |s| matches!(s.auth, AuthView::SignedIn { .. })) .await; assert!(matches!(snapshot.auth, AuthView::SignedIn { .. })); - assert_eq!(h.creds.stored().as_deref(), Some("tok")); + assert_eq!(h.stored_credential("tok").await.as_deref(), Some("tok")); } #[tokio::test(flavor = "multi_thread")] @@ -1043,6 +1053,7 @@ async fn failed_replayed_and_late_callbacks_cannot_replace_the_session() { let mut h = harness(FakeApi::new(), FakeCreds::with(None)); h.signed_out().await; h.sign_in("good").await; + assert_eq!(h.stored_credential("good").await.as_deref(), Some("good")); // Replay of the consumed callback, an intruder's attempt id, and garbage. let replay = h.callback("evil"); diff --git a/crates/app/src/engine/tests/catalog.rs b/crates/app/src/engine/tests/catalog.rs index 6bc5276a..f6a5852b 100644 --- a/crates/app/src/engine/tests/catalog.rs +++ b/crates/app/src/engine/tests/catalog.rs @@ -163,9 +163,24 @@ async fn a_selected_unmatched_entry_gets_engine_fetched_suggestions_and_can_use_ .suggestions .is_empty() ); - let covers = h.api.covers_fetched.lock().unwrap(); + // The cover download is planned with the match and runs after the snapshot that shows it. + let fetched = tokio::time::timeout(std::time::Duration::from_secs(45), async { + loop { + if h.api + .covers_fetched + .lock() + .unwrap() + .iter() + .any(|url| url.ends_with("co7.jpg")) + { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + }) + .await; assert!( - covers.iter().any(|url| url.ends_with("co7.jpg")), + fetched.is_ok(), "the chosen game's cover is fetched like a match-dialog choice" ); } diff --git a/crates/app/src/engine/tests/drm_free.rs b/crates/app/src/engine/tests/drm_free.rs index 0d776a37..ffccd2d7 100644 --- a/crates/app/src/engine/tests/drm_free.rs +++ b/crates/app/src/engine/tests/drm_free.rs @@ -2799,34 +2799,46 @@ async fn local_custom_cover_commits_survives_a_match_and_cleans_up_on_replace_ca }) .await; assert!(!h.covers.path().join(&second).exists()); - { - // The in-memory match this test set is not persisted; re-apply it while checking that the - // cleared registration falls back to the GameNode cover. - let mut state = h.engine.lock(); - state.library.set_local_inference( - key.clone(), - Some(10), - Some(GameRecord { - game_id: 10, - name: "Matched".into(), - release_year: Some(2020), - cover_url: None, - cover_file: Some("10.jpg".into()), - cover_failed_at: None, - platforms: vec![], - }), - ); - } - let row = h - .engine - .snapshot() - .library - .unwrap() - .rows - .iter() - .find(|row| row.key == key) - .cloned() - .unwrap(); + // The in-memory match this test set is not persisted; re-apply it while checking that the + // cleared registration falls back to the GameNode cover. The edit above starts an inference + // for the new revision whose result, and the refresh after it, replace the in-memory match + // when they land, so re-apply until no such write is left in flight. + let row = tokio::time::timeout(Duration::from_secs(45), async { + loop { + { + let mut state = h.engine.lock(); + state.library.set_local_inference( + key.clone(), + Some(10), + Some(GameRecord { + game_id: 10, + name: "Matched".into(), + release_year: Some(2020), + cover_url: None, + cover_file: Some("10.jpg".into()), + cover_failed_at: None, + platforms: vec![], + }), + ); + } + tokio::time::sleep(Duration::from_millis(50)).await; + let row = h + .engine + .snapshot() + .library + .unwrap() + .rows + .iter() + .find(|row| row.key == key) + .cloned() + .unwrap(); + if row.cover_file.as_deref() == Some("10.jpg") { + break row; + } + } + }) + .await + .expect("the cleared registration falls back to the matched GameNode cover"); assert_eq!(row.cover_file.as_deref(), Some("10.jpg")); assert_eq!(row.game_cover_file.as_deref(), Some("10.jpg")); diff --git a/crates/app/src/plugin_host/shared_resources.rs b/crates/app/src/plugin_host/shared_resources.rs index fa154432..6b7aa784 100644 --- a/crates/app/src/plugin_host/shared_resources.rs +++ b/crates/app/src/plugin_host/shared_resources.rs @@ -908,9 +908,11 @@ mod tests { use gamenode_core::drm_free::InstallationHost as _; use std::process::{Command, Stdio}; - // A private executable identity keeps the check independent of unrelated processes. + // A private executable identity keeps the check independent of unrelated processes. The + // copy keeps the name `sleep`: multicall coreutils builds (uutils, the Ubuntu 26.04 + // default) pick the utility from the file name and exit at once under any other name. let dir = tempfile::tempdir().unwrap(); - let executable = dir.path().join("approved-runtime"); + let executable = dir.path().join("sleep"); std::fs::copy("/bin/sleep", &executable).unwrap(); let registration = gamenode_platform::installations::LinuxInstallations .inspect(executable.to_str().unwrap()) @@ -944,7 +946,7 @@ mod tests { .unwrap_or_default() .into_iter() .filter(|process| { - process.pid == child.id() || process.exe_path.contains("approved-runtime") + process.pid == child.id() || process.exe_path == registration.executable }) .map(|process| (process.pid, process.exe_path)) .collect::>();