Comments (27)
@andrewtch — I'll take a stab at it later this week. But, feel free to try as well.
from v8js.
Digging in, I could not find any time limits for v8 engine itself, so maybe it's not possible at all.
from v8js.
@andrewtch you can use PHP's set_time_limit to achieve the same result. It's not the most elegant solution but it does work.
If you need to do any post-timeout clean-up then take a look at register_shutdown_function, which does get triggered after a timeout.
Here's an example:
function shutdown()
{
print('Shutting down' . PHP_EOL);
}
register_shutdown_function('shutdown');
set_time_limit(1);
$code = <<<EOT
var text = "testing";
for (var i = 0; i < 10000000; ++i) {
var encoded = encodeURI(text);
}
EOT;
$v8Js = new V8Js();
$v8Js->executeString($code);
from v8js.
I'm aware of that. There's no goal to end PHP script after some timeout. The goal is to terminate v8 script and continue PHP script.
from v8js.
That's exactly what it does. Give it a try.
from v8js.
@beest , no, it isn't.
Assume the following:
foreach ($objects as $object) {
foreach ($objects->getCallbacks() as $callback) {
try {
$v8->executeString($callback->getCode());
} catch (V8TimeoutException $e) {
$this->log('callback timeout, callback id: '.$callback->getId());
}
}
}
and this is what I actually want.
I do not want to stop the script. I must have all objects and callbacks processed. With your approach I would need to process rest of the objects and callbacks in shutdown function, and what if another script terminates there, do some nesting woodoo with shutdown function?
from v8js.
There's nothing available natively in V8 or PHP. You'll need to look at thread synchronization to achieve this.
I'm sure I could put something together to do this, and sounds like @preillyme is going to take a look too. It would indeed be a useful feature.
from v8js.
Yep, as I stated above there are no limits in v8 itself. This week I'll try to play with gearman and see how it works with v8, and I'll be much appreciated if you can look on this issue from PHP / V8 side.
from v8js.
Half-a-tangent...
We need similar functionality (i.e Executing a JS script on a backend server) and have looked at various options including spidermonkey and v8js. The challenge we always run into is the timeout issue. Our scripts will be written by external clients and as such we will not be able to vet them to ensure they are well written and do not consume excessive resources or take long to execute.
With this in mind, we are thinking of the following alternative solution:
Implement a NodeJs server using DNode RPC (https://github.com/substack/dnode).
The JS code is then called via a socket and timeouts can be implemented on both the Php & JS server side.
It is obviously more complex to implement but based on the timeout issue inherent with the built-in Php js engines I am not sure how else this could be effectively done.
So, question. Is this overkill, does anyone have an opinion on whether it would work and finally is there perhaps something we have missed with the built-in option that would enable to not go this alternate route?
from v8js.
Please take a look on this implementation: http://www.mail-archive.com/[email protected]/msg04249.html
from v8js.
I've got an implementation of timeouts working in my fork: https://github.com/beest/v8js/tree/timeout
It throws a V8JsTimeoutException when a script exceeds a timeout passed in via executeString.
from v8js.
maybe it would be better instead of using pthread
, which is a big bottleneck in PHP, to use v8::internal::Thread as proposed here: http://pastebin.com/M801zbDB . This would go to native v8 threads instead of messing with phtread library.
from v8js.
The v8::internal::Thread class is not in the standard version of the V8 library. The only place I can see it is here: http://blog.peschla.net/doxygen/v8_chromium_r157275/classv8_1_1internal_1_1_thread.html which seems to be a specific implementation of V8 embedded in the Chromium browser. If you have any more information then I'd happily take a look.
The use of pthreads shouldn't inherently be a problem. Note how the sample code you provided links to the pthread library anyway "-lpthread".
However, support for Windows is only provided via a pthreads port. When this version is completed I will look at how to achieve Windows support.
from v8js.
I take that back - I can see the Thread class defined in the V8 code base. I'll dig a little deeper and see what I can do with it.
Note that this code also uses pthreads throughout for Linux and OSX. What are the PHP bottlenecks that you're aware of?
from v8js.
At old days when I tried to hack PHP code (~5.1) the resources were the problem. For example, if you pass mysql_connect or fopen result to thread (assuming this is a shared connection / file handle), concurrent usage of this resource can/will segfault.
I suppose that the best practice would be to keep the code as is if there's no timeout, and do some woodoo if timeout is requested.
from v8js.
Btw, the example above http://pastebin.com/M801zbDB also protects from infinite loops.
from v8js.
FYI I've implemented both time and memory limiting in my fork at https://github.com/beest/v8js. It protects from any long-running or memory-intensive code, whether infinite loop or otherwise.
@andrewtch the example you provided was useful and this version works in a very similar way.
@preillyme take a look at the API additions and see if you'd be interested to include this functionality in the official extension.
@MarkOfSine this will give you exactly the functionality that you need.
from v8js.
@beest, which branch?
from v8js.
@andrewtch master
from v8js.
Looks good :)
from v8js.
@preillyme , ping!
from v8js.
@beest — Can you send a PR.
from v8js.
@beest — ping, can you submit a pull request with your changes.
from v8js.
I'll get this done this week. Couple of tweaks required. Sorry for the delay!
from v8js.
@beest — any luck?
from v8js.
Finally got this done. Hope it works out for you, please do give it a try and provide some feedback.
from v8js.
@beest — Okay I'll take a look. Thanks for all of your hard work.
from v8js.
Related Issues (20)
- Trying to compile with php8 branch on ubuntu 22, getting error: could not determine libv8 version (v8 compiled succesfully) HOT 3
- Dynamic Properties deprecation in PHP 8.2 HOT 4
- Broken build on PHP 8.0.x HOT 2
- Segmentation fault - possibly another GC issue? HOT 7
- Compromise between FORCE_ARRAY and standard setup HOT 8
- sandbox error with v8 11.1.277.13 HOT 1
- libv8 must be version 9.0 or greater (using dockerfile) HOT 1
- Issue running make in ubuntu 22.04 HOT 2
- windows php 8.1
- compile error with g++ 13: error: expected unqualified-id before '&' token
- With wich version of V8 this php library work ? HOT 4
- Error installing on debian 11
- This LIB is very hard to install ! HOT 14
- PROHIBITS_OVERWRITING removed from V8 and causing V8Js compile error. HOT 1
- V8Js 3.0.0 HOT 1
- Is the latest recommended release 2.1.1 or 2.1.2? HOT 4
- Do I have permission to refactor this? HOT 3
- Compilation error: "no matching function for call to 'v8::ObjectTemplate::SetAccessor" HOT 6
- Linux arm64 support? HOT 3
- Is there an official installation tutorial for the v8js extension? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v8js.