Skip to content

Commit

Permalink
Merge pull request #32 from YusukeIwaki/revert_workaround_for_send_recv
Browse files Browse the repository at this point in the history
Revert deletion of workaround for unordered SEND/RECV
  • Loading branch information
YusukeIwaki authored Nov 27, 2020
2 parents 2ed3b25 + ad50b77 commit 397c909
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/puppeteer/cdp_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(connection, target_type, session_id)
@connection = connection
@target_type = target_type
@session_id = session_id
@pending_messages = {}
end

attr_reader :connection
Expand All @@ -34,7 +35,12 @@ def async_send_message(method, params = {})
id = @connection.raw_send(message: { sessionId: @session_id, method: method, params: params })
promise = resolvable_future
callback = Puppeteer::Connection::MessageCallback.new(method: method, promise: promise)
@callbacks[id] = callback
if pending_message = @pending_messages.delete(id)
debug_puts "Pending message (id: #{id}) is handled"
callback_with_message(callback, pending_message)
else
@callbacks[id] = callback
end
promise
end

Expand All @@ -44,7 +50,19 @@ def handle_message(message)
if callback = @callbacks.delete(message['id'])
callback_with_message(callback, message)
else
raise Error.new("unknown id: #{id}")
debug_puts "unknown id: #{id}. Store it into pending message"

# RECV is sometimes notified before SEND.
# Something is wrong (thread-unsafe)...
# As a Workaround,
# wait about 10 frames before throwing an error.
message_id = message['id']
@pending_messages[message_id] = message
Concurrent::Promises.schedule(0.16, message_id) do |id|
if @pending_messages.delete(id)
raise Error.new("unknown id: #{id}")
end
end
end
else
emit_event(message['method'], message['params'])
Expand Down

0 comments on commit 397c909

Please sign in to comment.