Repository: flutter
Issue: [macOS] FlutterEngine shutDownEngine method cause DeadLock
Labels: engine,platform-mac,a: existing-apps,a: desktop,has reproducible steps,P2,found in release: 2.10,found in release: 2.13,team-macos,triaged-macos

As described in issue  [#101014](https://github.com/flutter/flutter/issues/101014) ,FlutterEngine will case memory leaks after FlutterViewController dealloc. So we try to call the shutDownEngine method directly to avoid the leak. But unfortunately, we founnd out this may lead to deadlocks.

---

## Steps to Reproduce

1. Create a macOS demo
2. Copy and paste 'Code Sample' into MainFlutterWindow.swift.
3. flutter run --debug
4. The first window will be displayed after the Demo started, and the second window will show after 5 seconds.
5. When we close the second window, sometimes the App will freezes.

**Expected results:**
No dealocks.

**Actual results:**
Occasionally deadlock.

<details>
<summary>Code sample</summary>

```dart
class DemoFlutterViewController: FlutterViewController {
    deinit {
        self.engine.shutDownEngine()
    }
}

class MainFlutterWindow: NSWindow {
    override func awakeFromNib() {
        let flutterViewController = FlutterViewController.init()
        let windowFrame = self.frame
        self.contentViewController = flutterViewController
        self.setFrame(windowFrame, display: true)
        
        RegisterGeneratedPlugins(registry: flutterViewController)
        
        super.awakeFromNib()
        
        self.perform(#selector(showWindow2), with: nil, afterDelay: 5)
    }
    
    @objc func showWindow2() {
        
        let flutterViewController = DemoFlutterViewController.init()
        var windowFrame = self.frame
        windowFrame.origin = CGPoint.init(x: windowFrame.origin.x + 50, y: windowFrame.origin.y + 50)

        let sub1Window = NSWindow(contentViewController: flutterViewController)
        sub1Window.contentViewController = flutterViewController
        sub1Window.setFrame(windowFrame, display: true)

        RegisterGeneratedPlugins(registry: flutterViewController)

        sub1Window.makeKeyAndOrderFront(nil)
    }
}
```

</details>

<details>
  <summary>Logs</summary>

```
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.10.3, on macOS 12.1 21C52 darwin-arm, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.65.2)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

! Doctor found issues in 1 category.
```

</details>

----

## Diagnosis and Suggestion
Through the dealock stack we found out, the ui thread locked with the raster thead.

UI Thread:
![dealock-1](https://user-images.githubusercontent.com/3714809/160781833-8b2c422d-2e48-4fd0-9235-3bd713f75d36.png)

Rater Thread:
![dealock-2](https://user-images.githubusercontent.com/3714809/160781757-57dfc27a-7cb8-43a6-980f-bdf3a43cd538.png)

After further analysis we found that the root cause of this BUG is because [FlutterView shutdown] is not called:
![deadlock-3](https://user-images.githubusercontent.com/3714809/160781890-862d4efe-794f-47c4-a427-3b90b79d79e5.png)

Raster thread is still working when FlutterEngine is shuting down.

### Suggestion
FlutterVIewController can call  [FlutterView shutdown]  before [FlutterEngine shutDownEngine].