Skip to content

Clarify that super.dispose() must be called synchronously - #193416

Merged
auto-submit[bot] merged 18 commits into
flutter:masterfrom
awababushnagh0-sudo:fix-140071-dispose-super-sync
Nov 14, 2026
Merged

auto-submit[bot] merged 18 commits into
flutter:masterfrom
awababushnagh0-sudo:fix-140071-dispose-super-sync

Conversation

@awababushnagh0-sudo

@awababushnagh0-sudo awababushnagh0-sudo commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

If a State.dispose override is marked async and calls super.dispose() after an await, the framework reports "dispose failed to call super.dispose" even though the call is there. This happens because StatefulElement.unmount checks the lifecycle state as soon as dispose() returns, before the rest of the method runs.

This PR:

  • adds an ErrorHint to that assertion, shown only when dispose() returned a Future, explaining that the framework does not await dispose(), so super.dispose() must be called synchronously;
  • updates the State.dispose documentation to explain the same, including that asynchronous cleanup started from dispose() continues after the State is unmounted;
  • adds a tested API example (examples/api/lib/widgets/framework/state.dispose.0.dart) of a StatefulWidget that cancels a stream subscription in dispose() without awaiting it and resubscribes in didUpdateWidget;
  • adds tests in async_lifecycle_test.dart for Future<void> dispose() async, void dispose() async, a synchronous dispose() that skips super.dispose() (no async hint), and an async dispose() that calls super.dispose() before awaiting (no error).

Fixes #140071
Fixes #148031

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 the framework flutter/packages/flutter repository. See also f: labels. label Sep 27, 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 updates the documentation and error handling in framework.dart to clarify that super.dispose() must be called synchronously, and adds a corresponding test in framework_test.dart. The review feedback suggests explicitly discouraging making the dispose method asynchronous altogether in both the documentation and the error hint, rather than suggesting calling super.dispose() before the first await, and recommends updating the test assertion to match this revised error message.

Comment on lines +1304 to +1306
/// method, as in `super.dispose()`. This call must be made synchronously,
/// not after an `await`, because the framework checks that it was made as
/// soon as this method returns.

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.

medium

While clarifying that super.dispose() must be called synchronously is helpful, we should explicitly state that the dispose method itself must not be asynchronous (e.g., marked async). Making dispose asynchronous is an anti-pattern because the framework does not await the returned Future, and any code running after an await will execute after the state is already disposed, which can lead to memory leaks or runtime errors.

  /// method, as in `super.dispose()`. This method must not be asynchronous
  /// (e.g., marked `async`), because the framework checks that `super.dispose()`
  /// was called synchronously as soon as this method returns.
References
  1. Optimize for readability and avoid patterns that lead to state duplication or lifecycle bugs. (link)

@awababushnagh0-sudo

Copy link
Copy Markdown
Contributor Author

@navaronbracke good morning I just want to ask if there is any new about this PR

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

Some feedback regarding the test, sample and an open question about an edge case

@awababushnagh0-sudo

Copy link
Copy Markdown
Contributor Author

@navaronbracke changes have been made your review is much appreciated

@awababushnagh0-sudo

Copy link
Copy Markdown
Contributor Author

@nichtverstehen hello hope your having a good day just want to check if there is any updates

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

Minor nit on the example, but otherwise this is good to go!

navaronbracke
navaronbracke previously approved these changes Oct 28, 2026
@navaronbracke navaronbracke added the CICD Run CI/CD label Oct 28, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 28, 2026

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

Thanks for the PR, does it also fix #148031? If yes, add to PR description.

The hint telling developers to make dispose() synchronous was shown for
every dispose() that failed to call super.dispose(), including ones that
are already synchronous. Show it only when dispose() returned a Future,
the same way initState() and didUpdateWidget() detect async overrides.
Previously, rebuilding CountDisplay with a different stream left it
subscribed to the old one. Make the subscription mutable and swap it
in didUpdateWidget.
StreamSubscription.cancel() stops event delivery synchronously, so the
listener can never run after dispose().
Cover a void async dispose(), a synchronous dispose() that skips
super.dispose() (no async hint), and an async dispose() that calls
super.dispose() before awaiting (no error).
@github-actions github-actions Bot added the d: api docs Issues with https://api.flutter.dev/ label Nov 8, 2026
@auto-submit

auto-submit Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/193416, Failed to enqueue flutter/flutter/193416 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@awababushnagh0-sudo

awababushnagh0-sudo commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for pushing this through @victorsanni @navaronbracke Looks like each autosubmit label event triggers a new "Code Freeze" run that needs first-time-contributor approval again, so the check goes back to "expected" right when auto-submit tries to enqueue. All Check Code Freeze runs on 94d4905 that actually ran passed. Is there another way to land it?

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit

auto-submit Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/193416, Failed to enqueue flutter/flutter/193416 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit

auto-submit Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/193416, Failed to enqueue flutter/flutter/193416 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit

auto-submit Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/193416, Failed to enqueue flutter/flutter/193416 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit

auto-submit Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/193416, Failed to enqueue flutter/flutter/193416 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit

auto-submit Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/193416, Failed to enqueue flutter/flutter/193416 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit

auto-submit Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/193416, Failed to enqueue flutter/flutter/193416 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 13, 2026
Merged via the queue into flutter:master with commit acbd9e0 Nov 14, 2026
57 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 14, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 16, 2026
flutter/flutter@1b95854...d8bd864

2026-11-16 [email protected] Roll Skia from f2790b0ea630 to 118eaff3deb4 (8 revisions) (flutter/flutter#196661)
2026-11-16 [email protected] [native_assets] Keep a dylib's install name in flutter test when its path does not fit (flutter/flutter#194802)
2026-11-16 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196579)
2026-11-16 [email protected] [flutter_tools] Bound Browser.close in Chromium.close (flutter/flutter#196211)
2026-11-16 [email protected] Roll Skia from e8ff7a4187ec to f2790b0ea630 (13 revisions) (flutter/flutter#196652)
2026-11-14 [email protected] Revert: Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196580)
2026-11-14 [email protected] Revert: Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196575)
2026-11-14 [email protected] [flutter_tools] Guard ExtensionManager initialization against concurrent disposal (flutter/flutter#196459)
2026-11-14 [email protected] [ImpellerC] Escape spaces in depfile paths (flutter/flutter#196555)
2026-11-14 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196549)
2026-11-14 [email protected] Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196260)
2026-11-13 [email protected] Safely return null on malformed stack trace line in StackFrame.fromStackTraceLine (flutter/flutter#194673)
2026-11-13 [email protected] Clarify that super.dispose() must be called synchronously (flutter/flutter#193416)
2026-11-13 [email protected] Consolidate exit-code parameters on runCommand with ExpectedExit (flutter/flutter#196479)
2026-11-13 [email protected] Support multi-view apps in widget test binding (flutter/flutter#194480)
2026-11-13 [email protected] Document using a ListView inside a Column (flutter/flutter#195145)
2026-11-13 [email protected] Add didChangeAlwaysUse24HourFormat to WidgetsBinding and update MediaQuery (flutter/flutter#196117)
2026-11-13 [email protected] Build local engine wasm configs on 32-core Linux bots (flutter/flutter#196284)
2026-11-13 [email protected] [Android] Fix Hybrid Composition resize deadlock (flutter/flutter#196255)
2026-11-13 [email protected] Roll Skia from 11f46cfe8838 to e8ff7a4187ec (9 revisions) (flutter/flutter#196557)
2026-11-13 [email protected] [Impeller] Fix SurfaceContextVK submission tracking and double HostBuffer reset (flutter/flutter#196064)
2026-11-13 [email protected] Manual roll Packages from 14f9933 to 015df97 (5 revisions) (flutter/flutter#196558)
2026-11-13 [email protected] Remove routes_transition_test from widgets (flutter/flutter#186620)
2026-11-13 [email protected] [flutter_tools] Replace TargetPlatform.simpleName with cpuArch (flutter/flutter#196264)
2026-11-13 [email protected] Add a deferred platform-view-created path to the shell and use it on Android (flutter/flutter#196342)
2026-11-13 [email protected] Optimize RenderSliverVariedExtentList and introduce protected itemExtentOf API in RenderSliverFixedExtentBoxAdaptor (flutter/flutter#192384)
2026-11-13 [email protected] Revert: [android]: prewarms shaders when the context is created (flutter/flutter#196468)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

3 participants