5

Please read carefully I know it's not a good practice etc. Let's skip it.

Question: I have to compile and run some code, i.e. compile input code (input code it's a string) and execute a method. This code could have issues like:

  • while without an exit
  • infinitive recursive calls

I wanna use .net core, but .net core doesn't support thread abort. I definitely don't wanna run code in a separate process.

Any ideas about how to stop executing code without thread abort?

GSerjo
  • 4,725
  • 1
  • 36
  • 55
  • https://github.com/filipw/dotnet-script – Daniel A. White Feb 11 '19 at 02:46
  • 1
    Try this: https://stackoverflow.com/a/39247629/324260 (https://github.com/dotnet/coreclr/blob/master/tests/src/baseservices/threading/regressions/threadex.cs). – Ilian Feb 11 '19 at 03:05
  • 1
    Thanks, yeah, I saw it, but this code has been removed :( – GSerjo Feb 11 '19 at 03:13
  • I've tried `ThreadEx` it doesn't work. it throws `"Failed to get Thread.Abort method"` – GSerjo Feb 11 '19 at 03:14
  • 2
    Yeah. Just tested it too. It looks like they completely removed it. I think you're out of luck. It looks like they won't be supporting it in the near future (https://github.com/dotnet/coreclr/issues/20705). – Ilian Feb 11 '19 at 03:22
  • Yeah, and I have no idea how to stop/abort unstoppable code :) I can use mono or java, but.. :) – GSerjo Feb 11 '19 at 03:26
  • I have an ill-conceived theory, that you could search the code for unescaped semicolons and insert a call to a static method ThrowIfCancelled immediately after each one, and you might be able to trigger an exception when you set a static flag to true. (Well, for loops kill this, but yeah... it was ill-conceived) – MineR Feb 11 '19 at 03:36
  • Yeah, but it's so difficult to find all cases. it's a `string` so a lot depends on code formatting. for instance `while(true){..}` or the same on several lines.. oh.. – GSerjo Feb 11 '19 at 03:43
  • If you do decide to inject some code, this may help https://github.com/dotnet/roslyn/wiki/How-To-Write-a-C%23-Analyzer-and-Code-Fix – MineR Feb 11 '19 at 03:44
  • Would running that code in Docker container work? That would give you much better sandboxing and ability to terminate... (There really no point in running hostile code in your own process as SO due to "infinite recursive calls" will tear your process down) – Alexei Levenkov Feb 11 '19 at 04:51
  • yes, it could be done with docker, but... – GSerjo Feb 11 '19 at 05:03

0 Answers0