Skip to content

Commit

Permalink
fix: Close connections
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Jan 4, 2024
1 parent 20e0b91 commit 076e872
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- Resizing page on creation is gone and moved to Cuprite [#427]
- Min Ruby version is 2.7
- Refactored internal API of `Ferrum::Browser`, `Ferrum::Page`, `Ferrum::Context`, `Ferrum::Contexts`, `Ferrum::Target`
instead of passing browser and making cyclic dependency on the browser instance, we pass now a simple client [#431]
instead of passing browser and making cyclic dependency on the browser instance, we pass now a thin client [#431]
- Bump `websocket-driver` to `~> 0.7` [#432]
- Got rid of `Concurrent::Async` in `Ferrum::Browser::Subscriber` [#432]

Expand Down
1 change: 1 addition & 0 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def restart
def quit
return unless @client

contexts.close_connections
@client.close
@process.stop
@client = @process = @contexts = nil
Expand Down
5 changes: 3 additions & 2 deletions lib/ferrum/browser/web_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def on_message(event)

def on_close(_event)
@messages.close
@sock.close
@thread.kill
end

Expand All @@ -65,7 +66,7 @@ def send_message(data)

def write(data)
@sock.write(data)
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, IOError # rubocop:disable Lint/ShadowedException
@messages.close
end

Expand All @@ -83,7 +84,7 @@ def start

@driver.parse(data)
end
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, IOError # rubocop:disable Lint/ShadowedException
@messages.close
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/ferrum/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def delete_target(target_id)
@targets.delete(target_id)
end

def close_targets_connection
@targets.each_value do |target|
next unless target.attached?

target.page.close_connection
end
end

def dispose
@contexts.dispose(@id)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/ferrum/contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ def create(**options)

def dispose(context_id)
context = @contexts[context_id]
context.close_targets_connection
@client.command("Target.disposeBrowserContext", browserContextId: context.id)
@contexts.delete(context_id)
true
end

def close_connections
@contexts.each_value(&:close_targets_connection)
end

def reset
@default_context = nil
@contexts.each_key { |id| dispose(id) }
Expand Down
8 changes: 7 additions & 1 deletion lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ def go_to(url = nil)
def close
@headers.clear
client(browser: true).command("Target.closeTarget", targetId: @target_id)
@page_client.close
close_connection

true
end

def close_connection
@page_client&.close
end

#
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
end

config.after(:all) do
@browser&.quit
@browser.quit
end

config.before(:each) do
Expand Down

0 comments on commit 076e872

Please sign in to comment.