Skip to content

Reland: Allow resetting test invariants in addTearDown - #193476

Merged
auto-submit[bot] merged 4 commits into
flutter:masterfrom
dkwingsmt:reland-fix-invariant-check
Oct 2, 2026
Merged

auto-submit[bot] merged 4 commits into
flutter:masterfrom
dkwingsmt:reland-fix-invariant-check

Conversation

@dkwingsmt

@dkwingsmt dkwingsmt commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

This PR relands #192082 after fixing the error in integration tests as described in #193383.

I've manually verified that the integration test fails on #192082 and passes now.

Why it failed

This integration test runs dev/automated_tests/flutter_test/ticker_test.dart, which leaves a Ticker running. It expects that test to fail with an "An animation is still running even after the widget tree was disposed." error. After #192082, ticker_test.dart passed instead.

The leaked ticker is caught by SchedulerBinding.debugAssertNoTransientCallbacks. Unlike most invariant checks, it doesn't throw; it reports the failure through FlutterError.reportError. During a test, FlutterError.onError stores reported errors in _pendingExceptionDetails, and the test completion handler reports them when runTest finishes. Changing the behavior of this method would be a breaking change to the public API in the framework, so instead the test framework should be changed to pick up this error.

Before #192082, the check ran inside _runTestBody, so the completion handler picked up the error. #192082 moved the check to postTest, which runs after the completion handler. The try/catch in postTest only catches thrown errors, and postTest then clears _pendingExceptionDetails, so the error was silently dropped and the test passed.

What this reland changes

See the 2nd commit for the list of changes.

  • After running the invariant checks, postTest also picks up _pendingExceptionDetails and reports it. If a check also threw, the thrown error is reported and the other one is printed to the console so it isn't lost.
  • postTest now passes the test description to reportTestException instead of '', so the output includes The test description was: ... again. The integration test's expected output relies on that line.
  • The four _verify*Unset helpers go back to FlutterError.reportError, which is what they used before Allow resetting test invariants in addTearDown #192082. Now that postTest handles reported errors, they don't need to throw, and they behave the same as before. Allow resetting test invariants in addTearDown #192082 made this change with the hope to simplify and unify the code flow, but now that we have to pick up _pendingExceptionDetails anyway, there's no reason to make this change.

Tests

  • Added a test to bindings_invariants_test.dart. It leaks a Ticker and checks that postTest reports the error along with the test description.
  • test_test.dart: flutter test should report a nice error when a Ticker is left running passes again.

What the 3rd commit changes

The 3rd commit refactors the bindings_invariants_test.dart to ensure that the postTest is run (and invariants are reset) even when errors occur during the test body.

Prior arts

This PR replaces #186604, an earlier attempt at this issue. While reviewing the implementation I found that this problem is much more complicated and would be easier for me to rewrite it. I appreciate the original author @/mvincentong for the inspiration.

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.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Sep 28, 2026
@github-actions github-actions Bot added a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels. labels Sep 28, 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 refactors TestWidgetsFlutterBinding to defer framework-level invariant verifications and binding state cleanups to postTest, allowing them to run after any addTearDown callbacks have executed. It also introduces a new test suite to verify these invariant behaviors. The review feedback correctly identifies several robustness issues in the new tests where global state modifications and resource allocations (such as Ticker and debugDefaultTargetPlatformOverride) are not guaranteed to be cleaned up if assertions fail, and suggests using addTearDown to ensure proper teardown.

Comment thread packages/flutter_test/test/bindings_invariants_test.dart Outdated
Comment thread packages/flutter_test/test/bindings_invariants_test.dart
Comment thread packages/flutter_test/test/bindings_invariants_test.dart
@dkwingsmt
dkwingsmt requested a review from justinmc September 28, 2026 21:42

@justinmc justinmc 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.

Reland LGTM 👍

@dkwingsmt dkwingsmt added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 28, 2026
@auto-submit

auto-submit Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/193476, because - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label.

@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 28, 2026
@dkwingsmt

dkwingsmt commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Update: It is caused by three more failures in Google's codebase and I've opened CLs to close them respectively.

Edit: As of Oct 1, 2 out of 3 CLs have been submitted. cl/989931886 remains, which I'm monitoring.

@dkwingsmt dkwingsmt added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 2, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Oct 2, 2026
Merged via the queue into flutter:master with commit 61407e9 Oct 2, 2026
23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 2, 2026
chrisbobbe added a commit to chrisbobbe/zulip-flutter that referenced this pull request Oct 8, 2026
Flutter's recent change
flutter/flutter#193476
moved the test harness's check for pending frame callbacks. It now
runs after the test's `addTearDown` callbacks, instead of right after
the harness's final pump.

Loading a font sends a `fontsChange` message, and text in the tree
reacts by scheduling a frame callback. When a `testWidgets` body loads
a font, the message is handled only after the final pump, so the
callback never runs. The check used to miss such callbacks, but now it
finds them. As a result, 178 tests in emoji_reaction_test and
katex_test failed with "An animation is still running even after the
widget tree was disposed".

Loading the fonts in `setUpAll` avoids this, because that runs before
any test has put text in the tree. Fonts persist across tests anyway.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: tests "flutter test", flutter_test, or one of our tests CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants