From e36483413620b31b7a328fd77596d9185ec88c39 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 8 Aug 2025 17:35:00 +0300 Subject: [PATCH 1/6] [dotnet] Fix the issue when service wants to write into disposed stream --- dotnet/src/webdriver/Firefox/FirefoxDriverService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs index 6825a48a32562..156e485d9ff83 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs @@ -279,13 +279,13 @@ protected override void OnDriverProcessDataReceived(object sender, DataReceivedE /// protected override void Dispose(bool disposing) { + base.Dispose(disposing); + if (logWriter != null && disposing) { logWriter.Dispose(); logWriter = null; } - - base.Dispose(disposing); } /// From 28cf415f509400733b45d2fb3338c0e39a25d298 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 8 Aug 2025 18:32:51 +0300 Subject: [PATCH 2/6] Don't block while waiting initialization --- dotnet/src/webdriver/DriverService.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dotnet/src/webdriver/DriverService.cs b/dotnet/src/webdriver/DriverService.cs index cf4ecfd1d5288..b02c2573acd19 100644 --- a/dotnet/src/webdriver/DriverService.cs +++ b/dotnet/src/webdriver/DriverService.cs @@ -256,13 +256,15 @@ public void Start() this.OnDriverProcessStarting(eventArgs); this.driverServiceProcess.Start(); - bool serviceAvailable = this.WaitForServiceInitialization(); - DriverProcessStartedEventArgs processStartedEventArgs = new DriverProcessStartedEventArgs(this.driverServiceProcess); - this.OnDriverProcessStarted(processStartedEventArgs); this.driverServiceProcess.BeginOutputReadLine(); this.driverServiceProcess.BeginErrorReadLine(); + bool serviceAvailable = this.WaitForServiceInitialization(); + + DriverProcessStartedEventArgs processStartedEventArgs = new DriverProcessStartedEventArgs(this.driverServiceProcess); + this.OnDriverProcessStarted(processStartedEventArgs); + if (!serviceAvailable) { throw new WebDriverException($"Cannot start the driver service on {this.ServiceUrl}"); From 222f4ca98dada30859345cfef07c21104f09735e Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 8 Aug 2025 18:36:33 +0300 Subject: [PATCH 3/6] Clean docs --- dotnet/src/webdriver/Firefox/FirefoxDriverService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs index 156e485d9ff83..e00c5c5ce3664 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs @@ -250,7 +250,6 @@ protected override void OnDriverProcessStarting(DriverProcessStartingEventArgs e /// /// The sender of the event. /// The data received event arguments. - /// A value indicating whether the data received is from the error stream. protected override void OnDriverProcessDataReceived(object sender, DataReceivedEventArgs args) { if (string.IsNullOrEmpty(args.Data)) From 4de8964bd9f5d89ee702147374062bd500b9fbe9 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 8 Aug 2025 19:09:52 +0300 Subject: [PATCH 4/6] Unsubscribe --- dotnet/src/webdriver/DriverService.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dotnet/src/webdriver/DriverService.cs b/dotnet/src/webdriver/DriverService.cs index b02c2573acd19..5ddaae03bc4ec 100644 --- a/dotnet/src/webdriver/DriverService.cs +++ b/dotnet/src/webdriver/DriverService.cs @@ -290,6 +290,12 @@ protected virtual void Dispose(bool disposing) this.Stop(); } + if (this.driverServiceProcess is not null) + { + this.driverServiceProcess.OutputDataReceived -= this.OnDriverProcessDataReceived; + this.driverServiceProcess.ErrorDataReceived -= this.OnDriverProcessDataReceived; + } + this.isDisposed = true; } } From cb0561670232300dc072fa1c83f648756214ea92 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 8 Aug 2025 19:12:24 +0300 Subject: [PATCH 5/6] Only while disposing --- dotnet/src/webdriver/DriverService.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dotnet/src/webdriver/DriverService.cs b/dotnet/src/webdriver/DriverService.cs index 5ddaae03bc4ec..6810a85675363 100644 --- a/dotnet/src/webdriver/DriverService.cs +++ b/dotnet/src/webdriver/DriverService.cs @@ -288,12 +288,12 @@ protected virtual void Dispose(bool disposing) if (disposing) { this.Stop(); - } - if (this.driverServiceProcess is not null) - { - this.driverServiceProcess.OutputDataReceived -= this.OnDriverProcessDataReceived; - this.driverServiceProcess.ErrorDataReceived -= this.OnDriverProcessDataReceived; + if (this.driverServiceProcess is not null) + { + this.driverServiceProcess.OutputDataReceived -= this.OnDriverProcessDataReceived; + this.driverServiceProcess.ErrorDataReceived -= this.OnDriverProcessDataReceived; + } } this.isDisposed = true; From 24bf257c3136b509e9b923980bf31709f30c7e3c Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 8 Aug 2025 19:28:51 +0300 Subject: [PATCH 6/6] Leave a comment --- dotnet/src/webdriver/DriverService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DriverService.cs b/dotnet/src/webdriver/DriverService.cs index 6810a85675363..0ed9a16b5dd66 100644 --- a/dotnet/src/webdriver/DriverService.cs +++ b/dotnet/src/webdriver/DriverService.cs @@ -255,8 +255,8 @@ public void Start() DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs(this.driverServiceProcess.StartInfo); this.OnDriverProcessStarting(eventArgs); + // Important: Start the process and immediately begin reading the output and error streams to avoid IO deadlocks. this.driverServiceProcess.Start(); - this.driverServiceProcess.BeginOutputReadLine(); this.driverServiceProcess.BeginErrorReadLine();