Skip to content

Commit 5990c6a

Browse files
committed
Improve websocket connection stability
When driver/browser are forcefully closed, we don't want to raise from the websocket thread and gracefully stop it instead.
1 parent 4041b4d commit 5990c6a

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
module Selenium
2323
module WebDriver
2424
class WebSocketConnection
25+
CONNECTION_ERRORS = [
26+
Errno::ECONNRESET, # connection is aborted (browser process was killed)
27+
Errno::EPIPE # broken pipe (browser process was killed)
28+
].freeze
29+
2530
RESPONSE_WAIT_TIMEOUT = 30
2631
RESPONSE_WAIT_INTERVAL = 0.1
2732

@@ -90,6 +95,8 @@ def attach_socket_listener
9095
end
9196
end
9297
end
98+
rescue *CONNECTION_ERRORS
99+
Thread.stop
93100
end
94101
end
95102

@@ -122,6 +129,8 @@ def callback_thread(params)
122129
Thread.current.report_on_exception = true
123130

124131
yield params
132+
rescue *CONNECTION_ERRORS
133+
Thread.stop
125134
end
126135
end
127136

rb/spec/integration/selenium/webdriver/devtools_spec.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@ module WebDriver
3030
end
3131

3232
it 'supports events' do
33-
callback = instance_double(Proc, call: nil)
34-
35-
driver.devtools.page.enable
36-
driver.devtools.page.on(:load_event_fired) { callback.call }
37-
driver.navigate.to url_for('xhtmlTest.html')
38-
sleep 0.5
39-
40-
expect(callback).to have_received(:call).at_least(:once)
33+
expect { |block|
34+
driver.devtools.page.enable
35+
driver.devtools.page.on(:load_event_fired, &block)
36+
driver.navigate.to url_for('xhtmlTest.html')
37+
sleep 0.5
38+
}.to yield_control
4139
end
4240

4341
it 'propagates errors in events' do
44-
driver.devtools.page.enable
45-
driver.devtools.page.on(:load_event_fired) { raise "This is fine!" }
46-
expect { driver.navigate.to url_for('xhtmlTest.html') }.to raise_error(RuntimeError, "This is fine!")
42+
expect {
43+
driver.devtools.page.enable
44+
driver.devtools.page.on(:load_event_fired) { raise "This is fine!" }
45+
driver.navigate.to url_for('xhtmlTest.html')
46+
sleep 0.5
47+
}.to raise_error(RuntimeError, "This is fine!")
4748
end
4849

4950
context 'authentication', except: {browser: %i[firefox firefox_nightly],

0 commit comments

Comments
 (0)