From 17e6a2261e648a931abda6e08760cde46e358d56 Mon Sep 17 00:00:00 2001 From: "xinjun.jiang" Date: Thu, 8 Oct 2026 10:59:48 +0800 Subject: [PATCH] Release the JAR file when closing its data source Signed-off-by: xinjun.jiang --- .../FileInJarReadableDataSource.java | 1 + .../FileInJarReadableDataSourceCloseTest.java | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 sentinel-extension/sentinel-datasource-extension/src/test/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSourceCloseTest.java diff --git a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java index 9dae866a64..50ae00d163 100644 --- a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java +++ b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java @@ -116,6 +116,7 @@ private void firstLoad() { @Override public void close() throws Exception { + jarFile.close(); buf = null; } diff --git a/sentinel-extension/sentinel-datasource-extension/src/test/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSourceCloseTest.java b/sentinel-extension/sentinel-datasource-extension/src/test/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSourceCloseTest.java new file mode 100644 index 0000000000..4b8dd3ddb5 --- /dev/null +++ b/sentinel-extension/sentinel-datasource-extension/src/test/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSourceCloseTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.datasource; + +import java.io.File; +import java.io.FileOutputStream; +import java.lang.reflect.Field; +import java.nio.charset.StandardCharsets; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.junit.Assert.fail; + +public class FileInJarReadableDataSourceCloseTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void testCloseReleasesJarAndIsIdempotent() throws Exception { + assertCloseReleasesJar(source -> source); + } + + @Test + public void testCloseReleasesJarAfterParserFailure() throws Exception { + assertCloseReleasesJar(source -> { + throw new IllegalArgumentException("Invalid configuration"); + }); + } + + private void assertCloseReleasesJar(Converter converter) throws Exception { + File jar = temporaryFolder.newFile(); + try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jar))) { + output.putNextEntry(new JarEntry("rules.json")); + output.write("[]".getBytes(StandardCharsets.UTF_8)); + output.closeEntry(); + } + FileInJarReadableDataSource dataSource = new FileInJarReadableDataSource<>( + jar.getAbsolutePath(), "rules.json", converter); + Field field = FileInJarReadableDataSource.class.getDeclaredField("jarFile"); + field.setAccessible(true); + JarFile jarFile = (JarFile) field.get(dataSource); + try { + dataSource.close(); + dataSource.close(); + try { + jarFile.getJarEntry("rules.json"); + fail("The data source's JarFile should be closed"); + } catch (IllegalStateException expected) { + // A closed JarFile rejects access to its entries. + } + } finally { + jarFile.close(); + } + } +}