Skip to content

[Tool] Handle DTD connection failures gracefully in widget-preview#186952

Merged
auto-submit[bot] merged 4 commits into
flutter:masterfrom
bkonyi:analyze-flutter-crash-trends-20260521
May 26, 2026
Merged

[Tool] Handle DTD connection failures gracefully in widget-preview#186952
auto-submit[bot] merged 4 commits into
flutter:masterfrom
bkonyi:analyze-flutter-crash-trends-20260521

Conversation

@bkonyi

@bkonyi bkonyi commented May 22, 2026

Copy link
Copy Markdown
Contributor

Previously, unhandled RpcExceptions and connection-closed StateErrors bubbled up and crashed the "flutter widget-preview start" command and the active file-watching sync streams when the Dart Tooling Daemon (DTD) connection was closed unexpectedly.

This change:

  • Wraps startup DTD calls in a try-catch block and throws a user-friendly ToolExit on failure instead of crashing.
  • Gracefully catches DTD call exceptions inside the file-watcher stream and logs a warning instead of crashing the active stream.
  • Adds a unit test case in widget_preview_test.dart to verify that DTD communication failures at startup result in a clean ToolExit.

Fixes #187322

Previously, unhandled RpcExceptions and connection-closed StateErrors bubbled
up and crashed the "flutter widget-preview start" command and the active
file-watching sync streams when the Dart Tooling Daemon (DTD) connection
was closed unexpectedly.

This change:
- Wraps startup DTD calls in a try-catch block and throws a user-friendly
  ToolExit on failure instead of crashing.
- Gracefully catches DTD call exceptions inside the file-watcher stream and
  logs a warning instead of crashing the active stream.
- Adds a unit test case in widget_preview_test.dart to verify that DTD
  communication failures at startup result in a clean ToolExit.
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label May 22, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label May 22, 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 improves error handling for the Dart Tooling Daemon (DTD) by catching exceptions during widget preview retrieval and logging warnings upon connection loss. Feedback suggests that the try-catch block in widget_preview.dart should also catch StateError to prevent crashes, as StateError does not inherit from Exception. Additionally, a redundant type check for RpcException in lsp_preview_detector.dart was identified for simplification.

Comment on lines +422 to +427
} on Exception catch (e) {
throwToolExit(
'Failed to retrieve widget previews from the Dart Tooling Daemon (DTD). '
'Ensure that the analysis server is running and reachable. Details: $e',
);
}

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.

medium

