We currently have no way to interrupt Groovy script evaluation, so a script as simple as this:
while (1) {}
Can shoot CPU usage to 100% and can only be stopped by restarting Tomcat (stopping the Groovy module doesn't stop the script nor does restarting the OpenMRS web application within Tomcat).
I would propose that Groovy scripts be run in their own thread and that the Groovy Module administer those threads.
- As a first pass, we could terminate any Groovy script after 30 seconds. This would be a crude cutoff, but would suffice for 80% or better use cases.
- A little better would be to increase the auto-termination to 1-5 minutes and provide a cancel/interrupt button on the UI that aborts the script.
- The ideal solution would be to create a new admin page for the Groovy Module that lists all running Groovy script processes on the server, allowing any of them to be killed.
Having Groovy scripts evaluated in a separate thread could facilitate streaming output to the client (#1428) and/or reporting on script progress in the UI (#1493).