Repository: flutter
Issue: Integration tests failing due to PathNotFoundException when debugging
Labels: tool,framework,a: debugging,f: integration_test,has reproducible steps,P2,team-tool,triaged-tool,found in release: 3.19,found in release: 3.20

### Steps to reproduce

Hello,

The integration tests I've written by themselves succeed, in Android Studio the test shows up as green (for a split second).
However when the tooling tries to uninstall the app from the device (Android Emulator), it fails to do so and then marks the test as failed as well.

So technically the test succeeded, but then the tooling failed for some reason marking the test as failed as well.

Note that this is not always. It feels like half of the time the test does fully succeed (including the uninstall), by running the exact same test several times in a row.

### Expected results

Either the tooling should not fail, or the test should still be marked as succeeded.

### Actual results

There are two possible outcomes:

**Full success**
1. Test succeeds (marked as green in Android Studio)
2. Uninstall succeeeds
3. All good!  (marked as green in Android Studio)

**Uninstall fails**
1. Test succeeds (marked as green in Android Studio, but only for a split second)
2. Uninstall succeeeds (marked as yellow in Android Studio)
3. Fail (marked as yellow in Android Studio)


```
adb uninstall failed: ProcessException: Process exited abnormally with exit code -2:

  Command: /Users/glennruysschaert/Library/Android/sdk/platform-tools/adb -s emulator-5554 uninstall app.getdriven.customer.staging.debug
unhandled error during finalization of test:
/Users/glennruysschaert/Code/get-driven-customer-app/integration_test/profile_options_test.dart
PathNotFoundException: Deletion failed, path = '/var/folders/l9/rsj1dsjd64b2hnrzxw53hvc80000gn/T/flutter_tools.VWvUbU/flutter_test_listener.Y3PBgI' (OS Error: No such file or directory, errno = 2)
#0      _Directory._deleteSync (dart:io/directory_impl.dart:188:7)
#1      FileSystemEntity.deleteSync (dart:io/file_system_entity.dart:407:7)
#2      ForwardingFileSystemEntity.deleteSync (package:file/src/forwarding/forwarding_file_system_entity.dart:70:16)
#3      FlutterPlatform._createListenerDart.<anonymous closure> (package:flutter_tools/src/test/flutter_platform.dart:601:15)
#4      FlutterPlatform._startTest (package:flutter_tools/src/test/flutter_platform.dart:565:26)
<asynchronous suspension>
#5      Future.wait.<anonymous closure> (dart:async/future.dart:518:21)
<asynchronous suspension>
#6      _FlutterPlatformStreamSinkWrapper.close.<anonymous closure> (package:flutter_tools/src/test/flutter_platform.dart:675:7)
<asynchronous suspension>

dart:io/directory_impl.dart 188:7                                     _Directory._deleteSync
dart:io/file_system_entity.dart 407:7                                 FileSystemEntity.deleteSync
package:file/src/forwarding/forwarding_file_system_entity.dart 70:16  ForwardingFileSystemEntity.deleteSync
package:flutter_tools/src/test/flutter_platform.dart 601:15           FlutterPlatform._createListenerDart.<fn>
package:flutter_tools/src/test/flutter_platform.dart 565:26           FlutterPlatform._startTest
===== asynchronous gap ===========================
dart:async/future.dart 518:21                                         Future.wait.<fn>
===== asynchronous gap ===========================
package:flutter_tools/src/test/flutter_platform.dart 675:7            _FlutterPlatformStreamSinkWrapper.close.<fn>
Error: the Dart compiler exited unexpectedly.
package:flutter_tools/src/base/common.dart 10:3  throwToolExit
package:flutter_tools/src/compile.dart 896:9     DefaultResidentCompiler._compile.<fn>
dart:async/zone.dart 1407:47                     _rootRunUnary
dart:async/zone.dart 1308:19                     _CustomZone.runUnary
dart:async/future_impl.dart 838:45               Future._propagateToListeners.handleValueCallback
dart:async/future_impl.dart 867:13               Future._propagateToListeners
dart:async/future_impl.dart 643:5                Future._completeWithValue
dart:async/future_impl.dart 713:7                Future._asyncCompleteWithValue.<fn>
dart:async/zone.dart 1399:13                     _rootRun
dart:async/zone.dart 1301:19                     _CustomZone.run
dart:async/zone.dart 1209:7                      _CustomZone.runGuarded
dart:async/zone.dart 1249:23                     _CustomZone.bindCallbackGuarded.<fn>
dart:async/schedule_microtask.dart 40:21         _microtaskLoop
dart:async/schedule_microtask.dart 49:5          _startMicrotaskLoop
dart:isolate-patch/isolate_patch.dart 118:13     _runPendingImmediateCallback
dart:isolate-patch/isolate_patch.dart 185:5      _RawReceivePort._handleMessage

PathNotFoundException: Deletion failed, path = '/var/folders/l9/rsj1dsjd64b2hnrzxw53hvc80000gn/T/flutter_tools.VWvUbU/flutter_test_listener.Y3PBgI' (OS Error: No such file or directory, errno = 2)
```

### Code sample

<details open><summary>Code sample</summary>

