Skip to content

Using IO or rescuing exceptions when setting a timeout #302

Open
@dvkch

Description

@dvkch

Hello everyone :)

I've ran into a curious issue when setting a timeout at context creation. The underlying timeout detection doesn't seem to allow for rescue exceptions in attached methods, nor using IO.

Here is an example :

def download_base64(url)
      data = URI.open(url)
      data = Base64.encode64(data&.read || "")
      { data: data }
rescue => e
      { error: e.to_s }
end

js_context = MiniRacer::Context.new(timeout: 10)
js_context.attach('download64', proc { |url|
        download_base64(url)
})

begin
  result = js_context.call('download64', 'https://google.com/favicon.ico')
  puts "RESULT: #{result}"
rescue => e
  puts "ERROR: #{e}"
end

The code above ends up outputting:

ERROR: JavaScript was terminated (either by timeout or explicitly)

If I remove the rescue from the download_base64 ruby method, the output turns into :

ERROR: Terminated during callback

I have traced the error above to stop_attached inside MiniRacer.

If I remove the timeout, the result is a base64 encoded string of the Google's favicon.

I have disabled the timeout for now but would be very interesting to understand what goes on behind the scene. I have tried to go into the def timeout(&blk) method, and I am not sure why the current code there doesn't work, it doesn't seem to do anything you wouldn't expect it do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions