C
C#2y ago
Hercules

❔ I need help making my code threadsafe that uses Selenium webdriver.

I am currently working on a service worker where i user selenium to take screenshots and the process for taking the screenshots is a multthreaded solutions where i use a Parallel.ForEach- loop with 4 or more threads on each round in the loops. in the loops i am createing a chrome driver and a ChromeDriverService for the driver. The service worker is installed as a windows service and it generates the following error after sometime after running perfectly fine.
2023-05-10 09:45:37.202 +00:00;;[INF];[T-];Unexpected Exception: OpenQA.Selenium.WebDriverException: An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://localhost:51961/session/ea521700fe9032a7c342c5da4ebfd806/window/rect. The exception message was: An error occurred while sending the request.
---> System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: The response ended prematurely.
2023-05-10 09:45:37.202 +00:00;;[INF];[T-];Unexpected Exception: OpenQA.Selenium.WebDriverException: An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://localhost:51961/session/ea521700fe9032a7c342c5da4ebfd806/window/rect. The exception message was: An error occurred while sending the request.
---> System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.IO.IOException: The response ended prematurely.
I need help stabilizing the code.
Task<ChromeDriver> PrepareDriverAsync(int portValue)
{
var options = new ChromeOptions();
options.AddArguments(new List<string> { "--headless", "--hide-scrollbars" });
options.PageLoadStrategy = PageLoadStrategy.Normal;
ChromeDriverService service = await CreateChromeDriverServiceAysnc();
service.Port = portValue;
var driver = new ChromeDriver(service, options);
return driver;
}

Task<ChromeDriverService> CreateChromeDriverServiceAysnc()
{
ChromeDriverService chromeDriverService = null;
try
{
chromeDriverService = ChromeDriverService.CreateDefaultService();
break;
}
catch (DriverServiceNotFoundException)
{

}
return chromeDriverService;
}
Task<ChromeDriver> PrepareDriverAsync(int portValue)
{
var options = new ChromeOptions();
options.AddArguments(new List<string> { "--headless", "--hide-scrollbars" });
options.PageLoadStrategy = PageLoadStrategy.Normal;
ChromeDriverService service = await CreateChromeDriverServiceAysnc();
service.Port = portValue;
var driver = new ChromeDriver(service, options);
return driver;
}

Task<ChromeDriverService> CreateChromeDriverServiceAysnc()
{
ChromeDriverService chromeDriverService = null;
try
{
chromeDriverService = ChromeDriverService.CreateDefaultService();
break;
}
catch (DriverServiceNotFoundException)
{

}
return chromeDriverService;
}
7 Replies
Denis
Denis2y ago
How have you determined that this is a thread safety issue?
Hercules
HerculesOP2y ago
@Deno This i a great question, the reason why i belive this is mostly due to th errors I have recieved.
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
--- End of inner exception stack trace ---
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Window.set_Size(Size value)
at Screenshot_WorkerService.Worker.NavigateToUrl(ChromeDriver driver, UrlConfiguration item, ValueTuple`2 configurations) in C:\Users\Alexa\source\repos\Elicit-WorkerService\Screenshot-WorkerService\Worker.cs:line 260
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
--- End of inner exception stack trace ---
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Window.set_Size(Size value)
at Screenshot_WorkerService.Worker.NavigateToUrl(ChromeDriver driver, UrlConfiguration item, ValueTuple`2 configurations) in C:\Users\Alexa\source\repos\Elicit-WorkerService\Screenshot-WorkerService\Worker.cs:line 260
line 260 is the last on the last line of my parallel.foreach loop. I belive the threads are targeting the same port and causing this.
Denis
Denis2y ago
And why are you using parallel for each, when you have Async methods?
Hercules
HerculesOP2y ago
Is there any issues running async on parallel? I wanted multiple instances of chromedriver to run simultaneously
Denis
Denis2y ago
I just find it strange to use that when you have Task.WhenAll
Denis
Denis2y ago
Stack Overflow
Nesting await in Parallel.ForEach
In a metro app, I need to execute a number of WCF calls. There are a significant number of calls to be made, so I need to do them in a parallel loop. The problem is that the parallel loop exits be...
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server