Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[web] fix hot restart in entrypoint generated by flutter create #110229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2022

Conversation

yjbanov
Copy link
Contributor

@yjbanov yjbanov commented Aug 25, 2022

Fix entrypoint generated by flutter create to support hot restart. #108776 fixed hot restart, but it requires an update to index.html. So flutter create should have been updated.

Fixes #109093

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.

@flutter-dashboard flutter-dashboard bot added c: contributor-productivity Team-specific productivity, code health, technical debt. tool Affects the "flutter" command-line tool. See also t: labels. labels Aug 25, 2022
@yjbanov yjbanov requested review from ditman and mdebbar August 25, 2022 00:44
@yjbanov yjbanov force-pushed the fix-generated-entrypoint branch from fab0d39 to 40c4758 Compare August 25, 2022 00:47
@yjbanov yjbanov changed the title [web] fix entrypoint generated by flutter create [web] fix hot restart in entrypoint generated by flutter create Aug 25, 2022
}

Future<void> _generateEntrypoint() async {
final Directory tempDirectory = Directory.systemTemp.createTempSync('flutter_web_generated_entrypoint.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the trailing . in the directory name intended or a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's intended. I stole it from another test from test.dart. I'm now considering it a "convention" :)

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, i think a hash gets appended, so the trailing . looks cleaner

@@ -0,0 +1,2 @@
# This file is generated by a test. It should not be committed to source control.
generated_entrypoint.html
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not git-ignore the entire directory flutter_web_generated_entrypoint/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

flutter_web_generated_entrypoint is a temp directory outside the source tree that's created then deleted (see Directory.systemTemp.createTempSync). It's only used as a temporary space to dump an entire project. The test actually only needs the index.html. It doesn't need the "counter app" Dart code.

Comment on lines +50 to +53
onEntrypointLoaded: function(engineInitializer) {
engineInitializer.initializeEngine().then(function(appRunner) {
appRunner.runApp();
});
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a potential race condition. I would suggest adding a global counter then doing:

Suggested change
onEntrypointLoaded: function(engineInitializer) {
engineInitializer.initializeEngine().then(function(appRunner) {
appRunner.runApp();
});
onEntrypointLoaded: function(engineInitializer) {
currentCounter++;
final int storedCounter = currentCounter;
engineInitializer.initializeEngine().then(function(appRunner) {
// Make sure this is not a stale promise.
if (storedCounter == currentCounter) {
appRunner.runApp();
}
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to understand more about the race condition. What is racing against what? I think if there is one, we should definitely handle it. However, I think we should avoid relying on user code to fix it (this code is generated once then it can be edited by the app developer), and instead fix the race in flutter.js itself.

Copy link
Contributor

@mdebbar mdebbar Aug 25, 2022

Choose a reason for hiding this comment

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

I was thinking of the case when onEntrypointLoaded is invoked again before the previous engine initialization promise was resolved. I don't know if this case is possible or not. Maybe if the user hot-reloads multiple times in a row?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yeah, that's a legitimate concern. However, I think it should be addressed at the flutter.js level, if it isn't already. flutter.js should be aware of which phase the loading process the app is in. For example, if onEntrypointLoaded has been called, engineInitializer.initializeEngine() has been called, but the promise hasn't resolved yet, and the user hot-restarts now, then the Promise from initializeEngine should not be resolved (or perhaps it should be rejected).

OTOH, something tells me that if our initialization process is interrupted by hot restart, then all bets are off. We have so much intricate async initialization logic - root DOM nodes, fonts, CanvasKit, service worker - that there are many places that would be easily corrupted by such interruption. Perhaps what we need is some collaboration between the framework and the tool, where the tool first sends an "intent" to hot restart, and the framework sends back a signal when it's safe to do so. I'm not sure it's practical for us to try and support hot restarts at completely random points in time.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair. If possible, we should at least print a warning (in flutter.js) when this race condition is detected, and ask the user to refresh the page instead.

That said, I think this race condition has existed for a while (and nobody complained) so it's probably not as important as I thought it was.

@yjbanov yjbanov added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 25, 2022
@auto-submit auto-submit bot merged commit 6e57ed6 into flutter:master Aug 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Aug 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App c: contributor-productivity Team-specific productivity, code health, technical debt. tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hot Restart broken on Flutter Web with --web-renderer html
3 participants