-
Bug
-
Resolution: Fixed
-
P4
-
8u221, 11, 12
-
b23
-
x86_64
-
windows
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8226399 | 11.0.5-oracle | Joe Wang | P4 | Resolved | Fixed | b02 |
JDK-8228987 | 11.0.5 | Joe Wang | P4 | Resolved | Fixed | b02 |
JDK-8242233 | openjdk8u262 | Joe Wang | P4 | Resolved | Fixed | team |
JDK-8261133 | 8u301 | Ravi Reddy | P4 | Resolved | Fixed | b01 |
JDK-8239796 | 8u291 | Ravi Reddy | P4 | Closed | Duplicate |
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
- backported by
-
JDK-8226399 SAXParser.parse(File, ..) does not close resources when Exception occurs.
- Resolved
-
JDK-8228987 SAXParser.parse(File, ..) does not close resources when Exception occurs.
- Resolved
-
JDK-8242233 SAXParser.parse(File, ..) does not close resources when Exception occurs.
- Resolved
-
JDK-8243839 SAXParser.parse(File, ..) does not close resources when Exception occurs.
- Resolved
-
JDK-8261133 SAXParser.parse(File, ..) does not close resources when Exception occurs.
- Resolved
-
JDK-8239796 SAXParser.parse(File, ..) does not close resources when Exception occurs.
- Closed
- duplicates
-
JDK-8139262 File handle not closed when using javax.xml.validation.Validator
- Closed
-
JDK-8172784 SAXParser leaks file descriptors when an exception is thrown
- Closed