Skip to content

[flutter_tools] Fix deadlock in debug adapters when process exits early - #190931

Queued
bkonyi wants to merge 4 commits into
flutter:masterfrom
bkonyi:issue-190721
Queued

[flutter_tools] Fix deadlock in debug adapters when process exits early#190931
bkonyi wants to merge 4 commits into
flutter:masterfrom
bkonyi:issue-190721

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

When launching a debug session, if the target process exited early (e.g., due to a startup error, non-existent target, or failure before the VM service connects), the debug adapter would deadlock waiting indefinitely for debuggerInitialized to complete.

This change:

  1. Tracks waitingForDebugger and adds a debuggerInitializationFailedCompleter in FlutterBaseDebugAdapter.
  2. Completes debuggerInitializationFailedCompleter with a DebugAdapterException in handleSessionTerminate if waitingForDebugger is active.
  3. In FlutterTestDebugAdapter.launchImpl and FlutterDebugAdapter._handleAppStarted, races debuggerInitialized against debuggerInitializationFailedCompleter.future via Future.any, allowing the adapter to fail fast and terminate cleanly without hanging.
  4. Adds a regression integration test verifying fail-fast behavior when the target process exits early.

Fixes #190721

Pre-launch Checklist

bkonyi added 2 commits August 10, 2026 21:32
When launching a debug session, if the process exits before the debugger
connects, the debug adapter would deadlock waiting for the debugger to
initialize.

This change sets `waitingForDebugger = true` before launching the process,
and uses `Future.any` to wait for either `debuggerInitialized` or
`debuggerInitializationFailedCompleter.future`. The completer is completed
with an error when the session terminates, breaking the deadlock.

Fixes flutter#190721
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 11, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Aug 11, 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 Flutter debug adapters to handle early session termination during debugger initialization by introducing a debuggerInitializationFailedCompleter and a waitingForDebugger flag, allowing the adapter to fail fast if the process exits early. The review feedback identifies a potential unhandled asynchronous exception if the completer is completed with an error before listeners are attached, and points out that the new public fields lack required documentation.

Comment on lines +48 to +49
bool waitingForDebugger = false;
final Completer<void> debuggerInitializationFailedCompleter = Completer<void>();

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.

high

Issue: Potential Unhandled Asynchronous Exception & Missing Documentation

  1. Unhandled Exception Risk: If handleSessionTerminate is called while launchAsProcess is running (where waitingForDebugger is true but Future.any has not yet been awaited), debuggerInitializationFailedCompleter.completeError will be called. Since there are no listeners on debuggerInitializationFailedCompleter.future at that moment, this will result in an unhandled asynchronous exception in Dart, which can crash the process or cause test flakiness.

    Adding a dummy error handler using ..future.catchError((Object _) {}) at creation time safely prevents unhandled exceptions while still allowing Future.any to receive the error when it eventually listens to it.

  2. Missing Documentation: According to the Repository Style Guide (Line 62: "All public members should have documentation."), these new public fields should have /// doc comments explaining their purpose.

Here is the suggested improvement:

  /// Whether the adapter is currently waiting for the debugger to initialize.
  bool waitingForDebugger = false;

  /// A completer that completes with an error if debugger initialization fails
  /// (for example, if the session terminates early).
  ///
  /// A dummy error handler is attached to the future to prevent unhandled
  /// asynchronous exceptions if it completes before any listeners are active.
  final Completer<void> debuggerInitializationFailedCompleter = Completer<void>()
    ..future.catchError((Object _) {});
References
  1. All public members should have documentation. (link)

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.

Did the bot get smart?

I think it's right - flutter_test_adapter sets waitingForDebugger = true before an await, so if it happens before reading the Future.any(), there will be no listeners and this might be an unhandled error?

@bkonyi
bkonyi requested a review from DanTup August 11, 2026 16:38
DanTup
DanTup previously approved these changes Aug 11, 2026

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

lgtm!

…initialization failure and document public members
DanTup
DanTup previously approved these changes Aug 11, 2026

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

Still LGTM!

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 11, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 11, 2026
@auto-submit

auto-submit Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/190931, because - The status or check suite Dashboard Checks has failed. Please fix the issues identified (or deflake) before re-applying this label.

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 11, 2026
@auto-submit

auto-submit Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/190931, because - The status or check suite Dashboard Checks has failed. Please fix the issues identified (or deflake) before re-applying this label.

@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 11, 2026
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 11, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Aug 11, 2026
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 CICD Run CI/CD tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mac tool_integration_tests_7 flakey / timing out

2 participants