Skip to content

Commit

Permalink
[ package:vm_service ] Don't try to send service extension response a…
Browse files Browse the repository at this point in the history
…fter 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]>
  • Loading branch information
bkonyi authored and Commit Queue committed Oct 22, 2024
1 parent 1d1cbf2 commit 98ddead
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/vm_service/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 14.3.1
- Fix crash that could occur when trying to send a service extension response
after the service connection had already been disposed of ([flutter/flutter #157296]).

[flutter/flutter #157296]: https://github.com/flutter/flutter/issues/157296

## 14.3.0
- Update to version `4.16` of the spec.
- Add `reloadFailureReason` property to `Event`.
Expand Down
4 changes: 4 additions & 0 deletions pkg/vm_service/lib/src/vm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,10 @@ class VmService {
Future _processRequest(Map<String, dynamic> json) async {
final result = await _routeRequest(
json['method'], json['params'] ?? <String, dynamic>{});
if (_disposed) {
// The service has disappeared. Don't try to send the response.
return;
}
result['id'] = json['id'];
result['jsonrpc'] = '2.0';
String message = jsonEncode(result);
Expand Down
2 changes: 1 addition & 1 deletion pkg/vm_service/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: vm_service
version: 14.3.0
version: 14.3.1
description: >-
A library to communicate with a service implementing the Dart VM
service protocol.
Expand Down
78 changes: 78 additions & 0 deletions pkg/vm_service/test/regress_157296_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2024, 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.

// Regression test for https://github.com/flutter/flutter/issues/157296

import 'dart:async';

import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';

import 'common/service_test_common.dart';
import 'common/test_helper.dart';

Future<void> testMain() async {}

final tests = <IsolateTest>[
hasStoppedAtExit,
(VmService service, IsolateRef isolateRef) async {
final continueExtensionExecutionCompleter = Completer<void>();
final extensionInvokedCompleter = Completer<void>();

// Setup the service extension.
const String serviceName = 'testService';
service.registerServiceCallback(
serviceName,
(Map<String, dynamic> params) async {
extensionInvokedCompleter.complete();
// Wait for the connection to go down before trying to send the
// response.
await continueExtensionExecutionCompleter.future;
return <String, dynamic>{};
},
);
await service.registerService(serviceName, '');

// Create a secondary service client to invoke the extension.
final client = await vmServiceConnectUri(service.wsUri!);
final serviceExtensionCompleter = Completer<String>();
client.onServiceEvent.listen((e) {
if (e.kind == EventKind.kServiceRegistered) {
print('Service extension registered: ${e.method}');
serviceExtensionCompleter.complete(e.method!);
}
});
await client.streamListen(EventStreams.kService);

// Invoke the extension and wait for the extension to begin executing.
final extensionName = await serviceExtensionCompleter.future;
unawaited(
client
.callServiceExtension(extensionName)
.catchError((_) => Response(), test: (o) => o is RPCError),
);
await extensionInvokedCompleter.future;

// Dispose the connection for the VmService instance with the service
// extension registered to simulate the VM service connection disappearing
// in the middle of handling a service extension request.
await service.dispose();

// Resume the service extension handler. This will cause a
// `Bad state: StreamSink is closed` error if there's a regression.
continueExtensionExecutionCompleter.complete();

// Wait a bit to make sure the exception is thrown before the test
// completes.
await Future.delayed(const Duration(milliseconds: 200));
},
];

void main([args = const <String>[]]) => runIsolateTests(
args,
tests,
'regress_157296_test.dart',
testeeConcurrent: testMain,
pauseOnExit: true,
);
4 changes: 4 additions & 0 deletions pkg/vm_service/tool/dart/generate_dart_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
Future _processRequest(Map<String, dynamic> json) async {
final result = await _routeRequest(json['method'], json['params'] ?? <String, dynamic>{});
if (_disposed) {
// The service has disappeared. Don't try to send the response.
return;
}
result['id'] = json['id'];
result['jsonrpc'] = '2.0';
String message = jsonEncode(result);
Expand Down

0 comments on commit 98ddead

Please sign in to comment.