Skip to content

Conversation

@jyameo
Copy link
Contributor

@jyameo jyameo commented Oct 14, 2025

This PR fixes crashes during hot reload/restart operations on web-server devices that occur when browser tabs are closed. Previously, the VM service would throw "Bad state: No element" errors when attempting operations with no connected clients. The fix introduces graceful handling on both sides: DWDS now catches NoClientsAvailableException in its hot reload/restart operations and returns structured JSON responses with a noClientsAvailable boolean flag instead of throwing exceptions. Flutter Tools reads this flag via a helper method _checkNoClientsAvailable() and handles the scenario gracefully by displaying "Recompile complete. No client connected." while preserving the DWDS connection to automatically support browser reconnections without requiring a full restart of the Flutter tools.

Fixes: #174791

Changes in DWDS (Parent PR): dart-lang/webdev#2699

Follow up bug: dart-lang/sdk#61757

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@github-actions github-actions bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Oct 14, 2025
@jyameo jyameo changed the title Fixed Hot Reload/Restart Crashes on Web-server Device After Browser Tab Closure Fixed hot reload/restart crashes after closing browser tab on web-server device Oct 14, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 addresses a crash that occurs during hot reload/restart on web-server devices after a browser tab is closed. The changes introduce try-catch blocks to handle exceptions during hot reload and restart operations. A new helper method, _handleNoClientsAvailableErrorIfFound, is added to specifically detect "No clients available" errors from DWDS by checking the error message string. When this error is detected, another new method, _invalidateConnection, is called to reset connection-related state, preventing the crash. My review includes suggestions to refactor duplicated error handling logic and to replace a magic string with a constant for better maintainability.

@jyameo jyameo requested review from bkonyi and srujzs October 14, 2025 20:20
Copy link
Contributor

@srujzs srujzs left a comment

Choose a reason for hiding this comment

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

Thanks! I'd consider adding a test as well where we close Chrome and then run a restart and/or reload.

