Repository: flutter
Issue: `flutter attach` doesn't work on physical iOS device in larger apps
Labels: platform-ios,tool,has reproducible steps,P2,found in release: 3.7,found in release: 3.9,team-ios,triaged-ios

I have a problem with `flutter attach` not working in larger apps. Of course, "larger apps" is subjective. I tried to reproduce this issue in 3 apps:

1. the default app (`flutter create example --platforms ios,android`) (1 dependency, not reproduced)
2. [example app](https://github.com/leancodepl/patrol/tree/master/packages/patrol/example) from [the Patrol project](https://patrol.leancode.co) (7 dependencies, reproduced)
3. [my private, closed-source app](https://play.google.com/store/apps/details?id=pl.baftek.discoverrudy) (50 dependencies, reproduced)

## Steps to Reproduce

1. Connect the physical iOS device
2. Clone [the Patrol repository](https://github.com/leancodepl/patrol/tree/master/packages/patrol/example) and `cd packages/patrol/example`
4. `flutter run`, and then `d` (to detach after the app starts)
5. If the "Local network" permission dialog shows up on the device, accept it
6. `flutter attach --app-id com.example.example` (PS This could be another bug report: `--app-id` is always required when using `flutter attach` on iOS – otherwise it fails).

**Expected results:**

After a short wait, `flutter attach` connects to the Dart VM running inside the application and I can debug my app.

**Actual results:**

The message `Waiting for a connection from Flutter on Barteks-iPhone...` shows up and nothing happens, even after a few minutes.

Also, sometimes (again, sorry for these "sometimes", but I just cannot deterministically reproduce these issues) the command crashes right away with:

<details>
<summary>Expand crash logs</summary>

```
(skipped)
[  +36 ms] Checking for advertised Dart observatories...
[+5014 ms] Checking for available port on com.example.example (2)._dartobservatory._tcp.local
[   +1 ms] Checking for authentication code for com.example.example (2)._dartobservatory._tcp.local
[   +8 ms] Attempting to forward device port 51455 to host port 51469
[   +1 ms] executing: /Users/bartek/fvm/versions/3.7.7/bin/cache/artifacts/usbmuxd/iproxy 51469:51455 --udid 00008020-001C499A3420802E --debug
[+1012 ms] Forwarded port ForwardedPort HOST:51469 to DEVICE:51455
[  +35 ms] Connecting to service protocol: http://127.0.0.1:51469/eb5BKd6n5VE=/
[  +12 ms] Fail to connect to service protocol: http://127.0.0.1:51469/eb5BKd6n5VE=/: HttpException: Connection reset by peer, uri =
http://127.0.0.1:51469/eb5BKd6n5VE=/ws
[   +1 ms] Error connecting to the service protocol: failed to connect to http://127.0.0.1:51469/eb5BKd6n5VE=/
[   +1 ms] Un-forwarding port ForwardedPort HOST:51469 to DEVICE:51455
[   +4 ms] "flutter attach" took 8,879ms.
[   +3 ms]
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
           #1      AttachCommand._attachToDevice (package:flutter_tools/src/commands/attach.dart:414:11)
           <asynchronous suspension>
           #2      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #3      AttachCommand.runCommand (package:flutter_tools/src/commands/attach.dart:254:5)
           <asynchronous suspension>
           #4      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1257:27)
           <asynchronous suspension>
           #5      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #6      CommandRunner.runCommand (package:args/command_runner.dart:209:13)
           <asynchronous suspension>
           #7      FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:283:9)
           <asynchronous suspension>
           #8      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #9      FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:229:5)
           <asynchronous suspension>
           #10     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:64:9)
           <asynchronous suspension>
           #11     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #12     main (package:flutter_tools/executable.dart:91:3)
           <asynchronous suspension>


[  +87 ms] ensureAnalyticsSent: 86ms
[        ] Running 1 shutdown hook
[        ] Shutdown hooks complete
[        ] exiting with code 2
```
</details>

### Environment

Host machine: MacBook Air M1, macOS Ventura (13.2.1 (22D68))

Tested my personal devices:
- iPhone 12 mini with iOS 16.3.1
- iPad Air 3 with iPad OS 16.3.1

<details>
  <summary>Logs</summary>

```console
$ flutter doctor --verbose
[✓] Flutter (Channel stable, 3.7.7, on macOS 13.2.1 22D68 darwin-arm64, locale en-PL)
    • Flutter version 3.7.7 on channel stable at /Users/bartek/fvm/versions/3.7.7
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2ad6cd72c0 (7 days ago), 2023-03-08 09:41:59 -0800
    • Engine revision 1837b5be5f
    • Dart version 2.19.4
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/bartek/androidsdk
    • Platform android-33, build-tools 33.0.1
    • ANDROID_HOME = /Users/bartek/androidsdk
    • Java binary at: /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9619390/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/221.6008.13.2211.9619390/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.15+0-b2043.56-8887301)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.3.3)
    • IntelliJ at /Users/bartek/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 72.1.4
    • Dart plugin version 223.8888

[✓] IntelliJ IDEA Ultimate Edition (version 2022.3.3)
    • IntelliJ at /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/223.8836.41/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.76.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.60.0

[✓] Connected device (3 available)
    • Barteks-iPhone (mobile) • 00008101-001611D026A0001E • ios            • iOS 16.3.1 20D67
    • macOS (desktop)         • macos                     • darwin-arm64   • macOS 13.2.1 22D68 darwin-arm64
    • Chrome (web)            • chrome                    • web-javascript • Google Chrome 111.0.5563.64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

```

</details>

Issues that might be related:
- https://github.com/flutter/flutter/issues/72161
- https://github.com/flutter/flutter/issues/122303