Timeout parameter
The ClearScript.V8 engine needs a timeout parameter. I am running a service where I'll give control to the user by giving them programming access with a Javascript engine and I don't want things like infinite loops to happen so the need for a timeout.
For now I'll have to fall back on Jint until there is such a parameter in ClearScript.V8 engine.
Thanks.
@imsmart-tech ClearScript's Interrupt method allows you to abort script execution based on a timeout or any other criteria.
This sample class provides an extension method for executing script code with a timeout:
public static class ScriptEngineExtensions {
public static object TimedEvaluate(this ScriptEngine engine, TimeSpan timeout, string code) {
using (var timer = new Timer(state => engine.Interrupt())) {
try {
timer.Change(timeout, TimeSpan.FromMilliseconds(Timeout.Infinite));
return engine.Evaluate(code);
}
catch (ScriptInterruptedException exception) {
throw new TimeoutException("Script execution timed out", exception);
}
}
}
}
And here's how you might use it:
try {
engine.TimedEvaluate(TimeSpan.FromSeconds(5), "while (true) {}");
}
catch (TimeoutException) {
Console.WriteLine("Timed out!");
}
Would something like that work for you?
How do I set a timeout on the dynamic property Script (engine.Script.myFunc) ?
engine.Execute(@"function myFunc(x, y, z) {
return { xValue: x, yValue: y, zValue: z };
}");
var yValue = engine.Script.myFunc(1, 2.0, "three").yValue;
Thanks.
Hi @lchtlx,
When invoking a script function directly, you can add timeout support the same way. Consider:
public static class ScriptEngineExtensions {
public static object TimedInvoke(this ScriptEngine engine, TimeSpan timeout, Func<object> func) {
using (var timer = new Timer(state => engine.Interrupt())) {
try {
timer.Change(timeout, TimeSpan.FromMilliseconds(Timeout.Infinite));
return func();
}
catch (ScriptInterruptedException exception) {
throw new TimeoutException("Invocation timed out", exception);
}
}
}
}
And then:
var yValue = engine.TimedInvoke(TimeSpan.FromSeconds(5), () => engine.Script.myFunc(1, 2.0, "three").yValue);
Good luck!
Hi @lchtlx,
When invoking a script function directly, you can add timeout support the same way. Consider:
public static class ScriptEngineExtensions { public static object TimedInvoke(this ScriptEngine engine, TimeSpan timeout, Func<object> func) { using (var timer = new Timer(state => engine.Interrupt())) { try { timer.Change(timeout, TimeSpan.FromMilliseconds(Timeout.Infinite)); return func(); } catch (ScriptInterruptedException exception) { throw new TimeoutException("Invocation timed out", exception); } } } }And then:
var yValue = engine.TimedInvoke(TimeSpan.FromSeconds(5), () => engine.Script.myFunc(1, 2.0, "three").yValue);Good luck!
It works great, thank you!
Dear ClearScript team,
I'm using version 6.0.2 via the Microsoft.ClearScript nuget package. When I run the following code, I find that the VBScriptEngine throws an exception after 10 seconds instead of 3 seconds. Can the engine return immediately after executing Interrupt? Thank you.
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var engine = new Microsoft.ClearScript.Windows.VBScriptEngine())
{
using (var timer = new System.Threading.Timer(state => engine.Interrupt()))
{
try
{
timer.Change(TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(Timeout.Infinite));
engine.AddHostType("Thread", typeof(Thread));
engine.Execute(@"Function TestTimeOut()
Thread.Sleep(10000)
End Function");
engine.Script.TestTimeOut();
}
catch (ScriptInterruptedException exception)
{
TimeSpan elapsedTime = stopwatch.Elapsed;
}
}
}
Hi @lchtlx,
ScriptEngine.Interrupt interrupts script execution by setting a flag that the engine checks at its discretion. When the script invokes a host method, the engine surrenders control until the method returns.
Instead of Thread.Sleep, consider exposing a sleep method that you can interrupt on the host side. Here's a simple example:
public class Threading {
internal CancellationTokenSource CancelSource;
public void Sleep(int timeout) {
using (CancelSource = new CancellationTokenSource()) {
var task = Task.Delay(timeout, CancelSource.Token);
try {
task.Wait();
}
catch when (task.IsCanceled) {
}
finally {
CancelSource = null;
}
}
}
}
And then:
var stopwatch = Stopwatch.StartNew();
using (var engine = new VBScriptEngine()) {
var threading = new Threading();
TimerCallback callback = state => {
engine.Interrupt();
threading.CancelSource?.Cancel();
};
using (var timer = new Timer(callback)) {
try {
timer.Change(TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(Timeout.Infinite));
engine.AddHostObject("Thread", threading);
engine.Execute(@"
Function TestTimeOut()
Thread.Sleep(10000)
End Function");
engine.Script.TestTimeOut();
}
catch (ScriptInterruptedException) {
Console.WriteLine(stopwatch.Elapsed);
}
}
}
Good luck!