Repository navigation
feat(browser)!: add disconnect and shutdown, drop session callbacks when Chrome detaches a target - #750
Conversation
…hen Chrome detaches a target browser.disconnect closes the connection and leaves the browser running, keeping browser.process, so close, disconnect and process.stop make up a custom shutdown. quit is disconnect followed by process.stop and still stops a browser that was disconnected first. browser.shutdown sends Browser.close, disconnects and stops a launched process with process.stop(grace:), which waits for it to exit on its own before TERM. Unlike quit it also shuts down a remote browser. A session's event callbacks are now removed when Chrome reports it detached, which also covers a page that closes itself, e.g. by window.close(), whose callbacks used to leak. The close_connection chain left over from per-page WebSockets is removed: page.close_connection, worker.close_connection, Target#close_connection, Context#close_targets_connection and Contexts#close_connections. SessionClient#close is renamed to unsubscribe. Browser-driven specs that sat in spec/unit move to the feature specs.
6265757 to
0129a9a
Compare
|
Repro, stopping Chrome so require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "ferrum", github: "rubycdp/ferrum", ref: ENV.fetch("REF", "main")
end
def threads = Thread.list.count { |t| t.alive? && !t.inspect.include?("thread_pool_executor") }
launched = Ferrum::Browser.new
before = threads
remote = Ferrum::Browser.new(ws_url: launched.process.ws_url.to_s, timeout: 2)
socket = remote.client.instance_variable_get(:@ws).instance_variable_get(:@sock)
puts "connected: +#{threads - before} threads"
Process.kill("STOP", launched.process.pid) # Chrome stops answering, so Browser.close times out
begin
remote.shutdown
rescue Ferrum::TimeoutError => e
puts "shutdown raised #{e.class}"
end
sleep 1
puts "after shutdown: +#{threads - before} threads, client #{remote.client ? "still set" : "nil"}, socket #{socket.closed? ? "closed" : "open"}"
Process.kill("CONT", launched.process.pid)
remote.quit
launched.quitOn main (9199cfb): Backtest on a hung Chrome (SIGSTOP) and on a
Impact. The docs present Fix, disconnecting in the begin
close_browser if @client
- disconnect
ensure
- thread = @process&.stop(wait: wait, grace: Process::Killer::KILL_TIMEOUT)
- @process = nil
+ begin
+ disconnect
+ ensure
+ thread = @process&.stop(wait: wait, grace: Process::Killer::KILL_TIMEOUT)
+ @process = nil
+ end
endWith it, the describe Ferrum::Browser do
describe "#shutdown" do
[Ferrum::TimeoutError, Ferrum::BrowserError.new("message" => "not allowed")].each do |error|
it "disconnects when Browser.close raises #{error.is_a?(Class) ? error : error.class}", :launch do
browser = Ferrum::Browser.new
allow(browser).to receive(:close).and_raise(error)
expect { browser.shutdown }.to raise_error(error.is_a?(Class) ? error : error.class)
expect(browser.client).to be_nil
expect(browser.process).to be_nil
ensure
browser&.quit
end
end
end
endNit: the |
Teardown now comes in steps you can combine yourself.
quitstays the safe default, andshutdownalways ends the browser.browser.closesendsBrowser.close, so Chrome exits gracefully, a remote one included.browser.disconnect(new) closes the connection and leaves the browser running.browser.processstays reachable, so a launched browser can be stopped later.browser.process.stop(grace:)kills a launched browser, reaps it and removes its temporary user data directory.grace:(new) waits that many seconds for it to exit on its own before signaling.quitisdisconnectfollowed byprocess.stop, and it now also stops a browser that was disconnected first. On a remote browser it still only disconnects.browser.shutdown(new) isclose,disconnect, thenprocess.stop(grace: 2). A launched Chrome exits on its own, which lets it save a persistent:user_data_dir, and is killed only if it doesn't. Unlikequit, it also shuts down a remote browser. A browser that doesn't answerBrowser.closeraisesTimeoutError; a launched one is killed first.The
close_connectionchain was left over from when every page had its own WebSocket. With a single connection it only removed a session's event callbacks, and only when the close went through Ferrum, so a page that closed itself, e.g. withwindow.close(), kept its callbacks for the rest of the browser's life. Now a session's callbacks are removed when Chrome reports it detached, whatever closed it.Breaking:
page.close_connectionandworker.close_connectionare removed. The internalTarget#close_connection,Context#close_targets_connectionandContexts#close_connectionsare removed too, andSessionClient#closeis renamed tounsubscribe.Browser-driven specs that were in
spec/unitmove to the feature specs.