Skip to content

Commit 74db60d

Browse files
committed
Thread safe driver service
1 parent 8dd2d6a commit 74db60d

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

dotnet/src/webdriver/DriverService.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public abstract class DriverService : ICommandServer
4141
private bool hideCommandPromptWindow;
4242
private bool isDisposed;
4343
private Process driverServiceProcess;
44+
private object driverServiceProcessLock = new object();
4445
private TimeSpan initializationTimeout = TimeSpan.FromSeconds(20);
4546

4647
/// <summary>
@@ -258,38 +259,42 @@ public void Dispose()
258259
/// </summary>
259260
public void Start()
260261
{
261-
if (this.driverServiceProcess != null)
262+
if (this.driverServiceProcess is null)
262263
{
263-
return;
264-
}
265-
266-
this.driverServiceProcess = new Process();
264+
lock (this.driverServiceProcessLock)
265+
{
266+
if (driverServiceProcess is null)
267+
{
268+
this.driverServiceProcess = new Process();
267269

268-
if (this.driverServicePath != null)
269-
{
270-
this.driverServiceProcess.StartInfo.FileName = Path.Combine(this.driverServicePath, this.driverServiceExecutableName);
271-
}
272-
else
273-
{
274-
this.driverServiceProcess.StartInfo.FileName = new DriverFinder(this.GetDefaultDriverOptions()).GetDriverPath();
275-
}
270+
if (this.driverServicePath != null)
271+
{
272+
this.driverServiceProcess.StartInfo.FileName = Path.Combine(this.driverServicePath, this.driverServiceExecutableName);
273+
}
274+
else
275+
{
276+
this.driverServiceProcess.StartInfo.FileName = new DriverFinder(this.GetDefaultDriverOptions()).GetDriverPath();
277+
}
276278

277-
this.driverServiceProcess.StartInfo.Arguments = this.CommandLineArguments;
278-
this.driverServiceProcess.StartInfo.UseShellExecute = false;
279-
this.driverServiceProcess.StartInfo.CreateNoWindow = this.hideCommandPromptWindow;
279+
this.driverServiceProcess.StartInfo.Arguments = this.CommandLineArguments;
280+
this.driverServiceProcess.StartInfo.UseShellExecute = false;
281+
this.driverServiceProcess.StartInfo.CreateNoWindow = this.hideCommandPromptWindow;
280282

281-
DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs(this.driverServiceProcess.StartInfo);
282-
this.OnDriverProcessStarting(eventArgs);
283+
DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs(this.driverServiceProcess.StartInfo);
284+
this.OnDriverProcessStarting(eventArgs);
283285

284-
this.driverServiceProcess.Start();
285-
bool serviceAvailable = this.WaitForServiceInitialization();
286-
DriverProcessStartedEventArgs processStartedEventArgs = new DriverProcessStartedEventArgs(this.driverServiceProcess);
287-
this.OnDriverProcessStarted(processStartedEventArgs);
286+
this.driverServiceProcess.Start();
287+
bool serviceAvailable = this.WaitForServiceInitialization();
288+
DriverProcessStartedEventArgs processStartedEventArgs = new DriverProcessStartedEventArgs(this.driverServiceProcess);
289+
this.OnDriverProcessStarted(processStartedEventArgs);
288290

289-
if (!serviceAvailable)
290-
{
291-
string msg = "Cannot start the driver service on " + this.ServiceUrl;
292-
throw new WebDriverException(msg);
291+
if (!serviceAvailable)
292+
{
293+
string msg = "Cannot start the driver service on " + this.ServiceUrl;
294+
throw new WebDriverException(msg);
295+
}
296+
}
297+
}
293298
}
294299
}
295300

dotnet/test/common/Environment/EnvironmentManager.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class EnvironmentManager
3434
private Type driverType;
3535
private Browser browser;
3636
private DriverService driverService;
37+
private object driverServiceLock = new object();
3738
private IWebDriver driver;
3839
private UrlBuilder urlBuilder;
3940
private TestWebServer webServer;
@@ -270,13 +271,17 @@ public UrlBuilder UrlBuilder
270271

271272
public DriverService GetCurrentDriverService()
272273
{
273-
if (driverService != null)
274+
if (driverService is null)
274275
{
275-
return driverService;
276+
lock (driverServiceLock)
277+
{
278+
if (driverService is null)
279+
{
280+
driverService = driverFactory.CreateDriverService(driverType);
281+
}
282+
}
276283
}
277284

278-
driverService = driverFactory.CreateDriverService(driverType);
279-
280285
return driverService;
281286
}
282287

0 commit comments

Comments
 (0)