JDK-8023652 : DocumentBuilder parse failures can cause file handle leak
  • Type: Bug
  • Component: xml
  • Sub-Component: javax.xml.parsers
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • Submitted: 2013-04-12
  • Updated: 2014-11-17
  • Resolved: 2014-04-11
Related Reports
Duplicate :  
Description
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
Comments
JDK6 only issue, as described in the report.
14-03-2014

test1
12-04-2013