开发者

Java - how to stop a thread running arbitrary code?

开发者 https://www.devze.com 2023-02-15 10:41 出处:网络
In my application which runs user submitted code[1] in separate threads, there might be some cases where the code might take very long to run or it might even开发者_如何学Python have an infinite loop!

In my application which runs user submitted code[1] in separate threads, there might be some cases where the code might take very long to run or it might even开发者_如何学Python have an infinite loop! In that case how do I stop that particular thread?

I'm not in control of the user code, so I cannot check for Thread.interrupted() from the inside. Nor can I use Thread.stop() carelessly. I also cannot put those code in separate processes.

So, is there anyway to handle this situation?

[1] I'm using JRuby, and the user code is in ruby.


With the constraints you've provided:

  1. User submitted code you have no control over.
  2. Cannot force checks for Thread.interrupted().
  3. Cannot use Thread.stop().
  4. Cannot put the user code in a process jail.

The answer to your question is "no, there is no way of handling this situation". You've pretty much systematically designed things so that you have zero control over untrusted third-party code. This is ... a suboptimal design.

If you want to be able to handle anything, you're going to have to relax one (or preferably more!) of the above constraints.


Edited to add:

There might be a way around this for you without forcing your clients to change code if that is a(nother) constraint. Launch the Ruby code in another process and use some form of IPC mechanism to do interaction with your main code base. To avoid forcing the Ruby code to suddenly have to be coded to use explicit IPC, drop in a set of proxy objects for your API that do the IPC behind the scenes which themselves call proxy objects in your own server. That way your client code is given the illusion of working inside your server while you jail that code in its own process (which you can ultimately kill -9 as the ultimate sanction should it come to that).

Later you're going to want to wean your clients from the illusion since IPC and native calls are very different and hiding that behind a proxy can be evil, but it's a stopgap you can use while you deprecate APIs and move your clients over to the new APIs.


I'm not sure about the Ruby angle (or of the threading angle) of things here, but if you're running user-submitted code, you had best run it in a separate process rather than in a separate thread of the same process.

Rule number one: Never trust user input. Much less if the input is code!

Cheers


Usually you have a variable to indicate to stop a thread. Some other thread then would set this variable to true. Finally you periodically check, whether the variable is set or not.

But given that you can't change user code , I am afraid there isn't a safe way of doing it.


For Running Thread Thread.Interrupt wont actually stop as sfussenegger mentioned aforth (thanks sfussenegger recollected after reading spec).

using a shared variable to signal that it should stop what it is doing. The thread should check the variable periodically,(ex : use a while loop ) and exit in an orderly manner.

private boolean isExit= false;

public void beforeExit() {
    isExit= true;
}

public void run() {
    while (!isExit) {

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号