[vm/isolates] Ensure removeErrorListener makes exceptions unhandled.

BUG=https://github.com/dart-lang/sdk/issues/53850
TEST=lib/isolate/remove_error_listener_test

Change-Id: I442ea403671cd42275a2a420c2cb234c2c5fa126
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335321
Commit-Queue: Alexander Aprelev <aam@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
diff --git a/runtime/vm/isolate.cc b/runtime/vm/isolate.cc
index a9239eb..97cd1b4 100644
--- a/runtime/vm/isolate.cc
+++ b/runtime/vm/isolate.cc
@@ -2305,14 +2305,16 @@
   arr_values[1] = &stack;
 
   SendPort& listener = SendPort::Handle(current_zone());
+  bool was_somebody_notified = false;
   for (intptr_t i = 0; i < listeners.Length(); i++) {
     listener ^= listeners.At(i);
     if (!listener.IsNull()) {
       Dart_Port port_id = listener.Id();
       PortMap::PostMessage(SerializeMessage(current_zone(), port_id, &arr));
+      was_somebody_notified = true;
     }
   }
-  return listeners.Length() > 0;
+  return was_somebody_notified;
 }
 
 static void ShutdownIsolate(uword parameter) {
diff --git a/tests/lib/isolate/remove_error_listener_test.dart b/tests/lib/isolate/remove_error_listener_test.dart
new file mode 100644
index 0000000..3d16fa6
--- /dev/null
+++ b/tests/lib/isolate/remove_error_listener_test.dart
@@ -0,0 +1,30 @@
+// Copyright (c) 2023, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+//
+// Test that removing error listener makes exception throwing unhandled.
+
+import 'dart:io';
+import 'dart:isolate';
+
+import "package:expect/expect.dart";
+
+void main(List<String> args) {
+  if (args.length == 0) {
+    final result = Process.runSync(Platform.executable, [
+      ...Platform.executableArguments,
+      Platform.script.toFilePath(),
+      'child'
+    ]);
+    Expect.isTrue(
+        result.stderr.contains("Unhandled exception:\nException: Oops!"));
+    return;
+  }
+  {
+    final port = ReceivePort();
+    Isolate.current.addErrorListener(port.sendPort);
+    Isolate.current.removeErrorListener(port.sendPort);
+    port.close();
+    throw Exception('Oops!');
+  }
+}
diff --git a/tests/lib_2/isolate/remove_error_listener_test.dart b/tests/lib_2/isolate/remove_error_listener_test.dart
new file mode 100644
index 0000000..ceba7ae
--- /dev/null
+++ b/tests/lib_2/isolate/remove_error_listener_test.dart
@@ -0,0 +1,32 @@
+// Copyright (c) 2023, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+//
+// @dart = 2.9
+//
+// Test that removing error listener makes exception throwing unhandled.
+
+import 'dart:io';
+import 'dart:isolate';
+
+import "package:expect/expect.dart";
+
+void main(List<String> args) {
+  if (args.length == 0) {
+    final result = Process.runSync(Platform.executable, [
+      ...Platform.executableArguments,
+      Platform.script.toFilePath(),
+      'child'
+    ]);
+    Expect.isTrue(
+        result.stderr.contains("Unhandled exception:\nException: Oops!"));
+    return;
+  }
+  {
+    final port = ReceivePort();
+    Isolate.current.addErrorListener(port.sendPort);
+    Isolate.current.removeErrorListener(port.sendPort);
+    port.close();
+    throw Exception('Oops!');
+  }
+}