await _vmService.service.callMethod(hotRestartMethod);
try {
await _vmService.service.callMethod(hotRestartMethod);
} on Exception catch (e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, can we instead put this in the response from callMethod in DWDS? I believe we could place this in the json. My concern is mostly that this exception handling is brittle. If we ever decide to change the exception text, we'd end up breaking Flutter. Additionally, if some other unrelated exception happens to have this text, we'd be treating the error incorrectly. Similarly, in reloadSources, we may want _performWebSocketHotReload to return an error value if there are no clients that we can then encode in the ReloadReport rather than directly throwing and catching.

I think it's just a bit cleaner to encode errors/exceptions we can recover from in the response rather than catching errors and then checking for the text.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! makes sense to me. I’ve made the corresponding changes in DWDS in this PR: dart-lang/webdev#2699, and updated the code here accordingly. I also added a test case to cover this scenario.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the changes and tests! It looks like Ben has some comments on the right error to bubble up on the DWDS PR. I'm guessing some of the code here will change after that so I'll re-review then.

@jyameo jyameo requested a review from srujzs October 15, 2025 19:08
_logger.printTrace('No browser clients currently connected');
status.stop();
_logger.printStatus(kNoClientConnectedMessage);
return OperationResult.ok;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be ok? Or are we doing this to suppress an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that’s right. That’s currently what we’re doing when attempting a hot reload without the initial DWDS connection. We do this to exit the method early before running into an error.

For reference: resident_web_runner.dart#L492

@jyameo jyameo requested a review from srujzs October 16, 2025 20:49
@jyameo jyameo added cp: beta cherry pick this pull request to beta release candidate branch and removed cp: beta cherry pick this pull request to beta release candidate branch labels Oct 20, 2025
@jyameo jyameo enabled auto-merge October 20, 2025 16:16
Copy link
Contributor

@srujzs srujzs left a comment

Choose a reason for hiding this comment

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

LGTM with the DWDS changes, thanks!

@jyameo jyameo added this pull request to the merge queue Oct 20, 2025
Merged via the queue into flutter:master with commit 47d35a2 Oct 20, 2025
148 of 151 checks passed
@jyameo jyameo deleted the jy-174791 branch October 20, 2025 17:35
@jyameo jyameo added the cp: beta cherry pick this pull request to beta release candidate branch label Oct 20, 2025
@flutteractionsbot
Copy link

Failed to create CP due to merge conflicts.
You will need to create the PR manually. See the cherrypick wiki for more info.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 20, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 20, 2025
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Oct 20, 2025
flutter/flutter@891d7d5...2d34167

2025-10-20 [email protected] Cleanup create_updated_flutter_deps.py a bit (flutter/flutter#177162)
2025-10-20 [email protected] Fixed hot reload/restart crashes after closing browser tab on web-server device (flutter/flutter#177026)
2025-10-20 [email protected] Roll Skia from 0a3ace6fde82 to ed4294faecde (2 revisions) (flutter/flutter#177249)
2025-10-20 [email protected] Add DropdownMenu.decorationBuilder (flutter/flutter#176264)
2025-10-20 [email protected] Roll Skia from 05e2f42f533d to 0a3ace6fde82 (1 revision) (flutter/flutter#177242)
2025-10-20 [email protected] Roll Skia from 89abc5393317 to 05e2f42f533d (1 revision) (flutter/flutter#177238)
2025-10-20 [email protected] [ios][engine] Fix autofill context cleanup and view lifecycle management (flutter/flutter#173598)
2025-10-20 [email protected] Fix Image.network not using cache when headers are specified (flutter/flutter#176831)
2025-10-19 [email protected] Roll Dart SDK from a66f334fee2a to 2cd2106f2cef (4 revisions) (flutter/flutter#177190)
2025-10-19 [email protected] Roll Skia from 2d424175a481 to 89abc5393317 (1 revision) (flutter/flutter#177235)
2025-10-19 [email protected] Make sure that a ListTile doesn't crash in 0x0 environment (flutter/flutter#176176)
2025-10-19 [email protected] Make sure that a DropdownButton doesn't crash in 0x0 environment (flutter/flutter#174880)
2025-10-19 [email protected] Roll Skia from 899155871d29 to 2d424175a481 (1 revision) (flutter/flutter#177229)
2025-10-19 [email protected] Roll Skia from b864c56efb66 to 899155871d29 (1 revision) (flutter/flutter#177227)
2025-10-19 [email protected] Roll Fuchsia Linux SDK from M8WT2GMY46e_0fFho... to tKrvmvTOQITL81oOC... (flutter/flutter#177223)
2025-10-19 [email protected] Roll Skia from 0992b560454f to b864c56efb66 (1 revision) (flutter/flutter#177222)
2025-10-18 [email protected] Fix HEIF decoding (flutter/flutter#176860)
2025-10-18 [email protected] Roll Skia from 74df18176924 to 0992b560454f (1 revision) (flutter/flutter#177217)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
auto-submit bot pushed a commit that referenced this pull request Oct 27, 2025
… on web-server device (#177456)

**Impacted Users:**
Flutter web developers using the `web-server` device for hot reload/restart.

**Impact Description:**
Hot reload/restart crashes when the browser tab is closed, causing “Bad state: No element” errors and breaking the DWDS connection.

**Workaround:**
Rerun the app.

**Risk:**
Low — changes only affect hot reload/restart handling when no clients are connected.

**Test Coverage:**
Yes — covered by automated integration tests and verified with manual testing across reload/restart scenarios.

**Validation Steps:**

1. run the app on webserver `flutter run -d web-server`
2. open the url
3. do hot reload
4. close the browser
5. do hot reload 

You should see a warning `WebSocketProxyService: No clients available.` along with `Recompile complete. No client connected.` printed in the console. The app should no longer crash.

**Merged PR:** #177026
**Changes in DWDS (Parent PR):** [https://github.com/dart-lang/webdev/pull/2699](https://github.com/dart-lang/webdev/pull/2699)

Fixes #174791
reidbaker pushed a commit to AbdeMohlbi/flutter that referenced this pull request Dec 10, 2025
…ver device (flutter#177026)

This PR fixes crashes during hot reload/restart operations on web-server
devices that occur when browser tabs are closed. Previously, the VM
service would throw "Bad state: No element" errors when attempting
operations with no connected clients. The fix introduces graceful
handling on both sides: DWDS now catches NoClientsAvailableException in
its hot reload/restart operations and returns structured JSON responses
with a noClientsAvailable boolean flag instead of throwing exceptions.
Flutter Tools reads this flag via a helper method
_checkNoClientsAvailable() and handles the scenario gracefully by
displaying "Recompile complete. No client connected." while preserving
the DWDS connection to automatically support browser reconnections
without requiring a full restart of the Flutter tools.


Fixes: flutter#174791

Changes in DWDS (Parent PR):
dart-lang/webdev#2699

Follow up bug: dart-lang/sdk#61757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cp: beta cherry pick this pull request to beta 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.

[tool_crash] web-server crashing when reloading without clients

4 participants