-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
[rb] fix silent hang on oversized WebSocket frames #17710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Chandan25sharma
wants to merge
4
commits into
SeleniumHQ:trunk
Choose a base branch
from
Chandan25sharma:trunk
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3989c16
[rb] fix silent hang on oversized WebSocket frames
Chandan25sharma cee234d
[dotnet][bidi] add ConfigureAwait extension to resolve IEventStream<T…
Chandan25sharma 9a852e1
[dotnet][bidi] fix CS0121 ambiguous ConfigureAwait on IEventStream<T>
Chandan25sharma bd3319f
Merge branch 'trunk' of https://github.com/Chandan25sharma/selenium i…
Chandan25sharma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // <copyright file="EventStreamExtensions.cs" company="Selenium Committers"> | ||
| // Licensed to the Software Freedom Conservancy (SFC) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The SFC licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // </copyright> | ||
|
|
||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace OpenQA.Selenium.BiDi; | ||
|
|
||
| public static class EventStreamExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures how awaits on the tasks returned from an iteration of the event stream are performed. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// <see cref="IEventStream{TEventArgs}"/> implements both <see cref="IAsyncEnumerable{T}"/> and | ||
| /// <see cref="IAsyncDisposable"/>, which makes a plain <c>.ConfigureAwait(bool)</c> call ambiguous | ||
| /// (CS0121) because <c>TaskAsyncEnumerableExtensions</c> provides an overload for each interface. | ||
| /// This extension method resolves the ambiguity by explicitly routing to the | ||
| /// <see cref="IAsyncEnumerable{T}"/> overload, which is the behavior callers need when using | ||
| /// <c>await foreach</c>. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <typeparam name="TEventArgs">The event-args type produced by the stream.</typeparam> | ||
| /// <param name="stream">The event stream to configure.</param> | ||
| /// <param name="continueOnCapturedContext"> | ||
| /// <see langword="true"/> to capture and marshal continuation back to the original context; | ||
| /// <see langword="false"/> to continue on a thread-pool thread. | ||
| /// </param> | ||
| /// <returns>A configured enumerable that applies the specified context-capture behavior.</returns> | ||
| public static ConfiguredCancelableAsyncEnumerable<TEventArgs> ConfigureAwait<TEventArgs>( | ||
| this IEventStream<TEventArgs> stream, | ||
| bool continueOnCapturedContext) | ||
| where TEventArgs : EventArgs | ||
| => ((IAsyncEnumerable<TEventArgs>)stream).ConfigureAwait(continueOnCapturedContext); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // <copyright file="EventStreamExtensionsTests.cs" company="Selenium Committers"> | ||
| // Licensed to the Software Freedom Conservancy (SFC) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The SFC licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // </copyright> | ||
|
|
||
| using System.Runtime.CompilerServices; | ||
| using OpenQA.Selenium.BiDi; | ||
|
|
||
| namespace OpenQA.Selenium.Tests.BiDi; | ||
|
|
||
| [Parallelizable(ParallelScope.All)] | ||
| [FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
| class EventStreamExtensionsTests | ||
| { | ||
| private IBiDi _bidi; | ||
| private FakeTransport _transport; | ||
|
|
||
| [SetUp] | ||
| public async Task SetUp() | ||
| { | ||
| _transport = new FakeTransport(); | ||
| _bidi = await Selenium.BiDi.BiDi.ConnectAsync(new Uri("ws://fake"), opts => opts.UseTransport(() => _transport)); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public async Task TearDown() | ||
| { | ||
| await _bidi.DisposeAsync(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ConfigureAwait_ReturnsConfiguredCancelableAsyncEnumerable() | ||
| { | ||
| var stream = await _bidi.Script.RealmDestroyed.StreamAsync() | ||
| .WithResponse(_transport, """{"subscription":"sub-1"}"""); | ||
|
|
||
| // This line previously failed to compile (CS0121) because IEventStream<T> implements | ||
| // both IAsyncEnumerable<T> and IAsyncDisposable and both have a matching ConfigureAwait overload. | ||
| // EventStreamExtensions.ConfigureAwait disambiguates toward IAsyncEnumerable<T>. | ||
| var configured = stream.ConfigureAwait(false); | ||
|
|
||
| Assert.That(configured, Is.InstanceOf<ConfiguredCancelableAsyncEnumerable<BiDi.Script.RealmDestroyedEventArgs>>()); | ||
|
|
||
| await stream.DisposeAsync().WithResponse(_transport); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ConfigureAwait_DeliverEventsThroughConfiguredEnumerable() | ||
| { | ||
| var stream = await _bidi.Script.RealmDestroyed.StreamAsync() | ||
| .WithResponse(_transport, """{"subscription":"sub-1"}"""); | ||
|
|
||
| _transport.EnqueueEvent("script.realmDestroyed", """{"realm":"r-1"}"""); | ||
|
|
||
| var received = new List<BiDi.Script.RealmDestroyedEventArgs>(); | ||
|
|
||
| using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); | ||
|
|
||
| await foreach (var e in stream.ConfigureAwait(false).WithCancellation(cts.Token)) | ||
| { | ||
| received.Add(e); | ||
| break; | ||
| } | ||
|
|
||
| Assert.That(received, Has.Count.EqualTo(1)); | ||
| Assert.That(received[0].Realm.Id, Is.EqualTo("r-1")); | ||
|
|
||
| await stream.DisposeAsync().WithResponse(_transport); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,8 +34,13 @@ class WebSocketConnection | |
| RESPONSE_WAIT_INTERVAL = 0.1 | ||
|
|
||
| MAX_LOG_MESSAGE_SIZE = 9999 | ||
| MAX_FRAME_SIZE = 100 * 1024 * 1024 # 100 MB; DevTools payloads can be large | ||
|
|
||
| def initialize(url:) | ||
| # websocket-ruby exposes max_frame_size only as a global; bump it for devtools use | ||
| # only when the current limit is lower so user-configured values are not overridden | ||
| WebSocket.max_frame_size = MAX_FRAME_SIZE if WebSocket.max_frame_size < MAX_FRAME_SIZE | ||
|
|
||
| @callback_threads = ThreadGroup.new | ||
|
|
||
| @callbacks_mtx = Mutex.new | ||
|
|
@@ -142,6 +147,10 @@ def attach_socket_listener | |
| @callback_threads.add(callback_thread(message['params'], &callback)) | ||
| end | ||
| end | ||
|
|
||
| # websocket-ruby rescues TooLong internally and returns nil from next; | ||
| # raise the stored error so the loop exits instead of spinning at 100% cpu | ||
| raise incoming_frame.error if incoming_frame.error? | ||
|
Comment on lines
+151
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3. Listener failure causes timeouts When attach_socket_listener raises incoming_frame.error, the surrounding rescue only logs and exits the listener thread without closing the socket or surfacing the error to waiting callers. After the listener exits, send_cmd can still successfully write but then waits until its 30s timeout because responses are never enqueued. Agent Prompt
|
||
| end | ||
| rescue *CONNECTION_ERRORS, WebSocket::Error => e | ||
| WebDriver.logger.debug "WebSocket listener closed: #{e.class}: #{e.message}", id: :ws | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
rb/spec/unit/selenium/webdriver/common/websocket_connection_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Licensed to the Software Freedom Conservancy (SFC) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The SFC licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| require File.expand_path('../spec_helper', __dir__) | ||
|
|
||
| module Selenium | ||
| module WebDriver | ||
| describe WebSocketConnection do | ||
| # Stub network I/O so we can test initialize without a real socket. | ||
| before do | ||
| allow_any_instance_of(described_class).to receive(:process_handshake) | ||
| allow_any_instance_of(described_class).to receive(:attach_socket_listener).and_return(nil) | ||
| end | ||
|
|
||
| describe 'MAX_FRAME_SIZE' do | ||
| it 'is 100 MB' do | ||
| expect(described_class::MAX_FRAME_SIZE).to eq(100 * 1024 * 1024) | ||
| end | ||
| end | ||
|
|
||
| describe '#initialize' do | ||
| it 'raises WebSocket.max_frame_size to MAX_FRAME_SIZE when current value is lower' do | ||
| original = WebSocket.max_frame_size | ||
| WebSocket.max_frame_size = 1024 | ||
|
|
||
| described_class.new(url: 'ws://localhost:4444') | ||
|
|
||
| expect(WebSocket.max_frame_size).to eq(described_class::MAX_FRAME_SIZE) | ||
| ensure | ||
| WebSocket.max_frame_size = original | ||
| end | ||
|
|
||
| it 'does not lower WebSocket.max_frame_size when already above MAX_FRAME_SIZE' do | ||
| original = WebSocket.max_frame_size | ||
| higher = described_class::MAX_FRAME_SIZE * 2 | ||
| WebSocket.max_frame_size = higher | ||
|
|
||
| described_class.new(url: 'ws://localhost:4444') | ||
|
|
||
| expect(WebSocket.max_frame_size).to eq(higher) | ||
| ensure | ||
| WebSocket.max_frame_size = original | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Public class missing xml summary
📘 Rule violation✧ QualityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools