diff --git a/src/main/java/com/riscure/trs/TraceSet.java b/src/main/java/com/riscure/trs/TraceSet.java index 4b7a5a9..85cf8dc 100644 --- a/src/main/java/com/riscure/trs/TraceSet.java +++ b/src/main/java/com/riscure/trs/TraceSet.java @@ -338,6 +338,7 @@ private void checkValid(Trace trace) { } private void closeReader() throws IOException { + buffer = null; readStream.close(); } diff --git a/src/test/java/TestTraceSet.java b/src/test/java/TestTraceSet.java index a865a88..ba6e132 100644 --- a/src/test/java/TestTraceSet.java +++ b/src/test/java/TestTraceSet.java @@ -663,4 +663,25 @@ void test61ReadLegacyTraceWithoutData() throws IOException, TRSFormatException { assertDoesNotThrow(() -> ts.get(0)); } } + + /** + * Test to reproduce Github issue #65: TRS files remain 'in use' after they have been closed + * Test this issue by opening and closing a file, and then checking whether the file can be deleted. + */ + @Test + void testFileReleasing() throws IOException, TRSFormatException, InterruptedException { + String filePath = tempDir.toAbsolutePath() + File.separator + BYTES_TRS; + // Open the file, and let the try-with-resources statement close it + try (TraceSet traceSet = TraceSet.open(filePath)) { + traceSet.getMetaData().getTraceSetParameters(); + } + // Unfortunately, the current solution requires a garbage collect to have been performed before the issue is resolved. + // Other fixes required either a Java 8 Cleaner.clean() call not accessible from Java 21, or a Java 20 Arena.close(), + // which is not been finalized in Java 21. + System.gc(); + Thread.sleep(1000); + // Assert that the opened file has been closed again, by deleting it. + File file = new File(filePath); + assert(file.delete()); + } }