diff --git a/CHANGELOG.md b/CHANGELOG.md index a4340cfe..0b354092 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ - `FERRUM_CLICK_WAIT` and `FERRUM_GOTO_WAIT`, will be removed in 0.22. Use `FERRUM_NAVIGATION_WAIT` instead ### Fixed +- A headful browser with `xvfb: true` sometimes started before Xvfb was ready and exited at once with + `ProcessTimeoutError`, Ferrum waits for the display now - `page.command(method, wait:)` ran a subclass override of `command`, like Cuprite's, twice - `evaluate` of a result nested about 100 levels deep raised an opaque `BrowserError` about Chrome's CBOR stack limit, it raises `ResultTooDeepError` now, a `BrowserError` that says to return less or use `evaluate_handle` diff --git a/lib/ferrum/browser/xvfb.rb b/lib/ferrum/browser/xvfb.rb index c2b0d532..b1618c6c 100644 --- a/lib/ferrum/browser/xvfb.rb +++ b/lib/ferrum/browser/xvfb.rb @@ -32,16 +32,27 @@ def initialize(options) @screen_size = "#{options.window_size.join('x')}x24" @display_id = (Time.now.to_f * 1000).to_i % 100_000_000 + @timeout = options.process_timeout end # - # Spawns the Xvfb process on the configured display. + # Spawns the Xvfb process on the configured display and waits until it + # accepts connections, so a browser started right after finds the display. # # @return [void] # + # @raise [Error] + # When Xvfb exits or isn't ready within `:process_timeout`. + # def start - @pid = ::Process.spawn("#{@path} :#{display_id} -screen 0 #{screen_size}") + reader, writer = IO.pipe + @pid = ::Process.spawn(@path, ":#{display_id}", "-screen", "0", screen_size, "-displayfd", "1", out: writer) ::Process.detach(@pid) + writer.close + wait_for_display(reader) + ensure + reader&.close + writer&.close end # @@ -52,6 +63,15 @@ def start def to_env { "DISPLAY" => ":#{display_id}" } end + + private + + def wait_for_display(io) + return if io.wait_readable(@timeout) && io.gets + + Process::Killer.kill(@pid) + raise Error, "Xvfb exited or wasn't ready on display :#{display_id} within #{@timeout} seconds" + end end end end diff --git a/sig/ferrum/browser/xvfb.rbs b/sig/ferrum/browser/xvfb.rbs index c9bc7cdf..5eb7651e 100644 --- a/sig/ferrum/browser/xvfb.rbs +++ b/sig/ferrum/browser/xvfb.rbs @@ -16,6 +16,10 @@ module Ferrum def start: () -> untyped def to_env: () -> ::Hash[::String, ::String] + + private + + def wait_for_display: (::IO io) -> void end end end diff --git a/spec/browser/xvfb_spec.rb b/spec/browser/xvfb_spec.rb index 78e6abe1..d715fc93 100644 --- a/spec/browser/xvfb_spec.rb +++ b/spec/browser/xvfb_spec.rb @@ -53,6 +53,22 @@ expect(process_alive?(pid)).to be(false) if pid end end + + it "waits for an Xvfb that is slow to start before launching a headful browser" do + Dir.mktmpdir do |dir| + slow_xvfb = File.join(dir, "Xvfb") + File.write(slow_xvfb, "#!/bin/sh\nsleep 1\nexec #{Ferrum::Browser::Binary.find('Xvfb')} \"$@\"\n", perm: 0o755) + allow(Ferrum::Browser::Binary).to receive(:find).and_call_original + allow(Ferrum::Browser::Binary).to receive(:find).with("Xvfb").and_return(slow_xvfb) + + browser = Ferrum::Browser.new(xvfb: true, headless: false) + browser.go_to(base_url) + + expect(browser.body).to include("Hello world!") + ensure + browser&.quit + end + end end def process_alive?(pid)