fysh711426/UndetectedChromeDriver

System.InvalidOperationException: Cannot process request because the process (##) has exited, for .NET Framework 4.7.2

Opened this issue · 1 comments

I encounter this problem when using .net framework 4.7.2
If I use .net core 3.1, there is no issue.
Here is the code, very simple:

using OpenQA.Selenium.Chrome;
using SeleniumUndetectedChromeDriver;
using System;
using System.IO;
using System.Reflection;

namespace BareBonesTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var chromeOptions = new ChromeOptions();
            chromeOptions.AddArgument("--user-agent=" +
                "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.90 Mobile Safari/537.36");
            chromeOptions.AddArgument("--lang=en");
            chromeOptions.AddArgument("--incognito");

            using (var driver = UndetectedChromeDriver.Create(
                driverExecutablePath: Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "chromedriver.exe"),
                options: chromeOptions,
                headless: true,
                noSandbox: true))
            {
                driver.Navigate().GoToUrl("https://www.google.com/");
                Console.ReadLine();
            }
        }
    }
}

Once the program runs I get this error:

Unhandled Exception: System.InvalidOperationException: Cannot process request because the process (26412) has exited.
   at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.Kill()
   at SeleniumUndetectedChromeDriver.ChromeDriverInstaller.<GetDriverVersion>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at SeleniumUndetectedChromeDriver.UndetectedChromeDriver.Create(ChromeOptions options, String userDataDir, String driverExecutablePath, String browserExecutablePath, Int32 port, Int32 logLevel, Boolean headless, Boolean noSandbox, Boolean suppressWelcome, Boolean hideCommandPromptWindow, Nullable`1 commandTimeout, Dictionary`2 prefs, Action`1 configureService)
   at BareBonesTest.Program.Main(String[] args) in D:\...\BareBonesTest\Program.cs:line 19

I am using the chromedriver.exe version 120.0.6099.109 downloaded from here:
https://googlechromelabs.github.io/chrome-for-testing/

With these nugets:
https://i.imgur.com/wjrBrBC.png

I think it may be related to line 12 here:
https://github.com/fysh711426/UndetectedChromeDriver/blob/master/UndetectedChromeDriver/ProcessExtension.cs#L12

But the exception itself I think happens here on line 192:
https://github.com/fysh711426/UndetectedChromeDriver/blob/master/UndetectedChromeDriver/ChromeDriverInstaller.cs#L192

I think the process is killed before the Kill() is called.
A simple fix is to wrap the Kill with a try-catch and ignore InvalidOperationException exceptions that contain the text "has exited". Since if the process is dead already, then why kill it? That said if HasExited is a possible true value, looking at the code in ProcessExtension.cs, this outcome should already be expected. But even checking if HasExited is false first and killing it after, the process may exit after the check is done and before the Kill command, in between the nanoseconds there, so it is still best to try catch it IMO.

As a quick workaround, removing the using() fixed it.