1

Trying to run script (using Roslyn Scripting API):

var source = "while(true) { }";
return CSharpScript.EvaluateAsync(source).Result;

It will run forever. How to interrupt it?

I don't mind using other script engine, though asking for one is off-topic. I've found similar questions (example), but they are not helpful.

Background: I want to add support for C# user-written scripts to my software. I want to run scripts in sandbox, so ability to interrupt scripts is vital. In past I had some experience with CodeDOM, but I fail to find a way to interrupt scripts there too.

Thoughts:

  • I could pre-process user-written text and insert my code (checks for CancellationToken and return/throw) after each line. But above example is immune to such technique and I'd then must go complex route of syntax parser (don't have experience with that, thoughts?).
  • I could provide a method, which user have to explicitly call in his script problematic places (e.g. inside loops), but user may simply decide to ignore that.
  • Is there a way to run script step-by-step? Sandbox-debugger mode? Then I'd be able to check external request to terminate script and stop debugging.

What are the other possibilities? Has anyone implemented safe user-written scripting yet?

3
  • What about this? Commented Apr 10, 2019 at 12:35
  • @MindSwipe, I've seen this answer and ignored it (it has 2 downvotes). But.. it seems to work! It looks like internally EvaluateAsync method is already doing what I thought I have to do: it perform in parallel the cancellation check and somehow interrupts the script. Commented Apr 10, 2019 at 13:10
  • Hmm.. I did some testing and it doesn't interrupt always. It works only if Cancel() of token called before script is compiled (takes some time, especially for the first run). It does not interrupt the running script. Commented Apr 10, 2019 at 13:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.