From: Charles O Nutter Date: 2006-08-16T04:05:20+09:00 Subject: Re: sandbox timers & block scopes ------=_Part_124705_4083129.1155668710521 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 8/15/06, why the lucky stiff wrote: > > On Wed, Aug 16, 2006 at 02:46:30AM +0900, MenTaLguY wrote: > > Having a watchdog thread should require the least hackery -- maybe the > > only question at that point is whether it's easy for a separate thread > > to cleanly kill a rogue sandbox the way you want it to? > > Yeah, that's just it. I don't want to kill the whole thread containing > the > sandbox. Just stop the eval. And if I can avoid using an actual Ruby > thread to > monitor the sandbox, I would like that! Caveat: I'm no Ruby C expert, but I've studied the threading code pretty closely to better ape it in JRuby. I think it should not be difficult for you to tie into the already-existing thread-context-switching code in Ruby. As I understand it, every 10ms a signal is fired that triggers a variable to be set which will, on appropriate execution boundaries, cause a thread context switch to occur. It's mostly a cooperative switching, since the code in question must encounter one of several nodes that do this check, but it ought to be a good place for you to hook in your timer and forcibly switch sandboxes. Bringing up my Ruby 1.8 source, I believe the macro you're looking for is: # define CHECK_INTS do {\ if (!(rb_prohibit_interrupt || rb_thread_critical)) {\ if (rb_thread_pending) rb_thread_schedule();\ if (rb_trap_pending) rb_trap_exec();\ }\ } while (0) When the 10ms signal fires, rb_thread_pending is set to 1, causing this macro (called at various places in the interpreter cycle) to invoke the thread scheduler. It shouldn't be hard to add in your sandbox timeout/sandbox switch code the same way. The only caveat I can see is that the 10ms signal is only started up once there's more than a single green thread. You'd probably need to forcibly start it once another sandbox is initialized. -- Contribute to RubySpec! @ www.headius.com/rubyspec Charles Oliver Nutter @ headius.blogspot.com Ruby User @ ruby.mn JRuby Developer @ www.jruby.org Application Architect @ www.ventera.com ------=_Part_124705_4083129.1155668710521--