Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jar file reference release race condition #43221

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.jar.JarFile;

Expand All @@ -22,6 +23,10 @@ public class JarFileReference {
// The JarFileReference is created as already acquired and that's why the referenceCounter starts from 2
private final AtomicInteger referenceCounter = new AtomicInteger(2);

// Close SHOULD be idempotent. It's possible that the same JarFilesReference is evicted from the class loader's
// cache and then closed multiple times.
private final AtomicBoolean closeRequested = new AtomicBoolean(false);

private JarFileReference(JarFile jarFile) {
this.jarFile = jarFile;
}
Expand Down Expand Up @@ -78,7 +83,9 @@ private boolean release(JarResource jarResource) {
* will leave.
*/
void close(JarResource jarResource) {
release(jarResource);
if (closeRequested.compareAndSet(false, true)) {
release(jarResource);
}
}

@FunctionalInterface
Expand Down