Skip to content

Crash when returning StreamHandlerError on Windows #101682

@swift-kim

Description

@swift-kim

Steps to Reproduce

  1. Download the connectivity_plus plugin source code on a Windows machine.

  2. Modify connectivity_plus_windows_plugin.cpp as follows to simulate a platform error. This makes sure that the constructor of StreamHandlerError<> is called.

    --- a/packages/connectivity_plus/connectivity_plus_windows/windows/connectivity_plus_windows_plugin.cpp
    +++ b/packages/connectivity_plus/connectivity_plus_windows/windows/connectivity_plus_windows_plugin.cpp
    @@ -143,7 +143,7 @@ ConnectivityStreamHandler::OnListenInternal(
       auto callback =
          std::bind(&ConnectivityStreamHandler::AddConnectivityEvent, this);
    
    -  if (!manager->StartListen(callback)) {
    +  if (manager->StartListen(callback)) {
       return std::make_unique<FlStreamHandlerError>(
             std::to_string(manager->GetError()), "NetworkManager::StartListen",
             nullptr);
  3. Run the example app (connectivity_plus/example).

Expected results:

Prints a Dart exception.

══╡ EXCEPTION CAUGHT BY SERVICES LIBRARY ╞══════════════════════════════════════════════════════════
The following PlatformException was thrown while activating platform stream on channel
dev.fluttercommunity.plus/connectivity_status:
PlatformException(0, NetworkManager::StartListen, null, null)

When the exception was thrown, this was the stack:
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177:18)
<asynchronous suspension>
#2      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:516:9)
<asynchronous suspension>
════════════════════════════════════════════════════════════════════════════════════════════════════

Actual results:

Program aborted.

What's the cause?

As you can see in the C++ client wrapper's event_stream_handler.h, StreamHandlerError stores only the references to error_code and error_message, and the actual values must be retained by the caller of the API until the error information is sent to the framework. However, it is very natural for users of the API to expect that the strings are copied by value when the constructor of StreamHandlerError is called.

A temporary fix is to simply remove & from the code as follows, just like name_ of BasicMessageChannel.

--- a/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h
+++ b/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h
@@ -13,8 +13,8 @@ class EncodableValue;

 template <typename T = EncodableValue>
 struct StreamHandlerError {
-  const std::string& error_code;
-  const std::string& error_message;
+  const std::string error_code;
+  const std::string error_message;
   const T* error_details;

   StreamHandlerError(const std::string& error_code,
Logs
> flutter doctor -v
[✓] Flutter (Channel unknown, 2.10.1, on Microsoft Windows [Version 10.0.19043.1526], locale ko-KR)
    • Flutter version 2.10.1 at E:\flutter\flutter-tizen\flutter
    • Upstream repository unknown
    • Framework revision db747aa133 (9 weeks ago), 2022-02-09 13:57:35 -0600
    • Engine revision ab46186b24
    • Dart version 2.16.1
    • DevTools version 2.9.2

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Professional 2019 16.10.0)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional
    • Visual Studio Professional 2019 version 16.10.31321.278
    • Windows 10 SDK version 10.0.22000.0

[✓] VS Code, 64-bit edition (version 1.65.2)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19043.1526]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 100.0.4896.60
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 99.0.1150.39

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: pluginsSupport for writing, building, and running plugin packagesengineflutter/engine related. See also e: labels.platform-windowsBuilding on or for Windows specificallyr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions