Skip to content

Using a single-instance lock for hot restart in debug mode will cause the application to become unresponsive. #76

Done
libnativeapi/nativeapi-core
#116

Description

@Empty-57

Flexible approach:

void checkSingleInstance(List<String> args) {
  if (kDebugMode) return;
  if (!Application.instance.requestSingleInstanceLock(
    _singleInstanceID,
    args,
  )) {
    Application.instance.quit(0);
  }

  Application.instance.addListener((event) {
    if (event is ApplicationSecondInstanceEvent) {
      WindowController.instance.show();
    }
  });
}

This bug should be fixed so that it won't cause any errors even during a hot restart in the debug mode.

Activity

  1. lijy91 commented on Dec 4, 2026

    @lijy91
    Member

    Thanks for reporting @Empty-57!

    We tested hot restart (Shift+R / R in flutter run -d windows) on Windows 11 with both flutter_single_instance_example and ZeroBit-Player (with if (kDebugMode) return; removed, using nativeapi_flutter: 0.6.3), and consecutive hot restarts completed in <1s with the window remaining responsive (RequestSingleInstanceLock is idempotent for the same identifier in the same process and returns true immediately).

    Could you share a bit more detail so we can reproduce the exact hang?

    1. Your flutter --version and IDE / how hot restart is triggered (flutter run R, VS Code Ctrl+Shift+F5, or full stop & relaunch).
    2. Whether another instance of the app (e.g. an installed Release build in the tray) was running at the same time, or if a debugger breakpoint was hit.
    3. Any console output when it becomes unresponsive.

    Also one small note on the snippet above: Application.instance.quit(0) schedules shutdown asynchronously, so if requestSingleInstanceLock returns false, make sure to return; early from main() (or call exit(0)) instead of continuing to initialize the rest of the app and calling runApp().

  2. Empty-57 commented on Dec 4, 2026

    @Empty-57
    Author

    @lijy91
    1.Flutter 3.50.1 Use the IDEA console and press Shift + R to perform a hot restart.
    2.Only one instance was run.
    3.The console does not output any content, the UI interface freezes, but the program does not immediately exit. After some time, Windows will prompt that the application is unresponsive.

  3. Empty-57 commented on Dec 4, 2026

    @Empty-57
    Author

    I captured the native stack for this hang. It reproduces with both Shift+R and an uppercase R entered with Caps Lock in the IntelliJ IDEA terminal. Only one instance of ZeroBit Player was running.

    Environment

    • Windows x64, Flutter 3.50.1, Dart 3.14.0
    • nativeapi_flutter, nativeapi, and cnativeapi 0.6.3

    Reproduction

    1. Start ZeroBit Player with flutter run --debug -d windows and let it initialize. The app's Rust code creates a WinRT MediaPlayer for System Media Transport Controls (SMTC).
    2. Put focus in the terminal and press R to hot restart. The new Dart isolate calls WindowManager.instance.getCurrent() during startup.
    3. On a failing restart, the player process stays alive and Windows reports its window as unresponsive. I have also seen successful restarts with the same setup.

    WinDbg capture

    cnativeapi!native_window_manager_get_current
      -> USER32!EnumWindows
      -> USER32!GetWindowTextLengthW
      -> USER32!SendMessageW
      -> win32u!NtUserMessageCall
    

    The blocked thread is the player's main window thread. I queried the target HWND from the stack. Its window class was MediaPlayer SMTC window - {94D27DC4-C3AE-4AD5-AFEF-666C581E243C}. It belonged to the player process on a different thread. That thread was in WaitOnAddress inside the app's Rust DLL when sampled.

    The stack points to the fallback in GetCurrent(). FindCurrentWindowCandidates() synchronously calls GetWindowTextLengthW() for a same-process candidate without a NativeAPI window ID. Windows sends WM_GETTEXTLENGTH to that window, and the caller can wait indefinitely if the owner thread does not pump messages. This accounts for the freeze in this capture. The single-instance lock was my initial hypothesis. Source at the core revision used by 0.6.3 · GetWindowTextLengthW documentation

    Could the candidate scan avoid an unbounded cross-thread title query? For example, a bounded WM_GETTEXTLENGTH via SendMessageTimeoutW could skip a timed-out candidate and continue looking for the app's main window. The expected behavior is for the lookup to finish even if another same-process window cannot answer.

  4. lijy91 commented on Dec 4, 2026

    @lijy91
    Member

    Thank you for the WinDbg capture @Empty-57 — that pinpointed the exact deadlock immediately!

    Fixed in libnativeapi/nativeapi-core#116 and #87:

    • Replaced unbounded cross-thread GetWindowTextLengthW calls in WindowManager::GetCurrent() (FindCurrentWindowCandidates) and WindowManager::GetAll() (EnumWindowsCallback) with a bounded SendMessageTimeoutW(..., WM_GETTEXTLENGTH, ..., SMTO_ABORTIFHUNG | SMTO_NORMAL, 50, ...) helper, skipping unresponsive worker-thread windows (such as WinRT SMTC helper HWNDs).
    • Added a multi-threaded Windows regression test (window_manager_get_current_windows_test).

    This fix will be included in the next cnativeapi / nativeapi patch release.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions