Skip to content

[flutter_tools] add preHotRestart callbacks - #130662

Closed
davidmartos96 wants to merge 51 commits into
flutter:masterfrom
davidmartos96:hot_restart_hook
Closed

davidmartos96 wants to merge 51 commits into
flutter:masterfrom
davidmartos96:hot_restart_hook

Conversation

@davidmartos96

Copy link
Copy Markdown
Contributor

Fixes #75528
Work on top of @jonahwilliams PR #86134

Hello! I took a stab at this, by updating the code from Jonah with latest master.
On top of the previous code I also added support for Pre Hot Restart callbacks to the web runner.

I did some real usage tests with this and it is exactly what I needed. On my project, a websocket connection is stablished when the app runs. Whenever the app does a hot restart, there is not a chance for the app to close it, so the socket server thinks another connection is made with the same "client ID" which is unexpected in our app domain. This hook provides the chance to close the connection and then restart normally.

I'm sure there are other use cases where this functionality could be really handy like native handles from FFI showcased in the original issue or Firestore streams as manifested by @maks here #75528 (comment)

@jonahwilliams What was exactly what you were thinking about reworking regarding the timeouts?
Do you remember if there was any other thing you were planning to change before stopping working in the PR?

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. framework flutter/packages/flutter repository. See also f: labels. labels Jul 15, 2023

@goderbauer goderbauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just some fly-by comments during triage.

Comment thread packages/flutter/lib/src/widgets/binding.dart Outdated
Comment thread packages/flutter/lib/src/widgets/binding.dart Outdated
Comment thread packages/flutter/lib/src/widgets/binding.dart Outdated
Comment thread packages/flutter/lib/src/widgets/binding.dart Outdated
@davidmartos96

Copy link
Copy Markdown
Contributor Author

Just some fly-by comments during triage.

Thanks for the comments!

@christopherfujino
christopherfujino self-requested a review July 20, 2023 20:47
@tjarvstrand

Copy link
Copy Markdown

This is awesome! Please prioritize getting this over the finish line!

@eliasyishak

Copy link
Copy Markdown
Contributor

Friendly ping @christopherfujino

@christopherfujino

Copy link
Copy Markdown
Contributor

Will take a look at this the week after next when @goderbauer is back in the office.

@flutter-dashboard

Copy link
Copy Markdown

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@davidmartos96

Copy link
Copy Markdown
Contributor Author

@christopherfujino Any update? CI is green now, the number of extensions in the test needed to be updated after merging with master

@christopherfujino

christopherfujino commented Dec 15, 2023 •

Copy link
Copy Markdown
Contributor

On my project, a websocket connection is stablished when the app runs. Whenever the app does a hot restart, there is not a chance for the app to close it, so the socket server thinks another connection is made with the same "client ID" which is unexpected in our app domain.

@davidmartos96 I apologize for the long delay getting back to you on this PR.

In real usage of this app, would it not be possible that the app fail without closing the connection, and then restart with the same client ID, leading to this same issue? In other words, how is the hot restart use case different from the app getting terminated and restarted?

@davidmartos96

Copy link
Copy Markdown
Contributor Author

@christopherfujino On Flutter Web, when restarting the page using the browser toolbar (F5), the websocket connection is closed. The problem is that hot restart (on web) doesn't behave the same way, and it stays open.

If the app fails, Flutter provides ways to deal with global errors, so you could close connections if you need to from the error handler.

@christopherfujino

Copy link
Copy Markdown
Contributor

@christopherfujino On Flutter Web, when restarting the page using the browser toolbar (F5), the websocket connection is closed. The problem is that hot restart (on web) doesn't behave the same way, and it stays open.

Ahh ok, I didn't realize you were targetting flutter for web. I was imagining a mobile app.

If the app fails, Flutter provides ways to deal with global errors, so you could close connections if you need to from the error handler.

You're talking here about thrown Dart errors, right? I was imagining a failure where the VM immediately died. However, in the context of the web maybe the browser handles cleaning up the websocket...

@christopherfujino

Copy link
Copy Markdown
Contributor

@davidmartos96 I'm sorry for the looong delay on this PR, and that I'm going to be closing it. This was a difficult decision because I agree that this is a legitimate bug in the Flutter SDK and conceptually this seems like a sound solution. The honest truth though is that I do not understand well enough all of the implications of this change, and whether or not the behavior would be correct for all flutter developers.

Moving forward, I filed #144737 for the web use case. There are a set of known issues with hot restart for the web, and I'm hopeful we can have a more general solution.

For the situation with dart:ffi, I think the right solution here would be for Dart finalizers to run on hot restart, which is tracked at dart-lang/sdk#50016.

Again I apologize for the way code review of this PR was handled, and I do appreciate your contribution. All the best.

@davidmartos96

Copy link
Copy Markdown
Contributor Author

@christopherfujino Thank you. I can understand the reason. Hot restart is a core feature in the framework which needs to be handled with care.

The original motivation of the PR was to have a way to close socket connections. If there is an automatic way to do that it would be great. With this approach the user would need to explicitly call close, whereas in io it's not needed.

This PR was a nice chance to dig into the framework and see how the restart features are wired together, no work is lost 🙂
Thanks again.

@christopherfujino

Copy link
Copy Markdown
Contributor

The original motivation of the PR was to have a way to close socket connections. If there is an automatic way to do that it would be great. With this approach the user would need to explicitly call close, whereas in io it's not needed.

AFAIK there is no out of the box automated way to do this. I can't promise anything, but if you follow #144737 I will make sure it stays up to date. There are other related problems, and work going on that--if it lands--would give you the hot restart semantics you want.

I can't promise because, as with this PR, the implications are deep, and it's still unclear the full impact of changing the semantics.

This PR was a nice chance to dig into the framework and see how the restart features are wired together, no work is lost 🙂 Thanks again.

I'm glad, and again I appreciate your contribution to a particularly tricky part of the codebase. Hope to see you around in the future!

bkonyi added a commit to bkonyi/flutter that referenced this pull request Sep 21, 2026
…orHandling.handleError

- Explain in `FutureErrorHandling.handleError`'s doc comment why `Future.then` is used instead of `Future.catchError` or `Future.onError` (`AvoidFutureCatchError` custom analyzer rule in `dev/bots/custom_rules/avoid_future_catcherror.dart`, flutter#130662, dart-lang/sdk#51248).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels. tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add hot restart hooks to support dart:ffi

8 participants