[dds/dap] Attempt to fix flaky tests on Linux during shutdown while debugger is initializing

Fixes https://github.com/dart-lang/sdk/issues/50058.

Change-Id: Ia0b498a4f1c943bc1f21f940c533584837ba5311
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/261401
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
diff --git a/pkg/dds/lib/src/dap/adapters/dart.dart b/pkg/dds/lib/src/dap/adapters/dart.dart
index 61722db..537b556 100644
--- a/pkg/dds/lib/src/dap/adapters/dart.dart
+++ b/pkg/dds/lib/src/dap/adapters/dart.dart
@@ -7,6 +7,7 @@
 import 'dart:io';
 
 import 'package:collection/collection.dart';
+import 'package:json_rpc_2/error_code.dart' as jsonRpcErrors;
 import 'package:meta/meta.dart';
 import 'package:path/path.dart' as path;
 import 'package:vm_service/vm_service.dart' as vm;
@@ -2099,13 +2100,22 @@
     try {
       return await func();
     } on vm.RPCError catch (e) {
-      // If we're been asked to shut down while this request was occurring,
-      // it's normal to get kServiceDisappeared so we should handle this
-      // silently.
-      if (isTerminating && e.code == RpcErrorCodes.kServiceDisappeared) {
-        return null;
+      // If we've been asked to shut down while this request was occurring,
+      // it's normal to get some types of errors from in-flight VM Service
+      // requests and we should handle them silently.
+      if (isTerminating) {
+        // kServiceDisappeared is thrown sometimes when services disappear.
+        if (e.code == RpcErrorCodes.kServiceDisappeared) {
+          return null;
+        }
+        // SERVER_ERROR can occur when DDS completes any outstanding requests
+        // with "The client closed with pending request".
+        if (e.code == jsonRpcErrors.SERVER_ERROR) {
+          return null;
+        }
       }
 
+      // Otherwise, it's an unexpected/unknown failure and should be rethrown.
       rethrow;
     }
   }
diff --git a/pkg/dds/lib/src/dap/isolate_manager.dart b/pkg/dds/lib/src/dap/isolate_manager.dart
index c1c586d..32502f9 100644
--- a/pkg/dds/lib/src/dap/isolate_manager.dart
+++ b/pkg/dds/lib/src/dap/isolate_manager.dart
@@ -922,7 +922,11 @@
         // We can't leave dangling completers here because others may already
         // be waiting on them, so propagate the error to them.
         completers.forEach((uri, completer) => completer.completeError(e));
-        rethrow;
+
+        // Don't rethrow here, because it will cause these completers futures
+        // to not have error handlers attached which can cause their errors to
+        // go unhandled. Instead, these completers futures will be returned
+        // below and awaited by the caller (which will propogate the errors).
       }
     }