diff --git a/lib/web_ui/dev/chrome.dart b/lib/web_ui/dev/chrome.dart index 356b4dd349154..9ae072b85d168 100644 --- a/lib/web_ui/dev/chrome.dart +++ b/lib/web_ui/dev/chrome.dart @@ -4,6 +4,7 @@ // @dart = 2.6 import 'dart:async'; +import 'dart:convert'; import 'dart:io'; import 'package:pedantic/pedantic.dart'; @@ -76,7 +77,7 @@ class Chrome extends Browser { ]; final Process process = - await Process.start(installation.executable, args); + await _spawnChromiumProcess(installation.executable, args); remoteDebuggerCompleter.complete( getRemoteDebuggerUrl(Uri.parse('http://localhost:${kDevtoolsPort}'))); @@ -91,3 +92,72 @@ class Chrome extends Browser { Chrome._(Future startBrowser(), this.remoteDebuggerUrl) : super(startBrowser); } + +/// Used by [Chrome] to detect a glibc bug and retry launching the +/// browser. +/// +/// Once every few thousands of launches we hit this glibc bug: +/// +/// https://sourceware.org/bugzilla/show_bug.cgi?id=19329. +/// +/// When this happens Chrome spits out something like the following then exits with code 127: +/// +/// Inconsistency detected by ld.so: ../elf/dl-tls.c: 493: _dl_allocate_tls_init: Assertion `listp->slotinfo[cnt].gen <= GL(dl_tls_generation)' failed! +const String _kGlibcError = 'Inconsistency detected by ld.so'; + +Future _spawnChromiumProcess(String executable, List args, { String workingDirectory }) async { + // Keep attempting to launch the browser until one of: + // - Chrome launched successfully, in which case we just return from the loop. + // - The tool detected an unretriable Chrome error, in which case we throw ToolExit. + while (true) { + final Process process = await Process.start(executable, args, workingDirectory: workingDirectory); + + process.stdout + .transform(utf8.decoder) + .transform(const LineSplitter()) + .listen((String line) { + print('[CHROME STDOUT]: $line'); + }); + + // Wait until the DevTools are listening before trying to connect. This is + // only required for flutter_test --platform=chrome and not flutter run. + bool hitGlibcBug = false; + await process.stderr + .transform(utf8.decoder) + .transform(const LineSplitter()) + .map((String line) { + print('[CHROME STDERR]:$line'); + if (line.contains(_kGlibcError)) { + hitGlibcBug = true; + } + return line; + }) + .firstWhere((String line) => line.startsWith('DevTools listening'), orElse: () { + if (hitGlibcBug) { + print( + 'Encountered glibc bug https://sourceware.org/bugzilla/show_bug.cgi?id=19329. ' + 'Will try launching browser again.', + ); + return null; + } + print('Failed to launch browser. Command used to launch it: ${args.join(' ')}'); + throw Exception( + 'Failed to launch browser. Make sure you are using an up-to-date ' + 'Chrome or Edge. Otherwise, consider using -d web-server instead ' + 'and filing an issue at https://github.com/flutter/flutter/issues.', + ); + }); + + if (!hitGlibcBug) { + return process; + } + + // A precaution that avoids accumulating browser processes, in case the + // glibc bug doesn't cause the browser to quit and we keep looping and + // launching more processes. + process.exitCode.timeout(const Duration(seconds: 1), onTimeout: () { + process.kill(); + return null; + }); + } +}