node-threads-a-gogo icon indicating copy to clipboard operation
node-threads-a-gogo copied to clipboard

Terminating a thread

Open artch opened this issue 10 years ago • 6 comments

I'm trying to figure out a way how a piece of Javascript code can be forced to terminate its execution. I've tried to use your library like this:

var t = require('threads_a_gogo').create();

var foo = function() {
    while(1) {};
};

t.eval('foo()');

setTimeout(function() {
    t.destroy();
}, 1000);

It doesn't work though. Method does not interrupt what is executing already. Is there a way to achieve this in Javascript?

I know that this task is not really common in node.js programming, but this is absolutely mandatory in my project's needs.

artch avatar May 19 '14 13:05 artch

The current implementation of destroy is only able to destroy the thread if the thread is idle, and your while(1) is preventing the thread from going idle (which will also prevent it from executing further eval calls. I think your work is better suited to node's child_process.

jnfeinstein avatar May 19 '14 15:05 jnfeinstein

child_process gets 10 Mb of memory for every new V8 instance which is quite an overhead for my simple tasks.

Any way to change or hack destroy implementation to terminate the thread in "forced mode"?

artch avatar May 19 '14 15:05 artch

I believe you can do:

function foo() {
  if (!kill) {
    DO_STUFF
    thread.nextTick(foo);
  }
}
thread.eval('kill=false').eval(foo).eval('foo()');

and then to kill:

thread.eval('kill=true');

The idea is to have the thread treat the loop as a queue of work, which will allow the parent thread to still eval additional work. I still don't think destroy will work, but it might.

jnfeinstein avatar May 19 '14 20:05 jnfeinstein

On 19/05/2014, at 22:22, jnfeinstein wrote:

Also looks like calling destroy(true) might work.

Nope, .destroy(true) won't work... sorry.

A while(1); can't be killed properly, because if TAGG attempted to kill the pthread forcefully from C (it doesn't) then the v8 engine wouldn't be given the oportunity to exit properly and cleanly. IOW, I tried and it made v8 crash.

I don't know, perhaps now there's a way to tell v8 to kill a while(1);, but last time I checked there wasn't.

xk avatar May 19 '14 22:05 xk

I was confusing destroy thread with destroy pool.

On Monday, May 19, 2014, xk [email protected] wrote:

On 19/05/2014, at 22:22, jnfeinstein wrote:

Also looks like calling destroy(true) might work.

Nope, .destroy(true) won't work... sorry.

A while(1); can't be killed properly, because if TAGG attempted to kill the pthread forcefully from C (it doesn't) then the v8 engine wouldn't be given the oportunity to exit properly and cleanly. IOW, I tried and it made v8 crash.

I don't know, perhaps now there's a way to tell v8 to kill a while(1);, but last time I checked there wasn't.

— Reply to this email directly or view it on GitHubhttps://github.com/xk/node-threads-a-gogo/issues/45#issuecomment-43566756 .

jnfeinstein avatar May 19 '14 22:05 jnfeinstein

I'm having the same issue where operations on large files should be terminated after a set timeout. Is there a solution to this problem?

It's very hard to include next.processTicks in these methods because they belong to external libraries.

StefanVanDyck avatar Jul 18 '14 13:07 StefanVanDyck