Skip to content

Prevent stale async suggestions in SearchAnchor - #190206

Closed
cuishuang wants to merge 1 commit into
flutter:masterfrom
cuishuang:fix_data_race
Closed

cuishuang wants to merge 1 commit into
flutter:masterfrom
cuishuang:fix_data_race

Conversation

@cuishuang

@cuishuang cuishuang commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #190205

This change prevents an older asynchronous SearchAnchor.suggestionsBuilder call from replacing the results of a newer call.

It assigns a monotonically increasing call ID to suggestions requests and only applies a completed result when its ID still matches the latest request. The same protection is shared by the initial suggestions-loading path and updates triggered by changes to the SearchController.

The existing suggestions builder invocation behavior is preserved.

A regression test starts requests for a and ab, completes the ab request first, and verifies that completing the older a request does not replace the displayed ab suggestions.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages labels Jul 29, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to ignore out-of-order asynchronous suggestions in SearchAnchor by tracking a call ID (_suggestionsCallId) and refactoring suggestion retrieval into a helper method _buildSuggestions(). A test is also added to verify this behavior. The feedback suggests checking if the widget is still mounted before invoking suggestionsBuilder to prevent using BuildContext across asynchronous gaps.

Comment thread packages/flutter/lib/src/material/search_anchor.dart

@QuncCccccc QuncCccccc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks a lot for your contribution!

@QuncCccccc QuncCccccc added the CICD Run CI/CD label Aug 6, 2026
@cuishuang

Copy link
Copy Markdown
Contributor Author

@QuncCccccc Thank you for your approval.

@dkwingsmt dkwingsmt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

@dkwingsmt

Copy link
Copy Markdown
Contributor

Thanks for your contribution! This PR has reached completion, however we're not able to merge this PR for the moment due to the Material/Cupertino code freeze (#184093). As soon as the decoupling is complete, we will post a way to port this PR to the new packages to land. In the meantime, I'll convert this PR to a draft and apply a label so that we can quickly find it when the freeze is lifted.

@dkwingsmt
dkwingsmt marked this pull request as draft August 12, 2026 16:45
@dkwingsmt dkwingsmt added the waiting for code freeze This PR is waiting for a code freeze to resolve. label Aug 12, 2026
@cuishuang

Copy link
Copy Markdown
Contributor Author

Thanks for your contribution! This PR has reached completion, however we're not able to merge this PR for the moment due to the Material/Cupertino code freeze (#184093). As soon as the decoupling is complete, we will post a way to port this PR to the new packages to land. In the meantime, I'll convert this PR to a draft and apply a label so that we can quickly find it when the freeze is lifted.

Thank you for your update and review!

I noticed that #184093 is closed, and material_ui/cupertino_ui 1.0.0 is now open for contributions. For porting instructions, please see #188444.

Since this PR only affects the Material code and has been approved, do I now need to port it to the flutter/packages(material_ui) directory according to these instructions?

@dkwingsmt

Copy link
Copy Markdown
Contributor

Yes, please! Thank you!

@cuishuang

Copy link
Copy Markdown
Contributor Author

Ported to flutter/packages#12478 Closing this PR per #188444.

Thanks @QuncCccccc @dkwingsmt

@cuishuang cuishuang closed this Aug 16, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Aug 27, 2026
Ports flutter/flutter#190206 to `material_ui` following flutter/flutter#188444.

Fixes flutter/flutter#190205.

This change prevents an older asynchronous `SearchAnchor.suggestionsBuilder` request from replacing the results of a newer request.

It assigns an incrementing ID to each suggestions request and only applies the result when it still represents the latest request.

## Pre-Review Checklist

**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
victorsanni pushed a commit to victorsanni/packages that referenced this pull request Sep 9, 2026
…r#12478)

Ports flutter/flutter#190206 to `material_ui` following flutter/flutter#188444.

Fixes flutter/flutter#190205.

This change prevents an older asynchronous `SearchAnchor.suggestionsBuilder` request from replacing the results of a newer request.

It assigns an incrementing ID to each suggestions request and only applies the result when it still represents the latest request.

## Pre-Review Checklist

**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
victorsanni pushed a commit to victorsanni/packages that referenced this pull request Sep 9, 2026
…r#12478)

Ports flutter/flutter#190206 to `material_ui` following flutter/flutter#188444.

Fixes flutter/flutter#190205.

This change prevents an older asynchronous `SearchAnchor.suggestionsBuilder` request from replacing the results of a newer request.

It assigns an incrementing ID to each suggestions request and only applies the result when it still represents the latest request.

## Pre-Review Checklist

**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages waiting for code freeze This PR is waiting for a code freeze to resolve.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SearchAnchor can show stale results when async suggestions complete out of order

3 participants