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

Prevent ConcurrentModificationException in PomInstallableUnitStore #3698

Merged
merged 1 commit into from
Mar 22, 2024
Merged
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 @@ -18,8 +18,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand Down Expand Up @@ -221,13 +223,34 @@ private Stream<Artifact> getArtifactStream(Artifact artifact, IArtifactFacade fa
MavenProject mavenProject = projectFacade.getReactorProject().adapt(MavenProject.class);
if (mavenProject != null) {
return Stream.concat(Stream.of(mavenProject.getArtifact()),
mavenProject.getAttachedArtifacts().stream());
safeCopy(mavenProject.getAttachedArtifacts()).stream());
}
}
return Stream.of(artifact);

}

private List<Artifact> safeCopy(List<Artifact> list) {
while (true) {
//in parallel execution mode it is possible that items are added to the attached artifacts what will throw ConcurrentModificationException so we must make a quite unusual copy here
//we can not only use one of the List.copyOf(), ArrayList(...) and so on e.g. they often just copy the data but a concurrent copy can lead to data corruption or null values
try {
List<Artifact> copyList = new ArrayList<>();
for (Iterator<Artifact> iterator = list.iterator(); iterator.hasNext();) {
Artifact a = iterator.next();
if (a != null) {
copyList.add(a);
}
}
return copyList;
} catch (ConcurrentModificationException e) {
//retry...
Thread.yield();
}
}

}

void addPomDependencyConsumer(Consumer<PomDependency> consumer) {
gatheredDependencies.forEach(consumer);
dependencyConsumer.add(consumer);
Expand Down
Loading