From 8bc43fc0262892c89b10904e42b640e4adf61961 Mon Sep 17 00:00:00 2001 From: Tom Hogervorst Date: Wed, 27 Nov 2024 13:46:03 +0100 Subject: [PATCH 1/2] #65: Reproduce issue in test --- src/test/java/TestTraceSet.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/java/TestTraceSet.java b/src/test/java/TestTraceSet.java index a865a88..235931c 100644 --- a/src/test/java/TestTraceSet.java +++ b/src/test/java/TestTraceSet.java @@ -663,4 +663,20 @@ 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(); + } + // Assert that the opened file has been closed again, by deleting it. + File file = new File(filePath); + assert(file.delete()); + } } From 3162738667e79c6f0f84fda3a659d958bf2390fb Mon Sep 17 00:00:00 2001 From: Tom Hogervorst Date: Wed, 27 Nov 2024 13:54:39 +0100 Subject: [PATCH 2/2] #65: Delete memory mapped buffer on close 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. --- src/main/java/com/riscure/trs/TraceSet.java | 1 + src/test/java/TestTraceSet.java | 5 +++++ 2 files changed, 6 insertions(+) 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 235931c..ba6e132 100644 --- a/src/test/java/TestTraceSet.java +++ b/src/test/java/TestTraceSet.java @@ -675,6 +675,11 @@ void testFileReleasing() throws IOException, TRSFormatException, InterruptedExce 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());