Skip to content

Commit

Permalink
[wasm][test-browser] Retry launching chrome if it fails on startup
Browse files Browse the repository at this point in the history
When starting chrome via selenium+chromedriver, it seems to crash
inexplicably (dotnet/runtime#43955).
As a workaround, let's retry that a few times.
  • Loading branch information
radical committed Oct 29, 2020
1 parent cfc0a52 commit 62bdf15
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
logProcessor,
logger);

var (driverService, driver) = GetChromeDriver();
var (driverService, driver) = GetChromeDriver(logger);
try
{
return await runner.RunTestsWithWebDriver(driverService, driver);
Expand All @@ -62,7 +62,7 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
}
}

private (DriverService, IWebDriver) GetChromeDriver()
private (DriverService, IWebDriver) GetChromeDriver(ILogger logger)
{
var options = new ChromeOptions();
options.SetLoggingPreference(LogType.Browser, SeleniumLogLevel.All);
Expand All @@ -73,7 +73,8 @@ protected override async Task<ExitCode> InvokeInternal(ILogger logger)
options.AddArguments(new List<string>(_arguments.BrowserArgs)
{
"--incognito",
"--headless"
"--headless",
"--no-sandbox"
});

var driverService = ChromeDriverService.CreateDefaultService();
Expand All @@ -87,7 +88,29 @@ protected override async Task<ExitCode> 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++;
}
}
}
}

0 comments on commit 62bdf15

Please sign in to comment.