JDK-8213734 : SAXParser.parse(File, ..) does not close resources when Exception occurs.
  • Type: Bug
  • Component: xml
  • Sub-Component: org.xml.sax
  • Affected Version: 8u221,11,12
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows
  • CPU: x86_64
  • Submitted: 2018-11-11
  • Updated: 2021-02-04
  • Resolved: 2018-11-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 12 JDK 8 Other
11.0.5-oracleFixed 12 b23Fixed 8u291Resolved openjdk8u262Fixed
Related Reports
Duplicate :  
Duplicate :  
Description
A DESCRIPTION OF THE PROBLEM :
By calling SAXParser.parse(File, DefaultHandler) the parser will parse the file, although if the DefaultHandler throws an exception in startElement (or some other place) the file will not be closed and there will be a resource leak. Since the file is opened by SAXParser it should be responsible for closing the file no matter what.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create an valid XML file and name it test.xml, place it in the execution path for the code provided and run the code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The file should be closed when the execution reaches the Thread.sleep line.
ACTUAL -
Go to the test.xml in the file browser and try chaning its name as an example, there will be an error that tells you the file is open in another process. Thus it has not been closed.

---------- BEGIN SOURCE ----------
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;

public class SAXParserBug {

    public static void main(String... args) throws ParserConfigurationException, SAXException, IOException, InterruptedException {
        SAXParserFactory factory = SAXParserFactory.newDefaultInstance();
        SAXParser parser = factory.newSAXParser();
        try {
            parser.parse(new File("test.xml"), new DefaultHandler() {
                @Override
                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                    throw new SAXException("Stop the parser.");
                }
            });
        } catch (SAXException e) {
            // Do nothing
        }

        Thread.sleep(1000000); // Some process that keeps the application running.
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Instead of using parser.parse(File, ...) one can use

try (FileInputStream fis = new FileInputStream(file)) {
  parser.parse(fis, ...);
}

FREQUENCY : always



Comments
TestNG refactoring AFAICT was necessary to make the test work due to missing library support: http://mail.openjdk.java.net/pipermail/jdk8u-dev/2020-March/011358.html
09-03-2020

Waiting for feedback on necessity of conversion of testcase from TestNG
18-02-2020

Fix Request (OpenJDK 8u): Please approve backporting this reliability fix to OpenJDK 8u. The patch didn't apply cleanly so I got it reviewed (by Mario Torre). Testing: tier1 on Linux x86_64 and javax/xml/jaxp tests. New test on Windows fails without the fix and passes after. Risk seems low. webrevs: jdk: http://cr.openjdk.java.net/~sgehwolf/webrevs/JDK-8213734/jdk8/jdk/01/webrev/ jaxp: http://cr.openjdk.java.net/~sgehwolf/webrevs/JDK-8213734/jdk8/jaxp/02/webrev/ RFR(s): http://mail.openjdk.java.net/pipermail/jdk8u-dev/2019-August/010164.html http://mail.openjdk.java.net/pipermail/jdk8u-dev/2019-October/010441.html
11-10-2019

Fix Request (OpenJDK 11u): Please approve backporting this to OpenJDK 11u which brings the project one step closer to Oracle JDK 11. The patch applies as-is from JDK 12. Testing: test/jdk/javax/xml/ test/jaxp added regression test.
29-07-2019

To reproduce the issue, run the attached test case. JDK 11- Fail JDK 11.0.1- Fail JDK 12-ea+16 - Fail On trying to rename test.xml, it throws the error "The action can't be completed because the file is open in OpenJDK Platform binary".
12-11-2018