TerminateExecution() does not send a kill signal to the process/thread or
anything like that. It sets a flag that requests termination of currently
running generated code for JS functions. Execution in loops isn't
necessarily terminated immediately, several iterations of the loop may
still happen (this depends on the length of the loop). The idea is that you
can quickly gain control over long-running scripts ("Your browser tab is
hanging, would you like to stop it?"). V8 does not have, and in my opinion
does not need, a mechanism to interrupt v8::internal::OS::Sleep(), as this
is never called from JS (in vanilla V8).

The reason your alternative "DoSleep" implementation terminates much
quicker is because the C++ function finishes much sooner, returning control
to JS, which in turn is interrupted after a couple of loop iterations. I'm
willing to bet that if you reduce the sleeping interval from 500ms to, say,
1ms in the other implementation, you'll observe much quicker termination
there (and, conversely, if you wait long enough, the 500ms version will
terminate eventually).

Long story short: v8::TerminateExecution() is not designed to interrupt
v8::internal::OS::Sleep(), nor any other function or FunctionTemplate
implemented in C++. If you want to be able to have sleeping threads and
terminate them, you'll have to use either another sleep implementation, or
another way to request termination. (Where "use another" means "implement
your own".)


On Thu, Apr 4, 2013 at 10:16 AM, Laszlo Szakony <laszlo.szak...@certec.at>wrote:

> Hi,
>
> I'm using v8 - v8_13185_v3.15.11 - on Windows, Win7 64bit in
> multi-threaded environment: several scripts are running parallel.
> At an arbitrary time I want to terminate certain running scripts or maybe
> all from another thread. Some of the scripts may have infinite loops.
> Everything works fine, until some scripts call dosleep() in a
> FunctionTemplate defined as follows:
>
> (In thread script[n])
>
> v8::Handle<v8::Value> DoSleep(const v8::Arguments& args) {****
>
>   int millisec = args[0]->ToInt32()->Int32Value();****
>
>   if (millisec)****
>
>   {****
>
>     v8::Unlocker unlock;****
>
>     v8::internal::OS::Sleep(millisec);****
>
>   }****
>
>   return v8::Undefined();****
>
> }****
>
> …****
>
> v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();****
>
> global->Set(v8::String::New("dosleep"),
> v8::FunctionTemplate::New(DoSleep));****
>
> …****
>
> The JavaScript Code looks sg. like this:****
>
> v8::Script::Compile(v8::String::New("function f() { while(true)
> {dosleep(500);} } f()"))->Run();****
>
> ** **
>
> In the main thread I call:****
>
> v8::Locker locker;****
>
> for (int i = 0; i < kThreads; i++) {****
>
>   v8::V8::TerminateExecution(threads[i]->GetV8ThreadId());****
>
> ** **
>
> The threads calling dosleep() won`t terminate. If I change the DoSleep()
> Function() to:****
>
> v8::Handle<v8::Value> DoSleep(const v8::Arguments& args) {****
>
>   int millisec = args[0]->ToInt32()->Int32Value();****
>
>   if (millisec)****
>
>   {****
>
>     v8::Unlocker unlock;****
>
>     for (int i = count; i--;) for (int j = 1000; j--;) ;****
>
>   }****
>
>   return v8::Undefined();****
>
> }
>
>
> then the threads calling dosleep() will be terminated.****
>
> I have modified test-thread-termination.cc to demonstrate the behavior.
> After replacing test-thread-termination.cc with the attached one and
> compiling of ccttest the tests can be called by:****
>
> C:\v8_13185_v3.15.11\build\Release>cctest
> test-thread-termination/TerminateMultipleV8ThreadsWithDelayDefaultIsolate
> => OK****
>
> C:\v8_13185_v3.15.11\build\Release>cctest
> test-thread-termination/TerminateMultipleV8ThreadsWithSleepDefaultIsolate
> => Failed: no termination****
>
>
> Is this a bug? If not, how can I terminate scripts calling sleep()?
>
>
> Thank you,
>
> Laszlo
>
> --
> --
> v8-users mailing list
> v8-users@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to v8-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to