Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8297552

Nashorn script returning null value instead of primitive number

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      This happens from an automated Jenkins pipeline that runs e2e tests really quickly.
      The server that executes the Nashorn script is run on docker using java base image.
      Notes about the implementation:
      1. We store the scripts in the database - fetch them as they are needed
      2. Before running the scripts we go through all the binding providers (custom-made). Important to note that some of these bindings have the possibility to store values from requests (in case of multiple entries).
      3. Each script gets run separately with the same bindings ENGINE_SCOPE
      4. Using a script engine with optimistic types

      A DESCRIPTION OF THE PROBLEM :
      I have noticed Nashorn not returning the correct value whenever Jenkins runs my company's e2e test pipeline. This issue has not occurred locally, only when Jenkins runs it. (Could possibly be a racing condition?)

      The script that is causing the issues is calling a function inside the script. That function could either return null or return a numerical value. The function calls a Java-bound class that makes a request to a separate server and returns a numerical value from the results.

      The issue could also be that Nashorn is having trouble mapping variables and functions with the same name.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. The script gets run
      2. Script evaluates that I should go into the custom-made provider method
      3. Everything looks good so far
      4. The given method should do a request to another server and then store the result for later use
      5. From LightStep I see that the request was made
      6. After the request finishes and is stored in the cache (aka private field param)
      ...
      I do not know from which step it actually fails, I only see from my logs that the returned value should have been numeric but is instead null.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      7. This primitive long value is returned from Java Stream API where the values are filtered according to some of the method parameters.
      8. After the stream filtering is done .count() method should be called which should return a long value.
      ACTUAL -
      null is returned

      ---------- BEGIN SOURCE ----------
      var isTestAllowed = isTestAllowed();

      var result = {
          type: 'boolean',
          value: getValue(isTestAllowed),
          result: isTestAllowed,
          remark: isTestAllowed + '' + isTestAllowed()
      };

      function isTestAllowed() {
          return !(nameProvider.firstName.toUpperCase() == 'TEST')
      };

      function getValue(isTestAllowed) {
          if (!isTestAllowed) {
              // This makes a request to the other server and the filters and counts the values (returns a primitive long)
              return someProvide.someRequest(2, 4);
          } else {
              return null;
          }
      }

      JSON.stringify(result);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I have seen two workarounds for this:
      1. Whenever I make the javascript function getValue else statement not return null, then it works as expected.
      2. Make variable and function names more different (from the script you can see that isTestAllowed is used in a lot of places. Currently, I try to use this workaround.



      FREQUENCY : occasionally


        1. NameProvider.java
          0.1 kB
          Andrew Wang
        2. NashornTest.java
          2 kB
          Andrew Wang
        3. SomeProvider.java
          0.4 kB
          Andrew Wang

            attila Attila Szegedi
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: