Skip to content

Commit 7ac76bd

Browse files
Swallow exceptions related to trying to dispose an inspector group (#9256)
1 parent 852c2e4 commit 7ac76bd

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

packages/devtools_app/lib/src/service/vm_service_wrapper.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,24 @@ class TrackedFuture<T> {
510510
final String name;
511511
final Future<T> future;
512512
}
513+
514+
extension RpcErrorExtension on RPCError {
515+
/// Whether this [RPCError] is some kind of "VM Service connection has gone"
516+
/// error that may occur if the VM is shut down.
517+
bool get isServiceDisposedError {
518+
if (code == RPCErrorKind.kServiceDisappeared.code ||
519+
code == RPCErrorKind.kConnectionDisposed.code) {
520+
return true;
521+
}
522+
523+
if (code == RPCErrorKind.kServerError.code) {
524+
// Always ignore "client is closed" and "closed with pending request"
525+
// errors because these can always occur during shutdown if we were
526+
// just starting to send (or had just sent) a request.
527+
return message.contains('The client is closed') ||
528+
message.contains('The client closed with pending request') ||
529+
message.contains('Service connection dispose');
530+
}
531+
return false;
532+
}
533+
}

packages/devtools_app/lib/src/shared/diagnostics/inspector_service.dart

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'package:flutter/cupertino.dart';
2222
import 'package:flutter/foundation.dart';
2323
import 'package:vm_service/vm_service.dart';
2424

25+
import '../../service/vm_service_wrapper.dart';
2526
import '../console/primitives/simple_items.dart';
2627
import '../globals.dart';
2728
import '../utils/utils.dart';
@@ -501,13 +502,22 @@ abstract class InspectorObjectGroupBase
501502
/// attempt carefully cancel futures.
502503
@override
503504
Future<void> dispose() {
504-
// No need to dispose the group if the isolate is already gone.
505-
final disposeComplete = inspectorService.isolateRef != null
506-
? invokeVoidServiceMethod(
507-
WidgetInspectorServiceExtensions.disposeGroup.name,
508-
groupName,
509-
)
510-
: Future<void>.value();
505+
var disposeComplete = Future<void>.value();
506+
try {
507+
// Only dispose the group if the isolate still exists.
508+
if (inspectorService.isolateRef != null) {
509+
disposeComplete = invokeVoidServiceMethod(
510+
WidgetInspectorServiceExtensions.disposeGroup.name,
511+
groupName,
512+
);
513+
}
514+
} on RPCError catch (e) {
515+
if (!e.isServiceDisposedError) {
516+
// Swallow exceptions related to trying to dispose an Inspector group on
517+
// an already disposed service connection. Otherwise, rethrow.
518+
rethrow;
519+
}
520+
}
511521
disposed = true;
512522
return disposeComplete;
513523
}

0 commit comments

Comments
 (0)