pyv8
pyv8 copied to clipboard
Added timeout argument to limit javascript function call time
So recently I faced the problem that there's no way to limit function call time in any way.
For example, if you run this simple javascript code from python:
while(true);
You will end up with infinite loop indeed, with no way exit that from python code, except kill the whole process.
That patch affects only apply method of javascript function object so it now has such signature:
apply(this, args = [], kwargs = {}, timeout = 0)
And the code that can handle call 500ms timeout:
try:
some_function.apply(some_function, [], {}, 500L)
except JSTimeoutError:
raise RuntimeError("Function call timeout!")
Under the hood, it uses SIGALRM and v8::V8::TerminateExecution to kill the running code gracefully.
Why don't just use SIGALRM on python side? Python cannot catch SIGALRM while C code is executing.
Added also a timeout to context.eval:
try:
context.eval(text, "", -1, -1, None, 500L)
except JSTimeoutError:
raise RuntimeError("Evaluation timeout!")
The patch sounds good to me but would like to get a feedback from @flier. Moreover I highly suggest to send PR against this repo https://github.com/flier/pyv8. This repo is just a fork I created for Thug (https://github.com/buffer/thug) but it is not intended to be the official one. Thanks!
@buffer as you don't have issues here I'd like also to mention that there's a serious issues with threading here, static doesn't go well when you create object template in one thread (isolate) and then use it in another. See the fix.
Did not create issue at flier's repo as it's obviously dead.
Thanks for pointing it out. FYI I am currently contributing to this new project
https://github.com/tbodt/v8py
and I invite you to do the same. PyV8 can be considered a dead project because of the lack of any support from the original author. IMHO investing time and efforts on it is worthless at this point .
@buffer looks good, does it have threading support? Is it production ready?
The code base is already extremely good and it should be thread-safe. I am going to send some PRs to add some features I need for Thug in the next days. But that's just minor stuff.