From 9471e4e216d6df974d62e972ee995f756623bc44 Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Tue, 22 May 2018 11:55:46 -0700 Subject: [PATCH] Ensure that VM service extension handlers are run on outer event loop. (#17812) Fixes https://github.com/flutter/flutter/issues/17597 --- packages/flutter/lib/src/foundation/binding.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/flutter/lib/src/foundation/binding.dart b/packages/flutter/lib/src/foundation/binding.dart index 5b7efffaf1..f5c3345977 100644 --- a/packages/flutter/lib/src/foundation/binding.dart +++ b/packages/flutter/lib/src/foundation/binding.dart @@ -363,6 +363,19 @@ abstract class BindingBase { final String methodName = 'ext.flutter.$name'; developer.registerExtension(methodName, (String method, Map parameters) async { assert(method == methodName); + + // VM service extensions are handled as "out of band" messages by the VM, + // which means they are handled at various times, generally ASAP. + // Notably, this includes being handled in the middle of microtask loops. + // While this makes sense for some service extensions (e.g. "dump current + // stack trace", which explicitly doesn't want to wait for a loop to + // complete), Flutter extensions need not be handled with such high + // priority. Further, handling them with such high priority exposes us to + // the possibility that they're handled in the middle of a frame, which + // breaks many assertions. As such, we ensure they we run the callbacks + // on the outer event loop here. + await new Future.delayed(Duration.zero); + dynamic caughtException; StackTrace caughtStack; Map result;