Skip to content

Commit f5e006f

Browse files
authored
GH-1834: Version filtering context with BF fails (#1836)
BF is by default going aggressively (in parallel, unlike old DF) for collection, and causes ConcurrentModificationEx if filtering is used as well. This simple change fixes that. Fixes #1834
1 parent 3422c35 commit f5e006f

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/collect/DefaultVersionFilterContext.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,51 @@
3535
/**
3636
* Default implementation of {@link VersionFilter.VersionFilterContext}.
3737
* Internal helper class for collector implementations.
38+
* This instance is not thread safe, and same context instance must not be shared across threads.
3839
*/
3940
public final class DefaultVersionFilterContext implements VersionFilter.VersionFilterContext {
4041
private final RepositorySystemSession session;
4142

4243
private Dependency dependency;
4344

44-
VersionRangeResult result;
45+
private VersionRangeResult result;
4546

46-
private List<Version> versions;
47+
private ArrayList<Version> versions;
4748

4849
public DefaultVersionFilterContext(RepositorySystemSession session) {
4950
this.session = session;
51+
this.dependency = null;
52+
this.result = null;
53+
this.versions = null;
5054
}
5155

56+
private DefaultVersionFilterContext(
57+
RepositorySystemSession session, Dependency dependency, VersionRangeResult result) {
58+
this.session = session;
59+
this.dependency = dependency;
60+
this.result = result;
61+
this.versions = new ArrayList<>(result.getVersions());
62+
}
63+
64+
/**
65+
* The use of this method allows strictly single-threaded use of context. Is unused in production, only in tests.
66+
*/
67+
@Deprecated
5268
public void set(Dependency dependency, VersionRangeResult result) {
5369
this.dependency = dependency;
5470
this.result = result;
5571
this.versions = new ArrayList<>(result.getVersions());
5672
}
5773

74+
/**
75+
* Creates an initialized, new context instance out of this context session and provided dependency and result.
76+
* The newly created context is still not thread safe, but allows to have multiple contexts with different
77+
* dependencies and results in the same time (processed in multiple threads in parallel like BF collector does).
78+
*/
79+
public DefaultVersionFilterContext initialize(Dependency dependency, VersionRangeResult result) {
80+
return new DefaultVersionFilterContext(this.session, dependency, result);
81+
}
82+
5883
public List<Version> get() {
5984
return new ArrayList<>(versions);
6085
}

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/collect/DependencyCollectorDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ protected static List<? extends Version> filterVersions(
450450

451451
List<? extends Version> versions;
452452
if (verFilter != null && rangeResult.getVersionConstraint().getRange() != null) {
453-
verContext.set(dependency, rangeResult);
453+
verContext = verContext.initialize(dependency, rangeResult);
454454
try {
455455
verFilter.filterVersions(verContext);
456456
} catch (RepositoryException e) {

0 commit comments

Comments
 (0)