Skip to content

Commit 481e0d7

Browse files
committed
keep error propagation for devtools but mark deprecated
1 parent 7333da6 commit 481e0d7

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

rb/lib/selenium/webdriver/bidi.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BiDi
3232
autoload :InterceptedItem, 'selenium/webdriver/bidi/network/intercepted_item'
3333

3434
def initialize(url:)
35-
@ws = WebSocketConnection.new(url: url)
35+
@ws = WebSocketConnection.new(url: url, protocol: :bidi)
3636
end
3737

3838
def close

rb/lib/selenium/webdriver/common/websocket_connection.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class WebSocketConnection
3535

3636
MAX_LOG_MESSAGE_SIZE = 9999
3737

38-
def initialize(url:)
38+
def initialize(url:, protocol: nil)
3939
@callback_threads = ThreadGroup.new
4040

4141
@callbacks_mtx = Mutex.new
@@ -45,6 +45,7 @@ def initialize(url:)
4545
@closing = false
4646
@session_id = nil
4747
@url = url
48+
@protocol = protocol
4849

4950
process_handshake
5051
@socket_thread = attach_socket_listener
@@ -91,7 +92,7 @@ def remove_callback(event, id)
9192
callbacks_for_event = callbacks[event]
9293
return if callbacks_for_event.reject! { |cb| cb.object_id == id }
9394

94-
ids = callbacks[event]&.map(&:object_id)
95+
ids = callbacks_for_event.map(&:object_id)
9596
raise Error::WebDriverError, "Callback with ID #{id} does not exist for event #{event}: #{ids}"
9697
end
9798
end
@@ -109,9 +110,7 @@ def send_cmd(**payload)
109110
raise e, "WebSocket is closed (#{e.class}: #{e.message})"
110111
end
111112

112-
wait.until do
113-
@messages_mtx.synchronize { messages.delete(id) }
114-
end
113+
wait.until { @messages_mtx.synchronize { messages.delete(id) } }
115114
end
116115

117116
private
@@ -140,9 +139,8 @@ def attach_socket_listener
140139
message = process_frame(frame)
141140
next unless message['method']
142141

143-
params = message['params']
144142
@messages_mtx.synchronize { callbacks[message['method']].dup }.each do |callback|
145-
@callback_threads.add(callback_thread(params, &callback))
143+
@callback_threads.add(callback_thread(message['params'], &callback))
146144
end
147145
end
148146
end
@@ -178,10 +176,14 @@ def callback_thread(params)
178176
rescue Error::WebDriverError, *CONNECTION_ERRORS => e
179177
WebDriver.logger.debug "Callback aborted: #{e.class}: #{e.message}", id: :ws
180178
rescue StandardError => e
181-
# Main thread needs to know that it can stop waiting
182-
Thread.main.raise(e) if params['isBlocked']
183179
return if @closing
184180

181+
if devtools?
182+
# Async thread exceptions are not deterministic and should not be relied on; we should stop
183+
WebDriver.logger.deprecate('propogating errors from DevTools callbacks')
184+
Thread.main.raise(e)
185+
end
186+
185187
bt = Array(e.backtrace).first(5).join("\n")
186188
WebDriver.logger.error "Callback error: #{e.class}: #{e.message}\n#{bt}", id: :ws
187189
end
@@ -208,6 +210,14 @@ def ws
208210
@ws ||= WebSocket::Handshake::Client.new(url: @url)
209211
end
210212

213+
def devtools?
214+
@protocol == :devtools
215+
end
216+
217+
def bidi?
218+
@protocol == :bidi
219+
end
220+
211221
def next_id
212222
@id ||= 0
213223
@id += 1

rb/lib/selenium/webdriver/devtools.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DevTools
2929
autoload :Response, 'selenium/webdriver/devtools/response'
3030

3131
def initialize(url:, target_type:)
32-
@ws = WebSocketConnection.new(url: url)
32+
@ws = WebSocketConnection.new(url: url, protocol: :devtools)
3333
@session_id = nil
3434
start_session(target_type: target_type)
3535
end

rb/sig/lib/selenium/webdriver/common/websocket_connection.rbs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Selenium
55

66
@callback_threads: untyped
77

8+
@protocol: Symbol?
9+
810
@session_id: untyped
911

1012
@url: untyped
@@ -33,7 +35,7 @@ module Selenium
3335

3436
MAX_LOG_MESSAGE_SIZE: Integer
3537

36-
def initialize: (url: untyped) -> void
38+
def initialize: (url: String, ?protocol?: Symbol) -> void
3739

3840
def add_callback: (untyped event) { () -> void } -> untyped
3941

@@ -47,6 +49,10 @@ module Selenium
4749

4850
private
4951

52+
def bidi?: -> bool
53+
54+
def devtools?: -> bool
55+
5056
def messages: () -> untyped
5157

5258
def process_handshake: () -> untyped

0 commit comments

Comments
 (0)