fix: discard stale search results when a newer query supersedes them #468

Merged
guettlibot merged 1 commits from issue-467-fix-search-stale-results into main 2026-06-06 08:32:38 +00:00
guettlibot commented 2026-06-06 07:33:28 +00:00 (Migrated from codeberg.org)

Summary

  • _onSearchChanged fired on every keystroke and launched concurrent IMAP searches via unawaited(_runSearch(...)). A slower earlier search completing after a faster later search would overwrite _searchResults with stale data — causing the wrong email to open when tapping a result (issue #467).
  • Added a _searchGeneration counter in EmailListScreen (incremented on each search start). Results are only applied when generation == _searchGeneration, so any in-flight response from a superseded query is silently discarded.
  • Clearing the search field resets state immediately without waiting for pending requests.

Test plan

  • New test: tapping first of multiple search results opens the correct email — verifies the right EmailDetailScreen opens
  • New test: stale results from a slower concurrent search are discarded — uses a Completer to simulate out-of-order IMAP responses; confirms the newer result wins even after the stale one resolves
  • All 23 widget tests pass: fvm flutter test test/widget/

Refs #467

## Summary - `_onSearchChanged` fired on every keystroke and launched concurrent IMAP searches via `unawaited(_runSearch(...))`. A slower earlier search completing after a faster later search would overwrite `_searchResults` with stale data — causing the wrong email to open when tapping a result (issue #467). - Added a `_searchGeneration` counter in `EmailListScreen` (incremented on each search start). Results are only applied when `generation == _searchGeneration`, so any in-flight response from a superseded query is silently discarded. - Clearing the search field resets state immediately without waiting for pending requests. ## Test plan - [x] New test: *tapping first of multiple search results opens the correct email* — verifies the right `EmailDetailScreen` opens - [x] New test: *stale results from a slower concurrent search are discarded* — uses a `Completer` to simulate out-of-order IMAP responses; confirms the newer result wins even after the stale one resolves - [x] All 23 widget tests pass: `fvm flutter test test/widget/` Refs #467
Sign in to join this conversation.