Detecting and ending infinite loops in JavaScript activities

Infinite loops can occur in JavaScript and Java code that runs inside IBM® Business Process Manager applications. You can configure loop detection parameters in the 100Custom.xml file to detect infinite loops and optionally ending them.

When infinite loops occur in JavaScript code that runs inside IBM BPM applications, this affects other resources. For example, Process Center or Process Server threads are lost until the server is stopped, which impacts server availability. To detect infinite loops, IBM BPM monitors the number of executed JavaScript instructions in each script activity. It sets an instruction threshold and a timeout to check the duration of the script activity. When the instruction threshold is reached, the JavaScript engine invokes a callback provided by IBM BPM that checks whether the script duration exceeded the configured timeout. If it exceeded the timeout, IBM BPM either throws a loop-detection-exception or logs an error message, depending on the configuration. The default value of the instruction threshold is 25 million JavaScript instructions.

Notes:
  • Calls to Java methods or IBM BPM JavaScript API calls, such as tw.system.executeServiceByName, count as one instruction.
  • JavaScript code within managed server-side files that are referenced by server-side components count as individual instructions, not just one instruction.

Using the following loop detection parameters, you can configure the timeout and the instruction threshold at which IBM BPM provides the callback. If the script duration exceeds the timeout, you can choose to stop the script.

  • loop-detection-duration: If the timeout value specified by the loop-detection-duration parameter is exceeded, it is assumed that the activity is in an endless loop. The default duration is set to 20 seconds, but it is configurable (see the following example).
  • loop-detection-exception: By default, when an endless loop is detected, the IBM BPM engine writes a warning to the SystemOut.log file, but the script activity continues. To configure the IBM BPM engine to stop infinitely looping script activities, set the loop-detection-exception parameter to true.
    If the loop-detection-exception parameter is set to false, one of the following messages is written to the SystemOut.log file:
    • CWLLG2261W: Infinite loop suspected after {0} seconds in ''UKNOWN'' activity. If this script is not in a loop, increase the loop-detection-duration property.
    • CWLLG2263W: Infinite loop suspected after {0} seconds in ''{1}'', for BPD ''{2}'', script activity ''{3}''. If this script is not in a loop, increase the loop-detection-duration property.
    • CWLLG2265W: Infinite loop suspected after {0} seconds in service ''{1}''. If this service is not in a loop, increase the loop-detection-duration property
    If the loop-detection-exception parameter is set to true, one of the following messages is written to the SystemOut.log file:
    • CWLLG2262E: Infinite loop detected after {0} seconds, ''UKNOWN'' activity terminated. If this script is not in a loop, increase the loop-detection-duration property.
    • CWLLG2264E: Infinite loop detected after {0} seconds in ''{1}'', for BPD ''{2}'', script activity ''{3}'' terminated. If this script is not in a loop, increase the loop-detection-duration property.
    • CWLLG2266E: Infinite loop detected after {0} seconds, service ''{1}'' terminated. If this service is not in a loop, increase the loop-detection-duration property.
  • instruction-threshold: When JavaScript code makes Java calls in a loop and most of the execution time is spent inside the Java code, detecting an infinite loop might take longer than what is optimal. The default threshold of 25 million JavaScript instructions could take much longer than the configured timeout and the script could remain uninterrupted for a long time. For example, a loop in a JAR file's code path prevents it from returning to the IBM BPM engine for processing, which prolongs the duration of the JavaScript activity. Using the instruction-threshold property, you can specify a lower number of JavaScript instructions between the calls to the callback for earlier loop detection.

    IBM BPM multiplies the configured value by 1000 before it instructs the JavaScript engine to call the loop-detection callback. The default value is 250. This means that the JavaScript engine calls the IBM BPM loop detection callback every 250000 JavaScript instructions.

The following example illustrates how to configure the JavaScript loop detection parameters in the 100Custom.xml file:
<common merge="mergeChildren">                                       
  <javascript-engine>                                                
    <loop-detection-duration merge="replace">90</loop-detection-duration>            
    <loop-detection-exception merge="replace">true</loop-detection-exception>
    <instruction-threshold merge="replace">250</instruction-threshold>        
  </javascript-engine>                                               
</common>