Repository: flutter
Issue: Flutter drive tests suddenly stop working
Labels: c: crash,tool,t: flutter driver,P2,team-tool,triaged-tool

## Steps to Reproduce

1. We have a suite of ~30 UI tests that are executed sequentially. In order to restart the application between each scenario, we are doing some workaround (already opened this feature request https://github.com/flutter/flutter/issues/48955 to restart app easily). Execute the tests on that suite ¡
`flutter drive -d "my_awesome_emulator" --target=test_driver/test_runner.dart`
<!-- You must include full steps to reproduce so that we can reproduce the problem. -->

**Expected results:** 
UI tests execution is working as expected. 

**Actual results:** 
We are having a random error that happens really often (~80% of builds) and only happens on Android that have this stack trace:
```
11:02:27  [warning] FlutterDriver: Instance of '_WebSocketImpl' is closed with an unexpected code 1005
11:02:27  [warning] FlutterDriver: Instance of '_WebSocketImpl' is closed with an unexpected code 1005
11:02:31  [warning] FlutterDriver: tap message is taking a long time to complete...
11:02:55  01:11 +10 -1: my_test_group my_test_name [E]
11:02:55  
11:02:55    TimeoutException after 0:00:30.000000: Test timed out after 30 seconds.
11:02:55  
11:02:55    package:test_api/src/backend/invoker.dart 324:28              Invoker._handleError.<fn>
11:02:55    dart:async/zone.dart 1122:38                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    package:test_api/src/backend/invoker.dart 322:10              Invoker._handleError
11:02:55    package:test_api/src/backend/invoker.dart 275:9               Invoker.heartbeat.<fn>.<fn>
11:02:55    dart:async/zone.dart 1126:13                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    package:test_api/src/backend/invoker.dart 273:38              Invoker.heartbeat.<fn>
11:02:55    package:stack_trace/src/stack_zone_specification.dart 209:15  StackZoneSpecification._run
11:02:55    package:stack_trace/src/stack_zone_specification.dart 119:48  StackZoneSpecification._registerCallback.<fn>
11:02:55    dart:async/zone.dart 1126:13                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 949:23                                   _CustomZone.bindCallback.<fn>
11:02:55    dart:async-patch/timer_patch.dart 23:15                       Timer._createTimer.<fn>
11:02:55    dart:isolate-patch/timer_impl.dart 384:19                     _Timer._runTimers
11:02:55    dart:isolate-patch/timer_impl.dart 418:5                      _Timer._handleMessage
11:02:55    dart:isolate-patch/isolate_patch.dart 174:12                  _RawReceivePortImpl._handleMessage
11:02:55    ===== asynchronous gap ===========================
11:02:55    dart:async/zone.dart 1047:19                                  _CustomZone.registerCallback
11:02:55    dart:async/zone.dart 948:22                                   _CustomZone.bindCallback
11:02:55    dart:async/zone.dart 1193:21                                  _rootCreateTimer
11:02:55    dart:async/zone.dart 1090:19                                  _CustomZone.createTimer
11:02:55    package:test_api/src/backend/invoker.dart 272:34              Invoker.heartbeat
11:02:55    package:test_api/src/backend/invoker.dart 235:5               Invoker.waitForOutstandingCallbacks
11:02:55    package:test_api/src/backend/declarer.dart 169:33             Declarer.test.<fn>.<fn>
11:02:55    dart:async/zone.dart 1126:13                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 1518:10                                  _runZoned
11:02:55    dart:async/zone.dart 1465:12                                  runZoned
11:02:55    package:test_api/src/backend/declarer.dart 168:13             Declarer.test.<fn>
11:02:55    package:test_api/src/backend/invoker.dart 392:25              Invoker._onRun.<fn>.<fn>.<fn>.<fn>
11:02:55    dart:async/future.dart 176:37                                 new Future.<fn>
11:02:55    package:stack_trace/src/stack_zone_specification.dart 209:15  StackZoneSpecification._run
11:02:55    package:stack_trace/src/stack_zone_specification.dart 119:48  StackZoneSpecification._registerCallback.<fn>
11:02:55    dart:async/zone.dart 1122:38                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 925:7                                    _CustomZone.runGuarded
11:02:55    dart:async/zone.dart 965:23                                   _CustomZone.bindCallbackGuarded.<fn>
11:02:55    package:stack_trace/src/stack_zone_specification.dart 209:15  StackZoneSpecification._run
11:02:55    package:stack_trace/src/stack_zone_specification.dart 119:48  StackZoneSpecification._registerCallback.<fn>
11:02:55    dart:async/zone.dart 1126:13                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 949:23                                   _CustomZone.bindCallback.<fn>
11:02:55    dart:async-patch/timer_patch.dart 23:15                       Timer._createTimer.<fn>
11:02:55    dart:isolate-patch/timer_impl.dart 384:19                     _Timer._runTimers
11:02:55    dart:isolate-patch/timer_impl.dart 418:5                      _Timer._handleMessage
11:02:55    dart:isolate-patch/isolate_patch.dart 174:12                  _RawReceivePortImpl._handleMessage
11:02:55    ===== asynchronous gap ===========================
11:02:55    dart:async/zone.dart 1047:19                                  _CustomZone.registerCallback
11:02:55    dart:async/zone.dart 964:22                                   _CustomZone.bindCallbackGuarded
11:02:55    dart:async/timer.dart 54:45                                   new Timer
11:02:55    dart:async/timer.dart 91:9                                    Timer.run
11:02:55    dart:async/future.dart 174:11                                 new Future
11:02:55    package:test_api/src/backend/invoker.dart 391:21              Invoker._onRun.<fn>.<fn>.<fn>
11:02:55    dart:async/zone.dart 1126:13                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 1518:10                                  _runZoned
11:02:55    dart:async/zone.dart 1465:12                                  runZoned
11:02:55    package:test_api/src/backend/invoker.dart 378:9               Invoker._onRun.<fn>.<fn>
11:02:55    package:test_api/src/backend/invoker.dart 430:15              Invoker._guardIfGuarded
11:02:55    package:test_api/src/backend/invoker.dart 377:7               Invoker._onRun.<fn>
11:02:55    package:stack_trace/src/chain.dart 101:24                     Chain.capture.<fn>
11:02:55    dart:async/zone.dart 1126:13                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 1518:10                                  _runZoned
11:02:55    dart:async/zone.dart 1465:12                                  runZoned
11:02:55    package:stack_trace/src/chain.dart 99:12                      Chain.capture
11:02:55    package:test_api/src/backend/invoker.dart 376:11              Invoker._onRun
11:02:55    package:test_api/src/backend/live_test_controller.dart 185:5  LiveTestController._run
11:02:55    package:test_api/src/backend/live_test_controller.dart 40:37  _LiveTest.run
11:02:55    dart:async/future.dart 202:37                                 new Future.microtask.<fn>
11:02:55    dart:async/zone.dart 1122:38                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 925:7                                    _CustomZone.runGuarded
11:02:55    dart:async/zone.dart 965:23                                   _CustomZone.bindCallbackGuarded.<fn>
11:02:55    dart:async/zone.dart 1126:13                                  _rootRun
11:02:55    dart:async/zone.dart 1023:19                                  _CustomZone.run
11:02:55    dart:async/zone.dart 925:7                                    _CustomZone.runGuarded
11:02:55    dart:async/zone.dart 965:23                                   _CustomZone.bindCallbackGuarded.<fn>
11:02:55    dart:async/schedule_microtask.dart 43:21                      _microtaskLoop
11:02:55    dart:async/schedule_microtask.dart 52:5                       _startMicrotaskLoop
11:02:55    dart:isolate-patch/timer_impl.dart 393:30                     _Timer._runTimers
11:02:55    dart:isolate-patch/timer_impl.dart 418:5                      _Timer._handleMessage
11:02:55    dart:isolate-patch/isolate_patch.dart 174:12                  _RawReceivePortImpl._handleMessage
```

**Extra information:**
We're using a couple of emulators and it's reproducible in both of them:
- system-images;android-29;google_apis;x86_64
- system-images;android-28;google_apis;x86_64

<details>
  <summary>Logs</summary>

```

Analyzing flutter-driver...                                             
No issues found! (ran in 3.5s)
```


```
 [✓] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.14.6 18G1012, locale en)
    • Flutter version 1.12.13+hotfix.5 at /Users/victorvargas/Desktop/flutter
    • Framework revision 27321ebbad (5 weeks ago), 2019-12-10 18:15:01 -0800
    • Engine revision 2994f7e1e6
    • Dart version 2.7.0

 
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
    • Android SDK at /Users/victorvargas/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.1
    • ANDROID_HOME = /Users/victorvargas/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

[✓] Xcode - develop for iOS and macOS (Xcode 11.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.1, Build version 11A1027
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.4)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 39.0.1
    • Dart plugin version 183.6270
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[!] IntelliJ IDEA Community Edition (version 2019.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

[✓] Connected device (1 available)
    • Android SDK built for x86 64 • emulator-5556 • android-x64 • Android 10 (API 29) (emulator) 

```

</details>