Repository: flutter
Issue: IsolateNameServer crashes on release mode with flutter 3.3.x but works with flutter 3.0.x
Labels: platform-android,engine,P2,team-android,triaged-android

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

I am using ```IsolateNameServer``` to handle background messages comes from firebase cloud messaging. When app receives background message from FCM, ```_firebaseMessagingBackgroundHandler``` was executed properly until ```sendPort.send(message)```. At ```sendPort.send``` app crashes without giving any visual clue or any log. The same code works as expected with Flutter 3.0.5, but does not work with any version of flutter 3.3.x.

Similar problem was reported here 2 months ago and closed as fixed. But it looks like similar problem still exists: https://github.com/flutter/flutter/issues/109248

PS: It is not easy to publish whole source code. Because it is not an open source code project and it has about 20K lines of dart code. It includes a lot of project specific codes like firebase configuration and cloud code scripts to send FCM messages.

**Expected results:** <!-- what did you want to see? -->
 Program execution should continue with ```Shared.debug("new firebase cloud message from port")```

**Actual results:** <!-- what did you see? -->
 App crashes without any visual outcome or any log.

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

``` 
  @pragma('vm:entry-point')
  static Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    try {
      Shared.firebaseApp ??= await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      Shared.debug("Handling a background message");
      Shared.debug("background message sending to port");
      final SendPort? sendPort = IsolateNameServer.lookupPortByName('firebaseCloudMessagePort');
      if (sendPort == null) {
        Shared.debug("sendPort is null. message not send");
      } else {
        Shared.debug("sending message to sendPort");
        sendPort.send(message);
      }
    } catch (error) {
      Shared.debug("Error on _firebaseMessagingBackgroundHandler: "+error.toString());
    }
  }
...
    ReceivePort receivePort = ReceivePort();
    IsolateNameServer.registerPortWithName(receivePort.sendPort, 'firebaseCloudMessagePort');
    receivePort.listen((dynamic data) async {
      Shared.debug("new firebase cloud message from port");
      await Shared.handleFirebaseCloudMessage(data, "terminated", updateView);
      if (Shared.notificationView != null) {
        setView(Shared.notificationView!);
        Shared.notificationView = null;
      }
    });
```

</details>

<details>
  <summary>Logs</summary>

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue. If there is a crash, please include
      the entirety of the crash log including the backtrace.
-->

```
flutter run --release
Installing build/app/outputs/flutter-apk/app.apk...                34.4s
Flutter run key commands.
h List all available interactive commands.
c Clear the screen
q Quit (terminate the application on the device).
D/FlutterLocationService( 3869): Creating service.
D/FlutterLocationService( 3869): Binding to location service.
I/flutter ( 3869): *** RELEASE *** Notifications.init started
W/FlutterJNI( 3869): FlutterJNI.loadLibrary called more than once
I/flutter ( 3869): *** RELEASE *** No initial message
W/FlutterJNI( 3869): FlutterJNI.prefetchDefaultFontManager called more than once
W/FlutterJNI( 3869): FlutterJNI.init called more than once
I/flutter ( 3869): *** RELEASE *** Handling a background message
I/flutter ( 3869): *** RELEASE *** background message sending to port
I/flutter ( 3869): *** RELEASE *** sending message to sendPort
```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
flutter analyze
Analyzing armdate...                                                    
No issues found! (ran in 6.5s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
flutter doctor -v
[✓] Flutter (Channel stable, 3.3.5, on Ubuntu 22.04.1 LTS 5.15.0-50-generic, locale en_US.UTF-8)
    • Flutter version 3.3.5 on channel stable at /home/coolapps/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d9111f6402 (2 days ago), 2022-10-19 12:27:13 -0700
    • Engine revision 3ad69d7be3
    • Dart version 2.18.2
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /home/coolapps/development/sdk
    • Platform android-33, build-tools 33.0.0
    • ANDROID_HOME = /home/coolapps/development
    • Java binary at: /home/coolapps/development/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 14.0.0-1ubuntu1
    • cmake version 3.22.1
    • ninja version 1.10.1
    • pkg-config version 0.29.2

[✓] Android Studio (version 2021.3)
    • Android Studio at /home/coolapps/development/android-studio
    • Flutter plugin version 70.2.3
    • Dart plugin version 213.7433
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] Connected device (3 available)
    • SM M526BR (mobile)           • 192.168.0.29:35611 • android-arm64 • Android 12 (API 31)
    • sdk gphone64 x86 64 (mobile) • emulator-5556      • android-x64   • Android 13 (API 33) (emulator)
    • Linux (desktop)              • linux              • linux-x64     • Ubuntu 22.04.1 LTS 5.15.0-50-generic

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->