Skip to content

Hot restart/reload does not work if program is stuck in infinite loop #85987

Open

Description

@krll-kov
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

void loop() {
  while (true) {
    /* Do nothing */
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: GestureDetector(
            onTap: loop,
            child: Container(
              child: Text('Hello World'),
            ),
          ),
        ),
      ),
    );
  }
}

Flutter version: 2.2.3 from stable channel

Activity

  1. jonahwilliams commented on Jul 7, 2021

    @jonahwilliams
    Contributor

    Both hot reload and hot restart require running code on the UI thread, which cannot be done if it is parked due to an infinite loop or paused via the VM Service. Leaving this open because it would be nice to find a way to detect or deal with this, but In the short term I don't have any ideas.

  2. changed the title [-]Hot restart/reload does not work when while loop without await is activated[/-] [+]Hot restart/reload does not work if program is stuck in infinite loop[/+] on Jul 7, 2021
  3. added
    a: qualityA truly polished experience
    P3Issues that are less important to the Flutter project
    t: hot reloadReloading code during "flutter run"
    toolAffects the "flutter" command-line tool. See also t: labels.
    on Jul 7, 2021
  4. krll-kov commented on Jul 7, 2021

    @krll-kov
    ContributorAuthor

    Both hot reload and hot restart require running code on the UI thread, which cannot be done if it is parked due to an infinite loop or paused via the VM Service. Leaving this open because it would be nice to find a way to detect or deal with this, but In the short term I don't have any ideas.

    Well, if i make the function async, the app won't restart either

    Future<void> loop() async {
      while (true) {
        /* Do nothing */
      }
    }
    
  5. jonahwilliams commented on Jul 7, 2021

    @jonahwilliams
    Contributor

    that is still an infinite loop, I'm not sure why that would change anything

  6. Hixie commented on Jul 22, 2021

    @Hixie
    Contributor

    Why does hot restart require running code on the UI thread?

  7. jonahwilliams commented on Jul 22, 2021

    @jonahwilliams
    Contributor

    @Hixie IIRC it has to do with Isolate shutdown requirements, but @zanderso would know more.

  8. jonahwilliams commented on Jul 22, 2021

    @jonahwilliams
    Contributor

    (this means we also need to disable breakpoints before hot restarts too)

  9. Hixie commented on Jul 22, 2021

    @Hixie
    Contributor

    Seems like an implementation detail, not a design requirement. If I want to hot restart my application, I really don't care what it's doing, I just want to terminate it, reset everything, and start over. If it can be done cleanly then great, if it can't then whatever. After all, if the tool isn't going to be able to do it for me, I'm just going to ^C the process and restart it cold!

  10. zanderso commented on Jul 22, 2021

    @zanderso
    Member

    I'm guessing that hot restart could be made to work without too much difficulty in this case, but at the moment I'd be surprised if the hot restart request had high enough priority to be handled with Dart frames on the stack by the stack overflow check that's implicitly in the loop[1]. Forcibly unwinding the Dart frames and then tearing-down and rebuilding the Isolate doesn't seem like it should be all that much different than what happens with an unhandled exception.

    My understanding is that supporting hot reload in this case would be a lot more difficult since you'd have to rebuild the Dart frames in the new world, but my knowledge in this area could be out of date.

    @rmacnak-google will know the real answer

    [1] Hopefully the stack overflow check in the infinite loop hasn't been optimized away.

  11. rmacnak-google commented on Jul 22, 2021

    @rmacnak-google
    Contributor

    Hot reload proper will work just fine even if the Dart code is sitting in an infinite loop (things need to be blocked in foreign code for the VM to be unable to handle service requests). The program will be updated and the infinite loop will continue. Though anything after hot reload proper that wants to run at the next event loop boundary, such as asking the framework to reassemble, would still need to force an unwind to get new events handled. I would highly discourage tooling from automatically doing so because it breaks fix-and-continue debugging (such as reloading when paused at an exception or breakpoint), where the existing stack is valuable context.

    For the hot restart case, Isolate.kill(immediate) would work on such an infinite loop.

  12. darshankawar commented on Jul 28, 2021

    @darshankawar
    Member

    Tapping on text and then trying to hot reload / restart shows below:

    Performing hot reload...
    Syncing files to device iPhone 12 Pro Max...
    
    
    flutter doctor -v
    [✓] Flutter (Channel stable, 2.2.3, on Mac OS X 10.15.4 19E2269 darwin-x64,
        locale en-GB)
        • Flutter version 2.2.3 at /Users/dhs/documents/fluttersdk/flutter
        • Framework revision f4abaa0735 (4 days ago), 2021-07-01 12:46:11 -0700
        • Engine revision 241c87ad80
        • Dart version 2.13.4
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30)
        • Android SDK at /Users/dhs/Library/Android/sdk
        • Platform android-30, build-tools 30.0.3
        • ANDROID_HOME = /Users/dhs/Library/Android/sdk
        • Java binary at: /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/Android
          Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
        • All Android licenses accepted.    
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        • CocoaPods version 1.10.1
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.1)
        • Android Studio at /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/Android
          Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6915495)        
    
    [✓] VS Code (version 1.57.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (4 available)
        • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 10 (API 29)
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios
          • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                               •
          web-javascript • Google Chrome 91.0.4472.114
    
    • No issues found!
    
    
    
    
    
    
  13. matanlurey commented on Jul 10, 2025

    @matanlurey
    Contributor

    @bkonyi 👁️

  14. bkonyi commented on Jul 11, 2025

    @bkonyi
    Contributor

    Confirmed this still happens.

    I think I've managed to fix the hot restart case by just killing all the isolates before performing the restart (see #171988) but I need to run all the tests to verify that hasn't broken something else.

    The hot reload case gets stuck invoking the ext.flutter.reassemble service extension, which makes sense since service extensions only execute once the event loop has a chance to process the next event. If the infinite loop contains an asynchronous gap, we don't get stuck. Having an infinite loop like this would already cause the UI isolate to become unresponsive, so I'm not too concerned about hot reload not working here as long as hot restart can act as an escape hatch.

  15. added
    tool-still-validIssues that have been deemed still valid as part of the Flutter Tools issue cleanup initiative.
    on Jul 11, 2025
  16. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Nov 14, 2025
  17. flutter-triage-bot commented on Nov 14, 2025

    @flutter-triage-bot

    This issue is assigned to @bkonyi but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  18. flutter-triage-bot commented on Jan 31, 2026

    @flutter-triage-bot

    This issue was assigned to @bkonyi but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed.

  19. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Jan 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: qualityA truly polished experiencefound in release: 2.2Found to occur in 2.2has reproducible stepsThe issue has been confirmed reproducible and is ready to work ont: hot reloadReloading code during "flutter run"team-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.tool-still-validIssues that have been deemed still valid as part of the Flutter Tools issue cleanup initiative.triaged-toolTriaged by Flutter Tool team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions