Skip to content

Commit 98ddead

Browse files
bkonyiCommit Queue
authored and
Commit Queue
committed
[ package:vm_service ] Don't try to send service extension response after service connection disposal
Also prepares for 14.3.1 release. Fixes flutter/flutter#157296 Change-Id: I623102a54ad7ca4481dd5957a7da65dbc5a2e9b3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391500 Commit-Queue: Ben Konyi <[email protected]> Auto-Submit: Ben Konyi <[email protected]> Reviewed-by: Derek Xu <[email protected]>
1 parent 1d1cbf2 commit 98ddead

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

pkg/vm_service/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 14.3.1
2+
- Fix crash that could occur when trying to send a service extension response
3+
after the service connection had already been disposed of ([flutter/flutter #157296]).
4+
5+
[flutter/flutter #157296]: https://github.com/flutter/flutter/issues/157296
6+
17
## 14.3.0
28
- Update to version `4.16` of the spec.
39
- Add `reloadFailureReason` property to `Event`.

pkg/vm_service/lib/src/vm_service.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,10 @@ class VmService {
20232023
Future _processRequest(Map<String, dynamic> json) async {
20242024
final result = await _routeRequest(
20252025
json['method'], json['params'] ?? <String, dynamic>{});
2026+
if (_disposed) {
2027+
// The service has disappeared. Don't try to send the response.
2028+
return;
2029+
}
20262030
result['id'] = json['id'];
20272031
result['jsonrpc'] = '2.0';
20282032
String message = jsonEncode(result);

pkg/vm_service/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: vm_service
2-
version: 14.3.0
2+
version: 14.3.1
33
description: >-
44
A library to communicate with a service implementing the Dart VM
55
service protocol.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Regression test for https://github.com/flutter/flutter/issues/157296
6+
7+
import 'dart:async';
8+
9+
import 'package:vm_service/vm_service.dart';
10+
import 'package:vm_service/vm_service_io.dart';
11+
12+
import 'common/service_test_common.dart';
13+
import 'common/test_helper.dart';
14+
15+
Future<void> testMain() async {}
16+
17+
final tests = <IsolateTest>[
18+
hasStoppedAtExit,
19+
(VmService service, IsolateRef isolateRef) async {
20+
final continueExtensionExecutionCompleter = Completer<void>();
21+
final extensionInvokedCompleter = Completer<void>();
22+
23+
// Setup the service extension.
24+
const String serviceName = 'testService';
25+
service.registerServiceCallback(
26+
serviceName,
27+
(Map<String, dynamic> params) async {
28+
extensionInvokedCompleter.complete();
29+
// Wait for the connection to go down before trying to send the
30+
// response.
31+
await continueExtensionExecutionCompleter.future;
32+
return <String, dynamic>{};
33+
},
34+
);
35+
await service.registerService(serviceName, '');
36+
37+
// Create a secondary service client to invoke the extension.
38+
final client = await vmServiceConnectUri(service.wsUri!);
39+
final serviceExtensionCompleter = Completer<String>();
40+
client.onServiceEvent.listen((e) {
41+
if (e.kind == EventKind.kServiceRegistered) {
42+
print('Service extension registered: ${e.method}');
43+
serviceExtensionCompleter.complete(e.method!);
44+
}
45+
});
46+
await client.streamListen(EventStreams.kService);
47+
48+
// Invoke the extension and wait for the extension to begin executing.
49+
final extensionName = await serviceExtensionCompleter.future;
50+
unawaited(
51+
client
52+
.callServiceExtension(extensionName)
53+
.catchError((_) => Response(), test: (o) => o is RPCError),
54+
);
55+
await extensionInvokedCompleter.future;
56+
57+
// Dispose the connection for the VmService instance with the service
58+
// extension registered to simulate the VM service connection disappearing
59+
// in the middle of handling a service extension request.
60+
await service.dispose();
61+
62+
// Resume the service extension handler. This will cause a
63+
// `Bad state: StreamSink is closed` error if there's a regression.
64+
continueExtensionExecutionCompleter.complete();
65+
66+
// Wait a bit to make sure the exception is thrown before the test
67+
// completes.
68+
await Future.delayed(const Duration(milliseconds: 200));
69+
},
70+
];
71+
72+
void main([args = const <String>[]]) => runIsolateTests(
73+
args,
74+
tests,
75+
'regress_157296_test.dart',
76+
testeeConcurrent: testMain,
77+
pauseOnExit: true,
78+
);

pkg/vm_service/tool/dart/generate_dart_client.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
215215
216216
Future _processRequest(Map<String, dynamic> json) async {
217217
final result = await _routeRequest(json['method'], json['params'] ?? <String, dynamic>{});
218+
if (_disposed) {
219+
// The service has disappeared. Don't try to send the response.
220+
return;
221+
}
218222
result['id'] = json['id'];
219223
result['jsonrpc'] = '2.0';
220224
String message = jsonEncode(result);

0 commit comments

Comments
 (0)