Repository: flutter
Issue: bug: a few hot restarts cause a canvaskit failure
Labels: tool,engine,t: hot reload,platform-web,e: web_canvaskit,has reproducible steps,assigned for triage,P3,team-web,triaged-web,found in release: 3.16,found in release: 3.18

### Steps to reproduce

1. Open example in VSCode, chose web platform
2. Set a breakpoint on line that calls runApp
3. Debug
4. Then the breakpoint is hit, click hot restart a few times

Issues related:

https://github.com/flutter/flutter/issues/90236

### Expected results

Not error when make hot reload

### Actual results

The app is freezed and show a error in console. The dialog of "Flutter: Hot reloading" not close.

![Captura de pantalla 2023-12-27 a la(s) 10 25 48 p m](https://github.com/flutter/flutter/assets/26438532/c9723e24-77de-4d33-9689-60f067a0d7e6)

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Material App Bar'),
        ),
        body: const Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

```

</details>

### Logs

<details open><summary>Logs</summary>

```console
Launching lib/main.dart on Chrome in debug mode...
This app is linked to the debug service: ws://127.0.0.1:64538/SH-yEsqGG2I=/ws
Debug service listening on ws://127.0.0.1:64538/SH-yEsqGG2I=/ws
Connecting to VM Service at ws://127.0.0.1:64538/SH-yEsqGG2I=/ws
Restarted application in 1,307ms.
Error: Expected a wrapped Dart object, but got a JS object or a wrapped Dart object from a separate runtime instead.
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3       throw_
dart-sdk/lib/_internal/js_shared/lib/js_interop_patch.dart 89:7                   JSBoxedDartObjectToObject.get$35toDart
lib/_engine/engine/canvaskit/native_memory.dart 24:46                             <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 574:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 579:39  dcall
```

</details>

In some case show this error to

```console
Restarted application in 2,200ms.
Error: Expected a wrapped Dart object, but got a JS object or a wrapped Dart object from a separate runtime instead.
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3       throw_
dart-sdk/lib/_internal/js_shared/lib/js_interop_patch.dart 89:7                   JSBoxedDartObjectToObject.get$35toDart
lib/_engine/engine/canvaskit/native_memory.dart 24:46                             <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 574:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 579:39  dcall
```

### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.16.5, on macOS 14.2 23C64 darwin-arm64, locale es-US)
    • Flutter version 3.16.5 on channel stable at /Users/yeikel/fvm/versions/3.16.5
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 78666c8dc5 (hace 8 días), 2023-12-19 16:14:14 -0800
    • Engine revision 3f3e560236
    • Dart version 3.2.3
    • DevTools version 2.28.4

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/yeikel/Library/Android/sdk/
    • Platform android-34, build-tools 33.0.1
    • ANDROID_HOME = /Users/yeikel/Library/Android/sdk/
    • Java binary at: /Applications/Android Studio 2.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C65
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio 2020.3.1.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio 2.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.78.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 14.2 23C64 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 120.0.6099.129

[✓] Network resources
    • All expected network resources are available.
```

</details>