Closed arnonax-tr closed 18 hours ago
@arnonax-tr, thank you for creating this issue. We will troubleshoot it as soon as we can.
Triage this issue by using labels.
If information is missing, add a helpful comment and then I-issue-template
label.
If the issue is a question, add the I-question
label.
If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted
label.
If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C),
add the applicable G-*
label, and it will provide the correct link and auto-close the
issue.
After troubleshooting the issue, please add the R-awaiting answer
label.
Thank you!
This issue is obvious.
network.RequestPaused += async (_, args) =>
{
await network.ContinueRequestWithoutModification(args.RequestData);
}
which may lead to System.IO.IOException : Cannot access a closed stream
.
It happens because your async event handler is invoked in background, while main execution program is continuing. And there is a chance that main execution flow disposes driver earlier than your network.ContinueRequestWithoutModification(args.RequestData)
.
This situation is fixed in new BiDi approach (https://github.com/SeleniumHQ/selenium/pull/14318). I have no ideas how to fix it.
Probably making your event handler sync can help (at least please try):
network.RequestPaused += (_, args) =>
{
Task.Run(async () => await network.ContinueRequestWithoutModification(args.RequestData));
}
EDITED:
You may prove my theory via adding a delay in your async handler like await Task.Delay(5_000)
before invocation of network.Continue...
.
And there is a chance that main execution flow disposes driver earlier than your
network.ContinueRequestWithoutModification(args.RequestData).
I don't think so. That's exactly the reason I added the CountdownEvent
object (executingHandlers
). This should ensure that I'm not disposing the driver (or even the DevToolsSession
) before all event handlers are done. (Or if they just start after dispose has already started, they immediately exit due to the if (!executingHandlers.TryAddCount())
statement)
Regarding the BiDi approach: do you have an ETA for when it will be available? Also I'll be grateful if you can provide an example of how my code should look like with that approach.
That's exactly the reason I added the CountdownEvent object (executingHandlers)
Your event handler is async void, selenium invokes (actually schedules your delegate in TaskScheduler to be invoked), and we don't know when exactly your handler is actually invoked. Moreover, in selenium we really fire and forget event handlers. This is design issue, which may lead to unexpected behavior in race conditions. This design issue is addressed in BiDi implementation (or at least should be addressed).
No ETA, PR is under review.
Regarding the BiDi approach: do you have an ETA for when it will be available? Also I'll be grateful if you can provide an example of how my code should look like with that approach.
I saw that the Bidi PR was merged to trunk. Does it mean that it will be available in the next release?
Also, are there examples or documentation on how to use it for capturing and manipulating network traffic?
There is nightly builds you can try right now: https://github.com/SeleniumHQ/selenium/pkgs/nuget/Selenium.WebDriver
Documentation is not available yet, hopefully the following code may help you to get started:
var context = await driver.AsBidirectionalContextAsync();
await context.Network.OnBeforeRequestSentAsync(e => Console.WriteLine(e));
await context.NavigateAsync("https://selenium.dev", new() { Wait = ReadinessState.Complete });
await context.Network.InterceptRequestAsync(async e => await e.Request.Request.FailAsync());
I will close this as the issue has not had any more activity.
What happened?
This bug happens pretty rarely, but it does. I ran the following test with
[Repeat(1000)]
to reproduce it:And I got the following exception:
How can we reproduce the issue?
Relevant log output
Operating System
Windows 11
Selenium version
C# 11 (dotnet 8), Selenium.WebDriver 4.23.0
What are the browser(s) and version(s) where you see this issue?
Chrome 128
What are the browser driver(s) and version(s) where you see this issue?
ChromeDriver 128.0.6613.8600
Are you using Selenium Grid?
No response