owlcs / owlapi

OWL API main repository
828 stars 315 forks source link

OWLParser not ensuring streams are closed on success or exception #973

Closed sszuev closed 4 years ago

sszuev commented 4 years ago

OWL-API ver 5.1.16, java version "1.8.0_241" Java(TM) SE Runtime Environment (build 1.8.0_241-b07) Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode) windows7x64

for several formats (NTRIPLES, NQUADS, TRIG, BINARY_RDF, OBO) the following testcase fails on file deleting with the exception "The process cannot access the file because it is being used by another process".

The testcase:

        Supplier<OWLOntologyManager> mf = OntManagers::createOWLAPIImplManager;
        Supplier<OWLDocumentFormat> ff = org.semanticweb.owlapi.formats.NTriplesDocumentFormat::new;
        //Supplier<OWLDocumentFormat> ff = org.semanticweb.owlapi.formats.TurtleDocumentFormat::new;
        //Supplier<OWLDocumentFormat> ff = org.semanticweb.owlapi.formats.OBODocumentFormat::new;

        Path dir = Files.createTempDirectory(TestRoundTrip.class.getSimpleName());

        Path file = Files.createTempFile(dir, "file.", ".xxx");
        OWLOntologyManager m1 = mf.get();
        OWLOntology o1 = m1.createOntology(IRI.create("http://" + Tmp_SaveFormats.class.getName()));
        try (OutputStream out = java.nio.file.Files.newOutputStream(file)) {
            m1.saveOntology(o1, ff.get(), out);
        }

        OWLOntologyDocumentSource src = new IRIDocumentSource(IRI.create(file.toUri()), ff.get(), null);
        OWLOntology o2 = mf.get().loadOntologyFromOntologyDocument(src);
        Assert.assertNotNull(o2);
        Files.delete(file);
        Files.delete(dir);

The stacktrace:

Exception in thread "main" java.nio.file.FileSystemException: at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269) at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103) at java.nio.file.Files.delete(Files.java:1126) at com.github.owlcs.ontapi.tmp.Tmp_SaveFormats$TestRoundTrip.main(Tmp_SaveFormats.java:105)