Repository: flutter
Issue: CGO dll works with dart standalone, but makes flutter die on windows
Labels: platform-windows,a: desktop,P3,team-windows,triaged-windows

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

I generate a dynamic library from Go code (basically an HTTP server, and an mDNS server) with CGO. (`CGO_ENABLED=1 go build -buildmode=c-shared`). I redo that step (generate an .so when on Unix, or a .dll when on windows) for each separate platform of course.
Then I use it with ffi.DynamicLibrary.open , lookup a function, call it, etc.

It works without any problem on Mac. (with `flutter run -d macos`)

On windows (on a windows machine, with `flutter run -d windows`), the call to .open seems to work (try catch around it does not show any exception), but right after that, the dart VM seems to die? The very first time this happens, the message is:

```
A Dart VM Service on Windows is available at: http://127.0.0.1:59115/otsWGWwNHvw=/
Lost connection to device.
```

Then all the subsequent runs fail with:

```
Error connecting to the service protocol: failed to connect to http://127.0.0.1:59148/gYFWwYfFGH0=/
```

as the only indication. No stacktrace, no other error message. The GUI from my app does not even have time to show up.

But here's the real kicker:

1) If I open and use the dll just with dart on windows (no flutter), no problem, it works.

2) If I ssh (from my mac) to windows (yeah, there's sshd on windows, who knew...), and if I run the app in flutter that way, then of course I get some error messages due to the fact that the GUI cannot show up (because over ssh), but then the dll actually loads and works too!

Given that there's no output to work with, I'm really stumped as how to debug that.
Does anyone have any suggestion on how I could get more debugging info about what's going on?

Update: with --verbose enabled, I get this:

```
[   +4 ms] √  Built build\windows\runner\Debug\my_app.exe.
[ +329 ms] VM Service URL on device: http://127.0.0.1:59186/dWdn3z3UvSc=/
[   +5 ms] Caching compiled dill
[  +58 ms] Connecting to service protocol: http://127.0.0.1:59186/dWdn3z3UvSc=/
[ +152 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at http://127.0.0.1:59186/dWdn3z3UvSc=/.
[  +92 ms] DDS is listening at http://127.0.0.1:59189/RMAiJllI4EM=/.
[ +868 ms] Fail to connect to service protocol: http://127.0.0.1:59186/dWdn3z3UvSc=/: Exception: DDS shut down too early
[   +1 ms] Error connecting to the service protocol: failed to connect to http://127.0.0.1:59186/dWdn3z3UvSc=/
[   +5 ms] "flutter run" took 13,581ms.
[   +4 ms]
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
           #1      RunCommand.runCommand (package:flutter_tools/src/commands/run.dart:757:9)
           <asynchronous suspension>
           #2      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1336:27)
           <asynchronous suspension>
           #3      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #4      CommandRunner.runCommand (package:args/command_runner.dart:212:13)
           <asynchronous suspension>
           #5      FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:296:9)
           <asynchronous suspension>
           #6      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #7      FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:244:5)
           <asynchronous suspension>
           #8      run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:91:9)
           <asynchronous suspension>
           #9      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #10     main (package:flutter_tools/executable.dart:91:3)
           <asynchronous suspension>


[  +26 ms] ensureAnalyticsSent: 20ms
[        ] Running 1 shutdown hook
[   +6 ms] Shutdown hooks complete
[        ] exiting with code 2
```

Does anyone know how I can get more info as to why the DDS died?


### Expected results

I would not expect the DDS to die.

I would expect loading the dll not to cause any problem, especially since it works in standalone dart.


### Actual results

The app dies immediately. Apparently because the DDS dies?

### Code sample

Would it actually help if I shared a minimum Go source to reproduce the problem, and how to create the dll from it?
If yes, then I'll definitely do it.
Then it's just the regular dart:ffi way of loading it, as described on https://dart.dev/guides/libraries/c-interop


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs</summary>

```console
[   +4 ms] √  Built build\windows\runner\Debug\my_app.exe.
[ +329 ms] VM Service URL on device: http://127.0.0.1:59186/dWdn3z3UvSc=/
[   +5 ms] Caching compiled dill
[  +58 ms] Connecting to service protocol: http://127.0.0.1:59186/dWdn3z3UvSc=/
[ +152 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at http://127.0.0.1:59186/dWdn3z3UvSc=/.
[  +92 ms] DDS is listening at http://127.0.0.1:59189/RMAiJllI4EM=/.
[ +868 ms] Fail to connect to service protocol: http://127.0.0.1:59186/dWdn3z3UvSc=/: Exception: DDS shut down too early
[   +1 ms] Error connecting to the service protocol: failed to connect to http://127.0.0.1:59186/dWdn3z3UvSc=/
[   +5 ms] "flutter run" took 13,581ms.
[   +4 ms]
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
           #1      RunCommand.runCommand (package:flutter_tools/src/commands/run.dart:757:9)
           <asynchronous suspension>
           #2      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1336:27)
           <asynchronous suspension>
           #3      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #4      CommandRunner.runCommand (package:args/command_runner.dart:212:13)
           <asynchronous suspension>
           #5      FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:296:9)
           <asynchronous suspension>
           #6      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #7      FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:244:5)
           <asynchronous suspension>
           #8      run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:91:9)
           <asynchronous suspension>
           #9      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #10     main (package:flutter_tools/executable.dart:91:3)
           <asynchronous suspension>


[  +26 ms] ensureAnalyticsSent: 20ms
[        ] Running 1 shutdown hook
[   +6 ms] Shutdown hooks complete
[        ] exiting with code 2
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
$ flutter doctor -v
[√] Flutter (Channel stable, 3.10.5, on Microsoft Windows [Version 10.0.19045.3086], locale en-150)
    • Flutter version 3.10.5 on channel stable at E:\sdk\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (2 weeks ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at E:/sdk/Android/Sdk/
    • Platform android-33, build-tools 33.0.2
    • Java binary at: E:\sdk\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Build Tools 2022 17.6.1)
    • Visual Studio at E:\sdk\Microsoft Visual Studio\2022\BuildTools
    • Visual Studio Build Tools 2022 version 17.6.33717.318
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2022.2)
    • Android Studio at E:\sdk\Android\Android Studio
    • 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-b2043.56-9586694)

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.3086]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 114.0.5735.135
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 114.0.1823.43

[√] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>