From 62bdf154cf964767fbd8c5c167d82fc6087c206e Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Thu, 29 Oct 2020 15:53:40 -0400 Subject: [PATCH] [wasm][test-browser] Retry launching chrome if it fails on startup When starting chrome via selenium+chromedriver, it seems to crash inexplicably (https://github.com/dotnet/runtime/issues/43955). As a workaround, let's retry that a few times. --- .../WASM/Browser/WasmTestBrowserCommand.cs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/WASM/Browser/WasmTestBrowserCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/WASM/Browser/WasmTestBrowserCommand.cs index 40fa884e5..9cd291b3a 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/WASM/Browser/WasmTestBrowserCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/WASM/Browser/WasmTestBrowserCommand.cs @@ -50,7 +50,7 @@ protected override async Task InvokeInternal(ILogger logger) logProcessor, logger); - var (driverService, driver) = GetChromeDriver(); + var (driverService, driver) = GetChromeDriver(logger); try { return await runner.RunTestsWithWebDriver(driverService, driver); @@ -62,7 +62,7 @@ protected override async Task InvokeInternal(ILogger logger) } } - private (DriverService, IWebDriver) GetChromeDriver() + private (DriverService, IWebDriver) GetChromeDriver(ILogger logger) { var options = new ChromeOptions(); options.SetLoggingPreference(LogType.Browser, SeleniumLogLevel.All); @@ -73,7 +73,8 @@ protected override async Task InvokeInternal(ILogger logger) options.AddArguments(new List(_arguments.BrowserArgs) { "--incognito", - "--headless" + "--headless", + "--no-sandbox" }); var driverService = ChromeDriverService.CreateDefaultService(); @@ -87,7 +88,29 @@ protected override async Task InvokeInternal(ILogger logger) // getLog() might not see anything for long durations! // // So -> use a larger timeout! - return (driverService, new ChromeDriver(driverService, options, _arguments.Timeout)); + + int max_retries = 3; + int retry_num = 0; + while(true) + { + try + { + return (driverService, new ChromeDriver(driverService, options, _arguments.Timeout)); + } + catch (WebDriverException wde) when (wde.Message.Contains("exited abnormally") && retry_num < max_retries - 1) + { + // chrome can sometimes crash on startup when launching from chromedriver. + // As a *workaround*, let's retry that a few times + // Error seen: + // [12:41:07] crit: OpenQA.Selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally. + // (chrome not reachable) + + // Log on max-1 tries, and rethrow on the last one + logger.LogWarning($"Failed to start chrome, attempt #{retry_num}: {wde}"); + } + + retry_num++; + } } } }