maps: fix lost recent searches from mirrored-snapshot race
- Commit
58f949e46270d06696dea0034d589f0036d9e9a7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 07:28 UTC (29 days ago)
- Parents
2857144
RecentSearchStore.add() did a read-modify-write through current(), which reads DataStoreUtils' asynchronously-mirrored snapshot. The mirror can lag the previous write, so a second search recorded in quick succession computed its list from the pre-first-write state and dropped the first query (and vice versa). DataStore is the source of truth here: move the read-modify-write inside one DataStore edit via the existing DataStoreUtils.updateString, which exists for exactly this hazard. Verified by StoresSlice3Test 'added queries read back most-recent first', which failed with [park] instead of [park, pizza] before this fix and passes after it. (cherry picked from commit d7a99263ee8764f811f01acf864ca643e78b068b)
Diff
maps/src/main/java/com/vayunmathur/maps/data/RecentSearchStore.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/data/RecentSearchStore.kt b/maps/src/main/java/com/vayunmathur/maps/data/RecentSearchStore.ktindex e1131ddcce..07bc672022 100644--- a/maps/src/main/java/com/vayunmathur/maps/data/RecentSearchStore.kt+++ b/maps/src/main/java/com/vayunmathur/maps/data/RecentSearchStore.kt@@ -28,8 +28,15 @@ class RecentSearchStore private constructor(private val ds: DataStoreUtils) { suspend fun add(query: String) { val q = query.trim() if (q.isEmpty()) return- val updated = (listOf(q) + current().filter { !it.equals(q, ignoreCase = true) }).take(MAX)- ds.setString(KEY, Json.encodeToString(updated))+ // Read-modify-write inside one DataStore edit: current() reads the+ // asynchronously-mirrored snapshot, which can lag the previous write,+ // so two quick searches could each drop the other's entry.+ ds.updateString(KEY) { raw ->+ val existing = decode(raw)+ Json.encodeToString(+ (listOf(q) + existing.filter { !it.equals(q, ignoreCase = true) }).take(MAX)+ )+ } } /** Clear all recent searches. */