Skip to content

Commit d74edbb

Browse files
committed
[flutter_tools] Improve ExtensionConnection unhandled error logging and isolate exit handling
- Log unhandled extension isolate errors using logger.printError instead of trace so failure reasons are visible at default verbosity. - Close receive port, error port, exit port, and notification controllers on unexpected isolate exit. - Validate onNotification payload type and warn on malformed payloads rather than falling back to an empty map. - Strengthen unit test assertions on StateError messages and verify notification streams emit done on unexpected isolate exit. Part of flutter#190755 Part of flutter#190746 Fixes flutter#193955
1 parent ba60b91 commit d74edbb

2 files changed

Lines changed: 59 additions & 33 deletions

File tree

‎packages/flutter_tools/lib/src/experimental/extension_discovery.dart‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,16 @@ class ExtensionConnection {
2727
required this._receivePort,
2828
}) {
2929
_errorPort.handler = (Object? error) {
30-
if (_isDisposed) {
31-
return;
32-
}
3330
final Object? message = error is List && error.isNotEmpty ? error[0] : error;
34-
_logger.printTrace('Extension isolate "$extensionName" unhandled error: $message');
35-
_receivePort.close();
31+
_logger.printError('Extension isolate "$extensionName" unhandled error: $message');
3632
};
3733

3834
_exitPort.handler = (Object? _) {
39-
if (_isDisposed) {
40-
return;
41-
}
4235
_logger.printTrace('Extension isolate "$extensionName" exited unexpectedly.');
36+
_errorPort.close();
37+
_exitPort.close();
4338
_receivePort.close();
39+
unawaited(_closeNotificationControllers());
4440
};
4541
}
4642

@@ -69,23 +65,24 @@ class ExtensionConnection {
6965
if (_isDisposed) {
7066
throw StateError('ExtensionConnection has been disposed.');
7167
}
72-
final StreamController<Map<String, Object?>> controller = _notificationControllers.putIfAbsent(
73-
method,
74-
() {
75-
final newController = StreamController<Map<String, Object?>>.broadcast();
76-
_peer.registerMethod(method, (json_rpc.Parameters params) {
77-
if (newController.isClosed) {
78-
return;
79-
}
80-
final Object? rawValue = params.value;
81-
final Map<String, Object?> payload = rawValue is Map<String, Object?>
82-
? rawValue
83-
: (rawValue is Map ? rawValue.cast<String, Object?>() : const <String, Object?>{});
84-
newController.add(payload);
85-
});
86-
return newController;
87-
},
88-
);
68+
final StreamController<Map<String, Object?>>
69+
controller = _notificationControllers.putIfAbsent(method, () {
70+
final newController = StreamController<Map<String, Object?>>.broadcast();
71+
_peer.registerMethod(method, (json_rpc.Parameters params) {
72+
if (newController.isClosed) {
73+
return;
74+
}
75+
final Object? rawValue = params.value;
76+
if (rawValue is! Map<String, Object?>) {
77+
_logger.printWarning(
78+
'Extension "$extensionName" sent malformed notification for "$method": expected Map<String, Object?>.',
79+
);
80+
return;
81+
}
82+
newController.add(rawValue);
83+
});
84+
return newController;
85+
});
8986
return controller.stream;
9087
}
9188

@@ -197,6 +194,14 @@ class ExtensionConnection {
197194
}
198195
}
199196

197+
Future<void> _closeNotificationControllers() async {
198+
for (final StreamController<Map<String, Object?>> controller
199+
in _notificationControllers.values) {
200+
await controller.close();
201+
}
202+
_notificationControllers.clear();
203+
}
204+
200205
/// Disposes the extension isolate connection.
201206
Future<void> dispose() async {
202207
if (_isDisposed) {
@@ -207,11 +212,7 @@ class ExtensionConnection {
207212
_errorPort.close();
208213
_exitPort.close();
209214
_receivePort.close();
210-
for (final StreamController<Map<String, Object?>> controller
211-
in _notificationControllers.values) {
212-
await controller.close();
213-
}
214-
_notificationControllers.clear();
215+
await _closeNotificationControllers();
215216
try {
216217
await _peer.close();
217218
} on Object catch (error) {

‎packages/flutter_tools/test/general.shard/extension_protocol/extension_discovery_test.dart‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,13 @@ void main() {
277277
final service = _DummyService('test', 'ping');
278278
expect(
279279
() => service.sendNotification('test.event', const <String, Object?>{}),
280-
throwsStateError,
280+
throwsA(
281+
isA<StateError>().having(
282+
(StateError e) => e.message,
283+
'message',
284+
contains('is not attached to an active peer'),
285+
),
286+
),
281287
);
282288
});
283289

@@ -368,10 +374,22 @@ void main() {
368374
);
369375
addTearDown(connection.dispose);
370376

377+
final Stream<Map<String, Object?>> notificationStream = connection.onNotification(
378+
'notify.customEvent',
379+
);
380+
final Future<void> streamDone = expectLater(notificationStream, emitsDone);
381+
371382
await expectLater(
372383
connection.sendRequest('exit.triggerExit', timeout: null),
373-
throwsA(isA<StateError>()),
384+
throwsA(
385+
isA<StateError>().having(
386+
(StateError e) => e.message,
387+
'message',
388+
contains('The client closed with pending request "exit.triggerExit"'),
389+
),
390+
),
374391
);
392+
await streamDone;
375393
},
376394
);
377395

@@ -385,8 +403,15 @@ void main() {
385403

386404
await expectLater(
387405
connection.sendRequest('error.triggerUnhandledError', timeout: null),
388-
throwsA(isA<StateError>()),
406+
throwsA(
407+
isA<StateError>().having(
408+
(StateError e) => e.message,
409+
'message',
410+
contains('The client closed with pending request "error.triggerUnhandledError"'),
411+
),
412+
),
389413
);
414+
expect(logger.errorText, contains('Simulated unhandled async error'));
390415
});
391416
});
392417
}

0 commit comments

Comments
 (0)