Skip to content

Commit 8fc36d2

Browse files
committed
[dotnet] Make DriverService.Start() thread-safe
1 parent 9b0ccf1 commit 8fc36d2

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

dotnet/src/webdriver/DriverService.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace OpenQA.Selenium
3737
public abstract class DriverService : ICommandServer
3838
{
3939
private bool isDisposed;
40+
private readonly object driverServiceProcessLock = new();
4041
private Process? driverServiceProcess;
4142

4243
/// <summary>
@@ -220,42 +221,55 @@ public void Dispose()
220221
[MemberNotNull(nameof(driverServiceProcess))]
221222
public void Start()
222223
{
223-
if (this.driverServiceProcess != null)
224+
if (this.driverServiceProcess is null)
224225
{
225-
return;
226-
}
226+
lock (this.driverServiceProcessLock)
227+
{
228+
if (this.driverServiceProcess is null)
229+
{
230+
var driverServiceProcess = new Process();
227231

228-
this.driverServiceProcess = new Process();
232+
try
233+
{
234+
if (this.DriverServicePath != null)
235+
{
236+
if (this.DriverServiceExecutableName is null)
237+
{
238+
throw new InvalidOperationException("If the driver service path is specified, the driver service executable name must be as well");
239+
}
229240

230-
if (this.DriverServicePath != null)
231-
{
232-
if (this.DriverServiceExecutableName is null)
233-
{
234-
throw new InvalidOperationException("If the driver service path is specified, the driver service executable name must be as well");
235-
}
241+
driverServiceProcess.StartInfo.FileName = Path.Combine(this.DriverServicePath, this.DriverServiceExecutableName);
242+
}
243+
else
244+
{
245+
driverServiceProcess.StartInfo.FileName = new DriverFinder(this.GetDefaultDriverOptions()).GetDriverPath();
246+
}
236247

237-
this.driverServiceProcess.StartInfo.FileName = Path.Combine(this.DriverServicePath, this.DriverServiceExecutableName);
238-
}
239-
else
240-
{
241-
this.driverServiceProcess.StartInfo.FileName = new DriverFinder(this.GetDefaultDriverOptions()).GetDriverPath();
242-
}
248+
driverServiceProcess.StartInfo.Arguments = this.CommandLineArguments;
249+
driverServiceProcess.StartInfo.UseShellExecute = false;
250+
driverServiceProcess.StartInfo.CreateNoWindow = this.HideCommandPromptWindow;
243251

244-
this.driverServiceProcess.StartInfo.Arguments = this.CommandLineArguments;
245-
this.driverServiceProcess.StartInfo.UseShellExecute = false;
246-
this.driverServiceProcess.StartInfo.CreateNoWindow = this.HideCommandPromptWindow;
252+
this.OnDriverProcessStarting(new DriverProcessStartingEventArgs(driverServiceProcess.StartInfo));
247253

248-
DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs(this.driverServiceProcess.StartInfo);
249-
this.OnDriverProcessStarting(eventArgs);
254+
driverServiceProcess.Start();
255+
bool serviceAvailable = this.WaitForServiceInitialization();
250256

251-
this.driverServiceProcess.Start();
252-
bool serviceAvailable = this.WaitForServiceInitialization();
253-
DriverProcessStartedEventArgs processStartedEventArgs = new DriverProcessStartedEventArgs(this.driverServiceProcess);
254-
this.OnDriverProcessStarted(processStartedEventArgs);
257+
this.OnDriverProcessStarted(new DriverProcessStartedEventArgs(driverServiceProcess));
255258

256-
if (!serviceAvailable)
257-
{
258-
throw new WebDriverException($"Cannot start the driver service on {this.ServiceUrl}");
259+
if (!serviceAvailable)
260+
{
261+
throw new WebDriverException($"Cannot start the driver service on {this.ServiceUrl}");
262+
}
263+
}
264+
catch
265+
{
266+
driverServiceProcess.Dispose();
267+
throw;
268+
}
269+
270+
this.driverServiceProcess = driverServiceProcess;
271+
}
272+
}
259273
}
260274
}
261275

0 commit comments

Comments
 (0)