-
Notifications
You must be signed in to change notification settings - Fork 3k
ClosedFileSystemException or NullPointerException thrown when SharedArchivePathTree is opened and closed concurrently #48221
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...rojects/bootstrap/app-model/src/test/java/io/quarkus/paths/SharedArchivePathTreeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.quarkus.paths; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SharedArchivePathTreeTest { | ||
private static final int WORKERS_COUNT = 128; | ||
|
||
@Test | ||
void nullPointerException() throws IOException, InterruptedException, ExecutionException { | ||
/* Reproduce https://github.com/quarkusio/quarkus/issues/48220 */ | ||
stress((OpenPathTree opened) -> { | ||
}); | ||
} | ||
|
||
@Test | ||
void closedFileSystemException() throws IOException, InterruptedException, ExecutionException { | ||
/* Reproduce https://github.com/quarkusio/quarkus/issues/48220 */ | ||
stress((OpenPathTree opened) -> { | ||
try { | ||
Path p = opened.getPath("org/assertj/core/api/Assertions.class"); | ||
Files.readAllBytes(p); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
static void stress(Consumer<OpenPathTree> consumer) throws IOException { | ||
/* Find assertj-core jar in the class path */ | ||
final String rawCp = System.getProperty("java.class.path"); | ||
final String assertjCoreJarPath = Stream.of(rawCp.split(System.getProperty("path.separator"))) | ||
.filter(p -> p.contains("assertj-core")) | ||
.findFirst() | ||
.orElseThrow(() -> new AssertionError("Could not find assertj-core in " + rawCp)); | ||
|
||
/* Create a copy of assertj-core jar in target directory */ | ||
final Path assertjCoreJarPathCopy = Path.of("target/assertj-core-" + UUID.randomUUID() + ".jar"); | ||
if (!Files.exists(assertjCoreJarPathCopy.getParent())) { | ||
Files.createDirectories(assertjCoreJarPathCopy.getParent()); | ||
} | ||
Files.copy(Path.of(assertjCoreJarPath), assertjCoreJarPathCopy); | ||
|
||
/* Now do some concurrent opening and closing of the SharedArchivePathTree instance */ | ||
final ArchivePathTree archivePathTree = SharedArchivePathTree.forPath(assertjCoreJarPathCopy); | ||
final ExecutorService executor = Executors.newFixedThreadPool(WORKERS_COUNT); | ||
final List<Future<Void>> futures = new ArrayList<>(WORKERS_COUNT); | ||
try { | ||
for (int i = 0; i < WORKERS_COUNT; i++) { | ||
final Future<Void> f = executor.submit(() -> { | ||
try (OpenPathTree opened = archivePathTree.open()) { | ||
consumer.accept(opened); | ||
} | ||
return null; | ||
}); | ||
futures.add(f); | ||
} | ||
|
||
// Ensure all tasks are completed | ||
int i = 0; | ||
for (Future<Void> future : futures) { | ||
Assertions.assertThat(future) | ||
.describedAs("Expected success at iteration %d", i++) | ||
.succeedsWithin(30, TimeUnit.SECONDS); | ||
} | ||
} finally { | ||
executor.shutdown(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am still not 100% sure what the intention behind the split into
SharedArchivePathTree
,SharedOpenArchivePathTree
andCallerOpenPathTree
is. A sentence of JavaDoc on each of those classes would help.My hypothesis is that the author wanted to allow multiple instances of
SharedOpenArchivePathTree
perSharedArchivePathTree
where:lastOpen
is the preferred and longer living oneclose()
.openFs()
multiple times on a single file (pernew SharedOpenArchivePathTree(openFs())
) does not make the underlyingZipFilesystemProvider
throw aFileSystemAlreadyExistsException
, because perhaps theSharedArchivePathTree.CACHE
somehow prevents it? Or what else is expected to happen in such situation?When I saw
SharedArchivePathTree.lastOpen
writes and reads being protected byOpenArchivePathTree.lock
inSharedOpenArchivePathTree
, I thought it was a mistake not to protect also writing tolastOpen
inSharedArchivePathTree.open()
:But then, I figured out that the reads/writes from
SharedOpenArchivePathTree
are protected by perSharedOpenArchivePathTree
lock anyway, so multiple instances ofSharedOpenArchivePathTree
can still race on accessing it. So perhaps it was not really intended not to protectSharedArchivePathTree.lastOpen
by those locks at all but rather, those concurrent reads/writes toSharedArchivePathTree.lastOpen
are by design and not considered harmful. Could please anybody confirm that? Git blame shows @aloubyansky as an author but hard to say if he wrote it originally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SharedArchivePathTree
has a bit of a Javadoc explaining the idea behind it.SharedOpenArchivePathTree
is theOpenArchivePathTree
impl of it.CallerOpenPathTree
is basically shielding theSharedOpenArchivePathTree
from the same caller callingclose()
multiple times on it and so from decrementing the user counter.You're right about
lastOpen
not being well protected though. I'll have another look into it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was very thankful to see it :)
Do we really need both Open* and Caller* flavors (and all abstraction layers above)?
Does OpenPathTree have to inherit from PathTree?
All those inheritance, inner-class and delegation links make it really hard to understand what is going on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, OpenPathTree is something that needs to be closed but it otherwise a PathTree.
The whole complication is around caching and concurrent access to the same open archive.
Unfortunately, the test rarely fails for me w/o the fix.