The PR description mentions that StateError (specifically connection-closed) was causing crashes. However, this try-catch block only catches Exception. Since StateError is a subclass of Error, not Exception, it will still bubble up and cause a crash here. Consider catching both Exception and StateError to ensure graceful handling as intended.

      } catch (e) {\n        if (e is! Exception && e is! StateError) {\n          rethrow;\n        }\n        throwToolExit(\n          'Failed to retrieve widget previews from the Dart Tooling Daemon (DTD). '\n          'Ensure that the analysis server is running and reachable. Details: $e',\n        );\n      }

} catch (e) {
if (_disposed || shutdownHooks.isShuttingDown) {
logger.printTrace('Failed to get widget previews during shutdown: $e');
} else if (e is RpcException || e is StateError || e is Exception) {

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.

medium

The check e is RpcException is redundant because RpcException is a subclass of Exception, which is already included in the condition. This can be simplified to improve readability.

Suggested change
} else if (e is RpcException || e is StateError || e is Exception) {
} else if (e is StateError || e is Exception) {
References
  1. Suggest simplification and refactoring to enhance readability and maintainability. (link)

- Catch StateError in addition to Exception during widget-preview startup to prevent crashes.
- Remove redundant RpcException check and unused import in lsp_preview_detector.dart.

TAG=agy

CONV=bd211533-a25a-4fe2-afbc-c14902fc9bca
@github-actions github-actions Bot removed the CICD Run CI/CD label May 22, 2026
@bkonyi bkonyi added CICD Run CI/CD and removed CICD Run CI/CD labels May 22, 2026
@fluttergithubbot

Copy link
Copy Markdown
Contributor

An existing Git SHA, 9650e86a909fbe599e9076bcb264a3c6d8afa89a, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

@github-actions github-actions Bot removed the CICD Run CI/CD label May 22, 2026
@bkonyi bkonyi added CICD Run CI/CD and removed CICD Run CI/CD labels May 22, 2026
@bkonyi bkonyi changed the base branch from main to master May 22, 2026 20:43
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label May 22, 2026
@fluttergithubbot

Copy link
Copy Markdown
Contributor

An existing Git SHA, 9637a10e121f956d780548e96976c673f3675df8, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

@github-actions github-actions Bot removed the CICD Run CI/CD label May 22, 2026
@bkonyi bkonyi added the CICD Run CI/CD label May 25, 2026
@bkonyi bkonyi requested a review from srawlins May 25, 2026 17:36
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label May 25, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue May 26, 2026
Merged via the queue into flutter:master with commit 610419e May 26, 2026
166 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label May 26, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request May 26, 2026
Roll Flutter from e03b91f1fe34 to f3a4b9897834 (63 revisions)

flutter/flutter@e03b91f...f3a4b98

2026-05-26 [email protected] Update batch release doc to reflect latest workflow (flutter/flutter#186979)
2026-05-26 [email protected] Roll Skia from 0442274cc696 to 27a819894f7c (5 revisions) (flutter/flutter#187094)
2026-05-26 [email protected] [Tool Robustness] Gracefully handle asynchronous subprocess crashes and connection timeouts (flutter/flutter#186964)
2026-05-26 [email protected] [pubspec] Bump Dart SDK constraint to ^3.13.0 (flutter/flutter#186957)
2026-05-26 [email protected] Roll Dart SDK from 7eb54169841d to 00e625453c43 (1 revision) (flutter/flutter#187086)
2026-05-26 [email protected] [Impeller] Retire Y-coord-scale plumbing by flipping GLES at the vertex stage (flutter/flutter#186556)
2026-05-26 [email protected] Roll Skia from f4f294bdf98d to 0442274cc696 (2 revisions) (flutter/flutter#187079)
2026-05-26 [email protected] [flutter_tools] Fix version cache poisoning from git environment variables (flutter/flutter#186595)
2026-05-26 [email protected] [Tool] Handle DTD connection failures gracefully in widget-preview (flutter/flutter#186952)
2026-05-25 [email protected] Roll Skia from 9d1adb5f2427 to f4f294bdf98d (1 revision) (flutter/flutter#187056)
2026-05-25 [email protected] Roll Skia from 4dd78179e6ec to 9d1adb5f2427 (1 revision) (flutter/flutter#187048)
2026-05-25 [email protected] Roll Skia from 1f26101197bf to 4dd78179e6ec (4 revisions) (flutter/flutter#187044)
2026-05-24 [email protected] Roll Skia from bbe9ccc2bdbf to 1f26101197bf (1 revision) (flutter/flutter#187016)
2026-05-24 [email protected] Roll Fuchsia Linux SDK from nsgcNDlZOuweOvy3Q... to Itd2Jq_ZIABH2rW7B... (flutter/flutter#187032)
2026-05-23 [email protected] Roll Dart SDK from 7e0f28eb5315 to 7eb54169841d (1 revision) (flutter/flutter#187005)
2026-05-23 [email protected] Roll Dart SDK from 90e55fa88456 to 7e0f28eb5315 (1 revision) (flutter/flutter#186990)
2026-05-23 [email protected] Roll Fuchsia Linux SDK from 6T6BY9PTftoG3vP_1... to nsgcNDlZOuweOvy3Q... (flutter/flutter#186984)
2026-05-23 [email protected] iOS] Migrate VSyncClient to a pure Obj-C implementation (#186166) (flutter/flutter#186935)
2026-05-23 [email protected] Disables embedder_tests.cm for fuchsia (flutter/flutter#186969)
2026-05-23 [email protected] Roll Dart SDK from b8414c46f6c7 to 90e55fa88456 (2 revisions) (flutter/flutter#186977)
2026-05-22 [email protected] Roll Skia from 6fdb013d1953 to bbe9ccc2bdbf (1 revision) (flutter/flutter#186980)
2026-05-22 [email protected] [web] Fix cutoff text in WebParagraph (flutter/flutter#186819)
2026-05-22 [email protected] fix(web): Removes the iterative downscaling hack (flutter/flutter#186914)
2026-05-22 [email protected] opts the linux embedder into sdf rendering (flutter/flutter#186909)
2026-05-22 [email protected] Roll Skia from dae8778ca40d to 6fdb013d1953 (5 revisions) (flutter/flutter#186970)
2026-05-22 [email protected] Fix hooks inputs outputs rebuilt (flutter/flutter#186701)
2026-05-22 [email protected] adds linux impeller integration test for external textures (flutter/flutter#186759)
2026-05-22 [email protected] fix(flutter_tools): defensively catch DWDS unregistered service extension errors (flutter/flutter#186896)
2026-05-22 [email protected] [Impeller] Add golden harness support to the renderer test layer (flutter/flutter#186735)
2026-05-22 [email protected] [web] Remove image codecs from canvaskit_chromium (flutter/flutter#178133)
2026-05-22 [email protected] opts all macos into wide gamut (flutter/flutter#186277)
2026-05-22 [email protected] Roll Skia from 356185490a75 to dae8778ca40d (9 revisions) (flutter/flutter#186949)
2026-05-22 [email protected] Filter out SwiftPM schemes when fetching schemes (flutter/flutter#186006)
2026-05-22 [email protected] Roll Packages from 3754d04 to 69cf959 (1 revision) (flutter/flutter#186950)
2026-05-22 [email protected] Roll Dart SDK from eca46bec956d to b8414c46f6c7 (2 revisions) (flutter/flutter#186944)
2026-05-22 [email protected] Saves a DeviceHolderVK with the CommandPoolVK (flutter/flutter#186749)
2026-05-22 [email protected] Roll Dart SDK from e0d509fd676e to eca46bec956d (1 revision) (flutter/flutter#186922)
2026-05-22 [email protected] [ Tool ] Stop generating widget preview scaffold under $TMP (flutter/flutter#186476)
2026-05-21 [email protected] Fix typo in StretchingOverscrollIndicator docs (flutter/flutter#186897)
2026-05-21 [email protected] Roll Dart SDK from 28c7cb5a8e8d to e0d509fd676e (1 revision) (flutter/flutter#186903)
2026-05-21 [email protected] Fix some issues in the integration between EmbedderExternalViewEmbedder and Impeller (flutter/flutter#184905)
2026-05-21 [email protected] Fix a potential buffer overflow in the animated PNG decoder when parsing malformed fdAT chunks (flutter/flutter#186700)
2026-05-21 [email protected] Roll Skia from 2ff20950975d to 356185490a75 (5 revisions) (flutter/flutter#186892)
2026-05-21 [email protected] Add primitive shadows to rendering benchmark (flutter/flutter#186779)
2026-05-21 [email protected] Move prefetchSwiftPackages to be per platform (flutter/flutter#186468)
2026-05-21 [email protected] Upgrade iOS version (flutter/flutter#186889)
...
@bkonyi bkonyi added cp: beta cherry pick this pull request to beta release candidate branch cp: stable cherry pick this pull request to stable release candidate branch labels May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD cp: beta cherry pick this pull request to beta release candidate branch cp: stable cherry pick this pull request to stable release candidate branch tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[widget-preview] Command and file-watching stream crash when DTD connection is lost or fails at startup

3 participants