```dart
Any integration test.
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

![Screenshot 2024-02-23 at 13 08 54](https://github.com/flutter/flutter/assets/3032294/bc800068-730c-4de7-93fc-24ee70335b4f)


</details>


### Logs

<details open><summary>Logs</summary>

```console
adb uninstall failed: ProcessException: Process exited abnormally with exit code -2:

  Command: /Users/glennruysschaert/Library/Android/sdk/platform-tools/adb -s emulator-5554 uninstall app.getdriven.customer.staging.debug
unhandled error during finalization of test:
/Users/glennruysschaert/Code/get-driven-customer-app/integration_test/profile_options_test.dart
PathNotFoundException: Deletion failed, path = '/var/folders/l9/rsj1dsjd64b2hnrzxw53hvc80000gn/T/flutter_tools.VWvUbU/flutter_test_listener.Y3PBgI' (OS Error: No such file or directory, errno = 2)
#0      _Directory._deleteSync (dart:io/directory_impl.dart:188:7)
#1      FileSystemEntity.deleteSync (dart:io/file_system_entity.dart:407:7)
#2      ForwardingFileSystemEntity.deleteSync (package:file/src/forwarding/forwarding_file_system_entity.dart:70:16)
#3      FlutterPlatform._createListenerDart.<anonymous closure> (package:flutter_tools/src/test/flutter_platform.dart:601:15)
#4      FlutterPlatform._startTest (package:flutter_tools/src/test/flutter_platform.dart:565:26)
<asynchronous suspension>
#5      Future.wait.<anonymous closure> (dart:async/future.dart:518:21)
<asynchronous suspension>
#6      _FlutterPlatformStreamSinkWrapper.close.<anonymous closure> (package:flutter_tools/src/test/flutter_platform.dart:675:7)
<asynchronous suspension>

dart:io/directory_impl.dart 188:7                                     _Directory._deleteSync
dart:io/file_system_entity.dart 407:7                                 FileSystemEntity.deleteSync
package:file/src/forwarding/forwarding_file_system_entity.dart 70:16  ForwardingFileSystemEntity.deleteSync
package:flutter_tools/src/test/flutter_platform.dart 601:15           FlutterPlatform._createListenerDart.<fn>
package:flutter_tools/src/test/flutter_platform.dart 565:26           FlutterPlatform._startTest
===== asynchronous gap ===========================
dart:async/future.dart 518:21                                         Future.wait.<fn>
===== asynchronous gap ===========================
package:flutter_tools/src/test/flutter_platform.dart 675:7            _FlutterPlatformStreamSinkWrapper.close.<fn>
Error: the Dart compiler exited unexpectedly.
package:flutter_tools/src/base/common.dart 10:3  throwToolExit
package:flutter_tools/src/compile.dart 896:9     DefaultResidentCompiler._compile.<fn>
dart:async/zone.dart 1407:47                     _rootRunUnary
dart:async/zone.dart 1308:19                     _CustomZone.runUnary
dart:async/future_impl.dart 838:45               Future._propagateToListeners.handleValueCallback
dart:async/future_impl.dart 867:13               Future._propagateToListeners
dart:async/future_impl.dart 643:5                Future._completeWithValue
dart:async/future_impl.dart 713:7                Future._asyncCompleteWithValue.<fn>
dart:async/zone.dart 1399:13                     _rootRun
dart:async/zone.dart 1301:19                     _CustomZone.run
dart:async/zone.dart 1209:7                      _CustomZone.runGuarded
dart:async/zone.dart 1249:23                     _CustomZone.bindCallbackGuarded.<fn>
dart:async/schedule_microtask.dart 40:21         _microtaskLoop
dart:async/schedule_microtask.dart 49:5          _startMicrotaskLoop
dart:isolate-patch/isolate_patch.dart 118:13     _runPendingImmediateCallback
dart:isolate-patch/isolate_patch.dart 185:5      _RawReceivePort._handleMessage

PathNotFoundException: Deletion failed, path = '/var/folders/l9/rsj1dsjd64b2hnrzxw53hvc80000gn/T/flutter_tools.VWvUbU/flutter_test_listener.Y3PBgI' (OS Error: No such file or directory, errno = 2)
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.19.1, on macOS 14.2.1 23C71 darwin-arm64
    (Rosetta), locale en-GB)
    • Flutter version 3.19.1 on channel stable at
      /Users/glennruysschaert/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision abb292a07e (3 days ago), 2024-02-20 14:35:05 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version
    32.1.0-rc1)
    • Android SDK at /Users/glennruysschaert/Library/Android/sdk
    • Platform android-34, build-tools 32.1.0-rc1
    • ANDROID_HOME = /Users/glennruysschaert/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • 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
      17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.85.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554             • android-arm64  •
      Android 14 (API 34) (emulator)
    • Glenn’s iPhone (mobile)     • 00008130-0005148E1061401C • ios            •
      iOS 17.3.1 21D61
    • macOS (desktop)             • macos                     • darwin-arm64   •
      macOS 14.2.1 23C71 darwin-arm64 (Rosetta)
    • Chrome (web)                • chrome                    • web-javascript •
      Google Chrome 121.0.6167.184
    ! Error: Browsing on the local area network for Glenn’s iPad. Ensure the
      device is unlocked and attached with a cable or associated with the same
      local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code
      -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>