Skip to content

Commit 6e34ed6

Browse files
authored
[dotnet] Fix the issue when service wants to write into disposed stream (#16148)
1 parent 3c1a376 commit 6e34ed6

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

dotnet/src/webdriver/DriverService.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,16 @@ public void Start()
255255
DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs(this.driverServiceProcess.StartInfo);
256256
this.OnDriverProcessStarting(eventArgs);
257257

258+
// Important: Start the process and immediately begin reading the output and error streams to avoid IO deadlocks.
258259
this.driverServiceProcess.Start();
260+
this.driverServiceProcess.BeginOutputReadLine();
261+
this.driverServiceProcess.BeginErrorReadLine();
262+
259263
bool serviceAvailable = this.WaitForServiceInitialization();
264+
260265
DriverProcessStartedEventArgs processStartedEventArgs = new DriverProcessStartedEventArgs(this.driverServiceProcess);
261266
this.OnDriverProcessStarted(processStartedEventArgs);
262267

263-
this.driverServiceProcess.BeginOutputReadLine();
264-
this.driverServiceProcess.BeginErrorReadLine();
265-
266268
if (!serviceAvailable)
267269
{
268270
throw new WebDriverException($"Cannot start the driver service on {this.ServiceUrl}");
@@ -286,6 +288,12 @@ protected virtual void Dispose(bool disposing)
286288
if (disposing)
287289
{
288290
this.Stop();
291+
292+
if (this.driverServiceProcess is not null)
293+
{
294+
this.driverServiceProcess.OutputDataReceived -= this.OnDriverProcessDataReceived;
295+
this.driverServiceProcess.ErrorDataReceived -= this.OnDriverProcessDataReceived;
296+
}
289297
}
290298

291299
this.isDisposed = true;

dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ protected override void OnDriverProcessStarting(DriverProcessStartingEventArgs e
250250
/// </summary>
251251
/// <param name="sender">The sender of the event.</param>
252252
/// <param name="args">The data received event arguments.</param>
253-
/// <param name="isError">A value indicating whether the data received is from the error stream.</param>
254253
protected override void OnDriverProcessDataReceived(object sender, DataReceivedEventArgs args)
255254
{
256255
if (string.IsNullOrEmpty(args.Data))
@@ -279,13 +278,13 @@ protected override void OnDriverProcessDataReceived(object sender, DataReceivedE
279278
/// </remarks>
280279
protected override void Dispose(bool disposing)
281280
{
281+
base.Dispose(disposing);
282+
282283
if (logWriter != null && disposing)
283284
{
284285
logWriter.Dispose();
285286
logWriter = null;
286287
}
287-
288-
base.Dispose(disposing);
289288
}
290289

291290
/// <summary>

0 commit comments

Comments
 (0)