From 25e6cc6743d35c50d6264dbd83b4978417382a3b Mon Sep 17 00:00:00 2001 From: Yegor Date: Mon, 12 Dec 2016 10:58:21 -0800 Subject: [PATCH] tools: add timeout to VM service client (#7175) --- packages/flutter_tools/lib/src/vmservice.dart | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/flutter_tools/lib/src/vmservice.dart b/packages/flutter_tools/lib/src/vmservice.dart index 2c8a940280..b44405f851 100644 --- a/packages/flutter_tools/lib/src/vmservice.dart +++ b/packages/flutter_tools/lib/src/vmservice.dart @@ -14,9 +14,15 @@ import 'package:web_socket_channel/io.dart'; import 'globals.dart'; +/// The default VM service request timeout. +const Duration kDefaultRequestTimeout = const Duration(seconds: 10); + +/// Used for RPC requests that may take a long time. +const Duration kLongRequestTimeout = const Duration(minutes: 1); + /// A connection to the Dart VM Service. class VMService { - VMService._(this.peer, this.httpAddress, this.wsAddress) { + VMService._(this.peer, this.httpAddress, this.wsAddress, this._requestTimeout) { _vm = new VM._empty(this); peer.registerMethod('streamNotify', (rpc.Parameters event) { @@ -24,8 +30,11 @@ class VMService { }); } - /// Connect to '127.0.0.1' at [port]. - static Future connect(Uri httpUri) async { + /// Connect to a Dart VM Service at [httpUri]. + /// + /// Requests made via the returns [VMService] time out after [requestTimeout] + /// amount of time, which is [kDefaultRequestTimeout] by default. + static Future connect(Uri httpUri, {Duration requestTimeout: kDefaultRequestTimeout}) async { Uri wsUri = httpUri.replace(scheme: 'ws', path: path.join(httpUri.path, 'ws')); WebSocket ws; try { @@ -35,11 +44,13 @@ class VMService { } rpc.Peer peer = new rpc.Peer.withoutJson(jsonDocument.bind(new IOWebSocketChannel(ws).cast())); peer.listen(); - return new VMService._(peer, httpUri, wsUri); + return new VMService._(peer, httpUri, wsUri, requestTimeout); } + final Uri httpAddress; final Uri wsAddress; final rpc.Peer peer; + final Duration _requestTimeout; VM _vm; /// The singleton [VM] object. Owns [Isolate] and [FlutterView] objects. @@ -558,13 +569,20 @@ class VM extends ServiceObjectOwner { /// Invoke the RPC and return the raw response. Future> invokeRpcRaw( - String method, [Map params]) async { + String method, [Map params, Duration timeout]) async { if (params == null) { params = {}; } - Map result = - await _vmService.peer.sendRequest(method, params); - return result; + + try { + Map result = await _vmService.peer + .sendRequest(method, params) + .timeout(timeout ?? _vmService._requestTimeout); + return result; + } on TimeoutException catch(_) { + printError('Request to Dart VM Service timed out: $method($params)'); + rethrow; + } } /// Invoke the RPC and return a ServiceObject response. @@ -661,7 +679,7 @@ class VM extends ServiceObjectOwner { } Future> getVMTimeline() { - return invokeRpcRaw('_getVMTimeline', {}); + return invokeRpcRaw('_getVMTimeline', {}, kLongRequestTimeout); } Future refreshViews() async { @@ -724,7 +742,7 @@ class Isolate extends ServiceObjectOwner { /// Invoke the RPC and return the raw response. Future> invokeRpcRaw( - String method, [Map params]) { + String method, [Map params, Duration timeout]) { // Inject the 'isolateId' parameter. if (params == null) { params = { @@ -733,7 +751,7 @@ class Isolate extends ServiceObjectOwner { } else { params['isolateId'] = id; } - return vm.invokeRpcRaw(method, params); + return vm.invokeRpcRaw(method, params, timeout); } /// Invoke the RPC and return a ServiceObject response. @@ -793,9 +811,9 @@ class Isolate extends ServiceObjectOwner { String method, [Map params]) async { try { return await invokeRpcRaw(method, params); - } catch (e) { + } on rpc.RpcException catch (e) { // If an application is not using the framework - if (_isMethodNotFoundException(e)) + if (e.code == rpc_error_code.METHOD_NOT_FOUND) return null; rethrow; } @@ -818,11 +836,6 @@ class Isolate extends ServiceObjectOwner { return state; } - static bool _isMethodNotFoundException(dynamic e) { - return (e is rpc.RpcException) && - (e.code == rpc_error_code.METHOD_NOT_FOUND); - } - // Reload related extension methods. Future> flutterReassemble() async { return await invokeFlutterExtensionRpcRaw('ext.flutter.reassemble');