Class: Selenium::WebDriver::WebSocketConnection Private
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | rb/lib/selenium/webdriver/common/websocket_connection.rb |
Constant Summary
-
CONNECTION_ERRORS =
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 26[ Errno::ECONNRESET, # connection is aborted (browser process was killed) Errno::EPIPE, # broken pipe (browser process was killed) Errno::EBADF, # file descriptor already closed (double-close or GC) IOError, # Ruby socket read/write after close EOFError # socket reached EOF after remote closed cleanly ].freeze
-
MAX_FRAME_SIZE =
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 46
websocket-ruby defaults to a 20MB limit and silently drops larger frames, which can stall the listener. CDP payloads (e.g. large data: URLs) can exceed that, so raise the ceiling for our connections. The gem only exposes the limit as process-global state, so it is raised (never lowered) and not restored on close - concurrent connections share the value. Other bindings rely on their own websocket clients' limits; this constant only affects websocket-ruby.
100 * 1024 * 1024
-
MAX_LOG_MESSAGE_SIZE =
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 379999 -
RESPONSE_WAIT_INTERVAL =
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 350.1 -
RESPONSE_WAIT_TIMEOUT =
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 3430
Class Method Summary
-
.new(url:) ⇒ WebSocketConnection
constructor
Internal use only
100MB.
Instance Attribute Summary
-
#frame_dropped? ⇒ Boolean
readonly
private
Internal use only
True when the buffered frame could not be decoded (e.g. exceeds MAX_FRAME_SIZE).
Instance Method Summary
- #add_callback(event, &block) Internal use only
- #callbacks Internal use only
-
#close
Internal use only
Idempotent: the listener may already have initiated shutdown (see #frame_dropped?), so always close the socket and join threads rather than short-circuiting on @closing.
- #remove_callback(event, id) Internal use only
- #send_cmd(**payload) Internal use only
- #apply_frame_size_limit private Internal use only
- #attach_socket_listener private Internal use only
- #await_response(id) private Internal use only
- #callback_thread(params) private Internal use only
- #close_socket private Internal use only
- #incoming_frame private Internal use only
- #messages private Internal use only
- #next_id private Internal use only
- #pending_responses private Internal use only
- #process_frame(frame) private Internal use only
- #process_handshake private Internal use only
- #process_incoming_frames private Internal use only
-
#release_pending_responses
private
Internal use only
Once the socket is closed no reply can arrive, so wake waiters instead of letting them run out the timeout.
- #socket private Internal use only
- #ws private Internal use only
Constructor Details
.new(url:) ⇒ WebSocketConnection
100MB
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 48
def initialize(url:) @callback_threads = ThreadGroup.new @callbacks_mtx = Mutex.new @messages_mtx = Mutex.new @closing_mtx = Mutex.new @closing = false @session_id = nil @url = url apply_frame_size_limit process_handshake @socket_thread = attach_socket_listener end
Instance Attribute Details
#frame_dropped? ⇒ Boolean (readonly, private)
True when the buffered frame could not be decoded (e.g. exceeds MAX_FRAME_SIZE). websocket-ruby swallows the error and keeps returning nil, so surface it and close the connection here instead of leaving a dead listener on an open socket.
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 213
def frame_dropped? return false unless incoming_frame.error? WebDriver.logger.error("WebSocket frame dropped (#{incoming_frame.error}); if payloads can legitimately " \ "exceed #{WebSocket.max_frame_size} bytes, set WebSocket.max_frame_size " \ 'to a higher value', id: :ws) close_socket true end
Instance Method Details
#add_callback(event, &block)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 83
def add_callback(event, &block) @callbacks_mtx.synchronize do callbacks[event] << block block.object_id end end
#apply_frame_size_limit (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 223
def apply_frame_size_limit WebSocket.max_frame_size = MAX_FRAME_SIZE if WebSocket.max_frame_size < MAX_FRAME_SIZE end
#attach_socket_listener (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 168
def attach_socket_listener Thread.new do Thread.current.report_on_exception = false loop do break if @closing incoming_frame << socket.readpartial(1024) process_incoming_frames break if frame_dropped? end rescue *CONNECTION_ERRORS, WebSocket::Error => e WebDriver.logger.debug "WebSocket listener closed: #{e.class}: #{e.}", id: :ws ensure # Nothing reads replies once the listener stops, however it stopped close_socket end end
#await_response(id) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 143
def await_response(id) deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + RESPONSE_WAIT_TIMEOUT @messages_mtx.synchronize do until .key?(id) raise IOError, 'WebSocket connection closed while waiting for a response' if @closing remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) raise Error::TimeoutError, "timed out after #{RESPONSE_WAIT_TIMEOUT} seconds" if remaining <= 0 pending_responses[id].wait(@messages_mtx, remaining) end .delete(id) end end
#callback_thread(params) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 252
def callback_thread(params) Thread.new do Thread.current.abort_on_exception = false Thread.current.report_on_exception = false next if @closing yield params rescue Error::WebDriverError, *CONNECTION_ERRORS => e WebDriver.logger.debug "Callback aborted: #{e.class}: #{e.}", id: :ws rescue StandardError => e next if @closing bt = Array(e.backtrace).first(5).join("\n") WebDriver.logger.error "Callback error: #{e.class}: #{e.}\n#{bt}", id: :ws end end
#callbacks
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 79
def callbacks @callbacks ||= Hash.new { |callbacks, event| callbacks[event] = [] } end
#close
Idempotent: the listener may already have initiated shutdown (see #frame_dropped?), so always close the socket and join threads rather than short-circuiting on @closing.
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 67
def close close_socket # Let threads unwind instead of calling exit @socket_thread&.join(0.5) unless @socket_thread == Thread.current @callback_threads.list.each do |thread| thread.join(0.5) rescue StandardError => e WebDriver.logger.debug "Failed to join thread during close: #{e.class}: #{e.}", id: :ws end end
#close_socket (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 188
def close_socket @closing_mtx.synchronize { @closing = true } socket.close rescue *CONNECTION_ERRORS => e WebDriver.logger.debug "WebSocket socket closed: #{e.class}: #{e.}", id: :ws ensure release_pending_responses end
#incoming_frame (private)
[ GitHub ]#messages (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 135
def @messages ||= {} end
#next_id (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 286
def next_id @id = (@id || 0) + 1 end
#pending_responses (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 139
def pending_responses @pending_responses ||= {} end
#process_frame(frame) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 231
def process_frame(frame) = frame.to_s # Firefox will periodically fail on unparsable empty frame return {} if .empty? msg = JSON.parse() if msg.key?('id') @messages_mtx.synchronize do # Replies to commands that already timed out are dropped so they don't accumulate if (waiter = pending_responses[msg['id']]) [msg['id']] = msg waiter.signal end end end WebDriver.logger.debug('WebSocket <-', id: :ws) { msg.to_s[...MAX_LOG_MESSAGE_SIZE] } msg end
#process_handshake (private)
[ GitHub ]#process_incoming_frames (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 197
def process_incoming_frames while (frame = incoming_frame.next) break if @closing = process_frame(frame) next unless ['method'] @messages_mtx.synchronize { callbacks[['method']].dup }.each do |callback| @callback_threads.add(callback_thread(['params'], &callback)) end end end
#release_pending_responses (private)
Once the socket is closed no reply can arrive, so wake waiters instead of letting them run out the timeout
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 159
def release_pending_responses @messages_mtx.synchronize { pending_responses.each_value(&:broadcast) } end
#remove_callback(event, id)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 90
def remove_callback(event, id) @callbacks_mtx.synchronize do return if @closing callbacks_for_event = callbacks[event] return if callbacks_for_event.reject! { |cb| cb.object_id == id } ids = callbacks_for_event.map(&:object_id) raise Error::WebDriverError, "Callback with ID #{id} does not exist for event #{event}: #{ids}" end end
#send_cmd(**payload)
# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 102
def send_cmd(**payload) # IOError to match what writing to an already-closed socket raises raise IOError, 'WebSocket connection is closed' if @closing # Callbacks send commands from their own threads, so the id must be # allocated and registered atomically or two commands can share one id = @messages_mtx.synchronize do next_id.tap { |new_id| pending_responses[new_id] = ConditionVariable.new } end begin data = payload.merge(id: id) WebDriver.logger.debug('WebSocket ->', id: :ws) { data.to_s[...MAX_LOG_MESSAGE_SIZE] } data = JSON.generate(data) out_frame = WebSocket::Frame::Outgoing::Client.new(version: ws.version, data: data, type: 'text') begin socket.write(out_frame.to_s) rescue *CONNECTION_ERRORS => e raise e, "WebSocket is closed (#{e.class}: #{e.})" end await_response(id) ensure @messages_mtx.synchronize do pending_responses.delete(id) .delete(id) end end end
#socket (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 269
def socket @socket ||= if URI(@url).scheme == 'wss' socket = TCPSocket.new(ws.host, ws.port) socket = OpenSSL::SSL::SSLSocket.new(socket, OpenSSL::SSL::SSLContext.new) socket.sync_close = true socket.connect socket else TCPSocket.new(ws.host, ws.port) end end
#ws (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/websocket_connection.rb', line 282
def ws @ws ||= WebSocket::Handshake::Client.new(url: @url) end