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 d7e31c1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
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
3 changes: 2 additions & 1 deletion 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 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 d7e31c1

Please sign in to comment.