Re: [therubyracer] Timeout for long running eval's

118 views
Skip to first unread message

Charles Lowell

unread,
Mar 1, 2013, 10:54:01 AM3/1/13
to therub...@googlegroups.com
jbr,

This has been on the todo list for awhile, but it is not something that is officially supported. 

The primitives should all be there however all the primitives are there in place. 

You have 

V8::C::Locker::StartPreemption()
V8::C::V8::GetCurrentThreadId()
V8::C::V8::TerminateExecution(thread_id)

You would need to start preemption, and then pass the thread Id back to your control thread, and then terminate it after a certain period.

cheers,
Charles


On Mar 1, 2013, at 4:31 AM, jbr wrote:

Hey guys,

I've been trying to timeout infinite loops for the past days and can't find a way around this problem.

The last post concerning this topic is almost 2 years old and got me nowhere,
so i was wondering if there is any new way to approach this problem?

I had a good look at the specs, but as they are pending, I'm really at wit's end.

Any help would be appreciated.

– jbr

--
You received this message because you are subscribed to the Google Groups "The Ruby Racer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to therubyracer...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Charles Lowell 
thefrontside.net | twitter: @cowboyd | github: cowboyd




jbr

unread,
Mar 1, 2013, 5:29:54 PM3/1/13
to therub...@googlegroups.com
Hey Charles,

thanks for the quick reply!

So the approach you're suggesting is exactly the one in the spec?

Because the following snippet throws a Segmentation fault at me when calling TherminateExecution(thread_id)

begin
      V8::C::Locker::StartPreemption(2)
      thread_id = nil
      Thread.new do
        loop until thread_id
        puts "thread id: #{thread_id}"
        V8::C::V8::TerminateExecution(thread_id)
      end
      Thread.new do
        V8::C::Locker() do
          thread_id = V8::C::V8::GetCurrentThreadId()
          V8::Context.new {|cxt| cxt.eval('1+1')}
        end
      end
      # V8::C::V8::TerminateExecution(thread_id)
    ensure
      V8::C::Locker::StopPreemption()
    end

This happens on Ruby 1.9.3 with Rails 3.1.2 and Ruby 1.9.3 with Rails 4.0.0.beta

–jbr

Charles Lowell

unread,
Mar 1, 2013, 9:54:25 PM3/1/13
to therub...@googlegroups.com
jbr,

I believe that you need to have the VM locked from the thread that is doing the termination as well.

cheers,
Charles

jbr

unread,
Mar 2, 2013, 7:34:44 AM3/2/13
to therub...@googlegroups.com
Hey Charles,

that was spot on, but I have the feeling the StartPreemption simply has no effect at all.

I tried a lot of different approaches this morning and read on the V8 boards, but still no luck.

Could you please provide me with a really simple snippet which actually terminates the evaluation?

Here is the follow up to your last reply with the output:

    V8::C::Locker::StartPreemption(2)
    @result = nil
    @thread_id = nil
   
    Thread.new do
      loop until @thread_id
      logger.info "thread id: #{@thread_id}"
      V8::C::Locker() do
        V8::C::V8::TerminateExecution(@thread_id)
      end

    end
    Thread.new do
      V8::C::Locker() do
        @thread_id = V8::C::V8::GetCurrentThreadId()
        V8::Context.new {|cxt| @result = cxt.eval('for(var i=0;i<999999999;i++) { 1 + 1 }')}
      end
    end   
    sleep 1
    render :text => "Result: #{@result}" 

CONSOLE OUTPUT

Started GET "/test" for 127.0.0.1 at 2013-03-02 13:28:03 +0100
Processing by TestController#test as HTML
thread id: 5
  Rendered text template (0.0ms)
Completed 200 OK in 4877ms (Views: 1.5ms)

This shouldn't take 4.8 seconds, as I'd like to immediately terminate the evaluation.

Your help is much appreciated!

– jbr

Jake Hoffner

unread,
Jul 16, 2013, 9:46:06 PM7/16/13
to therub...@googlegroups.com
jbr, 

Any luck with this? I am dealing with the same issue. 

Thanks
- Jake

Mark Brocato

unread,
Feb 2, 2014, 3:08:32 PM2/2/14
to therub...@googlegroups.com
Based on the docs it looks like timeouts should be supported by doing something like ctx = V8::Context.new(:timeout => 10), but my tests with long running scripts never get terminated.  Does anyone know what the state of this is?  I am running ruby 2.0 on mac os x mavericks.

Thanks!

Charles Lowell

unread,
Feb 3, 2014, 5:20:29 PM2/3/14
to therub...@googlegroups.com
Mark,

This functionality was only available on master. However, I have released a new version that includes it. Please update your gem version to 0.12.1 and try again. Thanks.

--
Reply all
Reply to author
Forward
0 new messages