Skip to content

[🐛 Bug]: [dotnet] WebSocketException from Firefox BiDi on .NET 4.8 after 100 seconds idle #16090

@kevinoid

Description

@kevinoid

Description

On Windows with .NET Framework 4.8 (and presumably 4.7 or 4.6), if the WebDriver BiDi connection to Firefox is idle for more than ~100 seconds, System.Net.WebSockets.WebSocketException (0x80004005): The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseSent' is thrown (caused by a preceding System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake. exception) when the connection is closed or a BiDi operation is attempted:

Unhandled exception WebSocketException with stack trace
Unhandled Exception: System.Net.WebSockets.WebSocketException: The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseReceived'
   at System.Net.WebSockets.WebSocket.ThrowOnInvalidState(WebSocketState state, WebSocketState[] validStates)
   at System.Net.WebSockets.WebSocketBase.<SendAsyncCore>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OpenQA.Selenium.BiDi.Communication.Transport.WebSocketTransport.<SendAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OpenQA.Selenium.BiDi.Communication.Broker.<ExecuteCommandCoreAsync>d__19`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OpenQA.Selenium.BiDi.Communication.Broker.<ExecuteCommandAsync>d__17`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OpenQA.Selenium.BiDi.Session.SessionModule.<UnsubscribeAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OpenQA.Selenium.BiDi.Communication.Broker.<UnsubscribeAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OpenQA.Selenium.BiDi.Subscription.<UnsubscribeAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OpenQA.Selenium.BiDi.Subscription.<DisposeAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ValueTaskAwaiter.GetResult()
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\Kevin\Source\Repos\SeleniumPostTest\Program.cs:line 19
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\Kevin\Source\Repos\SeleniumPostTest\Program.cs:line 19
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Program.<Main>(String[] args)

I've been unable to reproduce the issue on .NET 8 or with ChromeDriver or EdgeDriver.

Reproducible Code

using OpenQA.Selenium.BiDi;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Internal.Logging;

using System;
using System.Threading;

Log.SetLevel(LogEventLevel.Trace);

using var webDriver = new FirefoxDriver(new FirefoxOptions()
{
    UseWebSocketUrl = true,
});

await using var bidi = await webDriver.AsBiDiAsync().ConfigureAwait(false);
await using var logSubscription = await bidi.Log
    .OnEntryAddedAsync(Console.WriteLine);

Thread.Sleep(TimeSpan.FromSeconds(100));

Debugging Logs

17:05:05.261 TRACE SeleniumManager: DEBUG geckodriver not found in PATH
17:05:05.267 TRACE SeleniumManager: DEBUG firefox detected at C:\Program Files\Mozilla Firefox\firefox.exe
17:05:05.267 TRACE SeleniumManager: DEBUG Detected browser: firefox 141.0
17:05:05.267 TRACE SeleniumManager: DEBUG Required driver: geckodriver 0.36.0
17:05:05.268 TRACE SeleniumManager: DEBUG geckodriver 0.36.0 already in the cache
17:05:05.268 TRACE SeleniumManager: INFO Driver path: C:\Users\Kevin\.cache\selenium\geckodriver\win64\0.36.0\geckodriver.exe
17:05:05.268 TRACE SeleniumManager: INFO Browser path: C:\Program Files\Mozilla Firefox\firefox.exe
17:05:05.268 TRACE SeleniumManager: Driver path: C:\Users\Kevin\.cache\selenium\geckodriver\win64\0.36.0\geckodriver.exe
17:05:05.268 TRACE SeleniumManager: Browser path: C:\Program Files\Mozilla Firefox\firefox.exe
1753398305620   geckodriver     INFO    Listening on 127.0.0.1:52126
17:05:08.911 DEBUG HttpCommandExecutor: Executing command: []: newSession
17:05:09.086 TRACE HttpCommandExecutor: >> POST RequestUri: http://localhost:52126/session, Content: System.Net.Http.ByteArrayContent, Headers: 2
{"capabilities":{"firstMatch":[{"browserName":"firefox","webSocketUrl":true,"moz:firefoxOptions":{"binary":"C:\\Program Files\\Mozilla Firefox\\firefox.exe","prefs":{"remote.active-protocols":1}}}]}}
1753398309088   mozrunner::runner       INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "--marionette" "--remote-debugging-port" "52127" "--remote-allow-hosts" "localhost" "-no-remote" "-profile" "C:\\Users\\Kevin\\AppData\\Local\\Temp\\rust_mozprofileQu2EKS"
Read port: 52133
17:05:12.092 TRACE HttpCommandExecutor: << StatusCode: 200, ReasonPhrase: OK, Content: System.Net.Http.StreamContent, Headers: 2
17:05:12.380 DEBUG HttpCommandExecutor: Response: (6f94cb3b-f685-416c-bcff-d2ff8c1a5e48 Success: System.Collections.Generic.Dictionary`2[System.String,System.Object])
17:05:13.392 TRACE WebSocketTransport: BiDi SND --> {"id":1,"method":"session.subscribe","params":{"events":["log.entryAdded"]}}
17:05:13.427 TRACE WebSocketTransport: BiDi RCV <-- {"type":"success","id":1,"result":{"subscription":"6cc48687-88fd-42b0-a86d-fa1b308887d2"}}
17:06:53.255 ERROR Broker: Couldn't process received BiDi remote message: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
   at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.WebSocketBase.<ReceiveAsyncCore>d__45.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Transport.WebSocketTransport.<ReceiveAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Broker.<ReceiveMessagesAsync>d__15.MoveNext()
17:06:53.375 ERROR Broker: Couldn't process received BiDi remote message: System.Net.WebSockets.WebSocketException (0x80004005): The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseSent'
   at System.Net.WebSockets.WebSocket.ThrowOnInvalidState(WebSocketState state, WebSocketState[] validStates)
   at System.Net.WebSockets.WebSocketBase.<ReceiveAsyncCore>d__45.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Transport.WebSocketTransport.<ReceiveAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Broker.<ReceiveMessagesAsync>d__15.MoveNext()
17:06:53.491 TRACE WebSocketTransport: BiDi SND --> {"id":2,"method":"session.unsubscribe","params":{"subscriptions":["6cc48687-88fd-42b0-a86d-fa1b308887d2"]}}
17:06:53.519 ERROR Broker: Couldn't process received BiDi remote message: System.Net.WebSockets.WebSocketException (0x80004005): The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseSent'
   at System.Net.WebSockets.WebSocket.ThrowOnInvalidState(WebSocketState state, WebSocketState[] validStates)
   at System.Net.WebSockets.WebSocketBase.<ReceiveAsyncCore>d__45.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Transport.WebSocketTransport.<ReceiveAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Broker.<ReceiveMessagesAsync>d__15.MoveNext()
17:06:53.772 ERROR Broker: Couldn't process received BiDi remote message: System.Net.WebSockets.WebSocketException (0x80004005): The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseSent'
   at System.Net.WebSockets.WebSocket.ThrowOnInvalidState(WebSocketState state, WebSocketState[] validStates)
   at System.Net.WebSockets.WebSocketBase.<ReceiveAsyncCore>d__45.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Transport.WebSocketTransport.<ReceiveAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at OpenQA.Selenium.BiDi.Communication.Broker.<ReceiveMessagesAsync>d__15.MoveNext()
17:06:54.155 DEBUG HttpCommandExecutor: Executing command: [6f94cb3b-f685-416c-bcff-d2ff8c1a5e48]: quit
17:06:54.156 TRACE HttpCommandExecutor: >> DELETE RequestUri: http://localhost:52126/session/6f94cb3b-f685-416c-bcff-d2ff8c1a5e48, Content: null, Headers: 2
17:06:57.750 TRACE HttpCommandExecutor: << StatusCode: 200, ReasonPhrase: OK, Content: System.Net.Http.StreamContent, Headers: 2
17:06:57.751 DEBUG HttpCommandExecutor: Response: ( Success: )

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions