Nashorn URLReader does not close stream properly

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      I'm using jdk.nashorn.api.scripting.URLReader with Nashorn script engine to evaluate a script from a file (passing the file URL to the reader). After the evaluation is finished (and the reader closed explicitly) often the script file stays locked.

      I have attached a test program to reproduce the bug, which creates a test script file, evaluates it and attempts to delete it after that. The deletion fails quite often (i.e. on the third/fourth test), but not always, which makes me believe a synchronization issue is at hand.

      The attached program runs 10 such tests and reliably fails on every run.

      Note that if the reader at line 33:

      Reader reader = new URLReader(scriptURL);

      is replaced with

      Reader reader = new InputStreamReader(scriptURL.openStream());

      the program finishes without error.


      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.Reader;
      import java.net.URL;

      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;
      import javax.script.ScriptException;

      import jdk.nashorn.api.scripting.URLReader;

      public class Main {

        private static final File SCRIPT_FILE = new File("test.js");
        private static final String SCRIPT = "print('test')";

        public static void main(String[] args) throws Exception {
          for (int i = 0; i < 10; i++) {
            test();
          }

          System.out.println("All swell");
        }

        public static void test() throws Exception {
          saveFile(SCRIPT_FILE, SCRIPT);

          ScriptEngineManager manager = new ScriptEngineManager();
          ScriptEngine engine = manager.getEngineByExtension("js");

          URL scriptURL = SCRIPT_FILE.toURI().toURL();
          Reader reader = new URLReader(scriptURL);
          try {
            engine.eval(reader);
          } catch (ScriptException ex) {
            System.err.println(ex.getMessage());
          } finally {
            reader.close();
          }

          boolean deleted = SCRIPT_FILE.delete();

          if (!deleted) {
            throw new Exception("Failed to delete script file");
          }
        }

        private static void saveFile(File file, String contents) throws IOException {
          FileOutputStream out = new FileOutputStream(file);

          try {
            out.write(contents.getBytes());
          } finally {
            out.close();
          }
        }

      }
      ---------- END SOURCE ----------

            Assignee:
            Jim Laskey
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: