-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using IO or rescuing exceptions when setting a timeout #302
Comments
Interesting 🤔 Just to be clear: The error with the timeout is to be expected, as it is in milliseconds. I got curious and removed the IO-related part and using just # frozen_string_literal: true
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "mini_racer", "0.12.0"
end
require "libv8-node"
puts "RUBY_VERSION : #{RUBY_VERSION}"
puts "RUBY_PLATFORM: #{RUBY_PLATFORM}"
puts "MiniRacer::VERSION: #{MiniRacer::VERSION}"
puts "Libv8::Node::LIBV8_VERSION: #{Libv8::Node::LIBV8_VERSION}"
puts "=" * 80
def sleep_rescue(duration_ms)
sleep duration_ms.to_f / 1000
:result_a
rescue MiniRacer::ScriptTerminatedError => e
puts "attached: TIMEOUT! #{e.class} - #{e.message}"
:timeout
rescue => e
puts "attached: ERROR: #{e.class} - #{e.message}"
end
def sleep_no_rescue(duration_ms)
sleep duration_ms.to_f / 1000
:result_b
end
js_context = MiniRacer::Context.new(timeout: 10)
js_context.attach("sleep_rescue", method(:sleep_rescue))
js_context.attach("sleep_no_rescue", method(:sleep_no_rescue))
started = Time.now
puts "sleep_rescue:"
begin
result = js_context.call("sleep_rescue", 1000)
puts "RESULT: #{result}"
rescue => e
puts "ERROR: #{e.class} - #{e.message}"
end
puts "==> #{Time.now - started} sec"
puts
started = Time.now
puts "sleep_no_rescue:"
begin
result = js_context.call("sleep_no_rescue", 1000)
puts "RESULT: #{result}"
rescue => e
puts "ERROR: #{e.class} - #{e.message}"
end
puts "==> #{Time.now - started} sec" resulting in this printed:
|
Thank you for such a detailed answer ! I had completely missed the fact that the timeout was in ms. I am very curious as to why the rescue would also impact such a delay, exceptions are often costly, but I wouldn't have imagined to that extent 🤔 |
I have not figured out where the time is spent exactly, but it is not in the Ruby method (added some crude debug statements in mini_racer directly). I'm suspecting this is happening in C-land somewhere, but that's out of reach for me to understand further. That might be something @SamSaffron or @lloeki might be able to help better. IMO the huge delay is a bug. |
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 :
The code above ends up outputting:
If I remove the
rescue
from thedownload_base64
ruby method, the output turns into :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 thedef 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.The text was updated successfully, but these errors were encountered: