----- Mail original ----- > De: "Attila Szegedi" <szege...@gmail.com> > À: "Viktor Remennik" <bbik...@gmail.com> > Cc: "nashorn-dev" <nashorn-dev@openjdk.java.net> > Envoyé: Samedi 27 Février 2021 18:07:55 > Objet: Re: Terminate running script thread
Hi, > No, there’s no support for that. Nashorn compiles all scripts to JVM bytecode > and runs them as such. There’s no more of a way to stop that code than any > Java > code. Even if there was some other way, I don’t see how would stopping a > thread > be materially better than what Thread#stop provides. I’m not saying there’s no > better way, I’m just saying I don’t see it. I’m open to suggestion on how > could > a different mechanism be better. > > I can imagine that if someone wanted something like this, they could > implement a > ClassFileTransformer and transform the bytecode to maybe add a check for some > variable on the back edges of all loops? That’d help with infinite loops in > Java code invoked from scripts as well. Of course, if the variable isn’t > global, then you’d need to pass it around, or retrieve it from a thread local, > which could be expensive (you could add a local variable for every loop to > count and only check the thread local every 1000 iterations?) And again, how > would the behavior of acting on the “stop” flag be different than a stack > unwind through a ThreadDeath? > > Lots of questions, but I’m happy to listen and discuss. Loom has a mechanism to preempt a coroutine https://download.java.net/java/early_access/loom/docs/api/java.base/java/lang/Continuation.html#tryPreempt(java.lang.Thread) It uses the safepoint mechanism already present in the VM. So if you run on a pool of virtual threads and ask gently, it may work. > > Attila. Rémi > > >> On 2021. Feb 27., at 14:53, Viktor Remennik <bbik...@gmail.com> wrote: >> >> Is there already a way to terminate a running script? I found some old >> discussions and articles regarding it, is there any progress these days? It >> would be great to have something like CompiledScript#stop or something like >> interrupt handler inside the Nashorn eval, because currently I have to use >> Thread#stop with all its disadvantages and problems. I have user defined >> scripts running in my application, users are technicians but still could >> make a >> mistakes, so I'd like to control the script execution time and kill scripts >> threads if configured maximum execution time limit has been exceeded. As >> Nashorn is standalone now, I suppose I, or maybe someone more experienced in >> Nashorn's internals, could try to implement this, but I'd like to check >> first, >> isn't it is already implemented. >> >> https://stackoverflow.com/questions/24855182/interrupt-java-thread-running-nashorn-script >> <https://stackoverflow.com/questions/24855182/interrupt-java-thread-running-nashorn-script> >> http://blog.getsandbox.com/2018/01/15/nashorn-interupt/ >> <http://blog.getsandbox.com/2018/01/15/nashorn-interupt/> >> >> >> Thank you >> > > Viktor