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

DocumentBuilder parse failures can cause file handle leak

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • xml

      FULL PRODUCT VERSION :
      java version " 1.6.0_43 "
      Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]


      A DESCRIPTION OF THE PROBLEM :
      Basically, if you parse a poorly formatted xml file using a javax.xml.parsers.DocumentBuilder instance, it will raise a org.xml.sax.SAXParseException but not " close " an internal handle to the file being parsed, which means that, depending on when the GC runs, it may or may not be, for instance, on windows, deletable for some period of time.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      If you run the code sample, for instance with jre6/jdk 6, the problem sporadically reproduces (in this snippet, it outputs " leaked a file handle " ). It more consistently reproduces when java is run as a " system " call from within another process.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      it should output " no leak detected "
      ACTUAL -
      with jdk6 it outputs " leaked a file handle " see steps to reproduce. with jre7 it always outputs " no leak detected "

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import java.io.*;

      class Bad {
        public static void main(String[] args) throws Exception {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setNamespaceAware(true); // ??
          DocumentBuilder builder = factory.newDocumentBuilder();
          File fromThisFile = new File( " yo " );
          fromThisFile.createNewFile();
          try {
            builder.parse(fromThisFile);
          } finally {
            if(!fromThisFile.delete())
              System.out.println( "
      leaked a file handle " );
            else
              System.out.println( " no leak detected " );
          }
         }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      upgrading to jre7

            aefimov Aleksej Efimov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: