Skip to content
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

Terminating a thread #45

Open
artch opened this issue May 19, 2014 · 6 comments
Open

Terminating a thread #45

artch opened this issue May 19, 2014 · 6 comments

Comments

@artch
Copy link

artch commented May 19, 2014

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.

@jnfeinstein
Copy link

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.

@artch
Copy link
Author

artch commented May 19, 2014

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"?

@jnfeinstein
Copy link

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.

@xk
Copy link
Owner

xk commented May 19, 2014

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.

@jnfeinstein
Copy link

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//issues/45#issuecomment-43566756
.

@StefanVanDyck
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants