Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
classpath 'com.palantir.gradle.externalpublish:gradle-external-publish-plugin:1.11.0'
classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:2.10.0'
classpath 'com.gradle.publish:plugin-publish-plugin:0.21.0'
classpath 'com.palantir.baseline:gradle-baseline-java:4.133.0'
classpath 'com.palantir.baseline:gradle-baseline-java:4.131.0'
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.24.0'
}
}
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2268.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix concurrency issue in LazilyConfiguredMapping
links:
- https://github.com/palantir/gradle-baseline/pull/2268
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,32 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import javax.annotation.concurrent.GuardedBy;
import org.gradle.api.Action;

final class LazilyConfiguredMapping<K, V, A> {
private final Supplier<V> valueFactory;

@GuardedBy("this")
private final List<LazyValues<K, V, A>> values = new ArrayList<>();

@GuardedBy("this")
private final Map<K, Optional<V>> computedValues = new HashMap<>();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considered swapping to a ConcurrentHashmap which handles nested mutations more gracefully, however that's not expected to happen (and isn't related to this issue).


@GuardedBy("this")
private boolean finalized = false;

LazilyConfiguredMapping(Supplier<V> valueFactory) {
this.valueFactory = valueFactory;
}

public void put(LazyValues<K, V, A> lazyValues) {
public synchronized void put(LazyValues<K, V, A> lazyValues) {
ensureNotFinalized();

values.add(lazyValues);
}

public void put(K key, Action<V> value) {
public synchronized void put(K key, Action<V> value) {
ensureNotFinalized();

put((requestedKey, _ignored) -> {
Expand All @@ -62,7 +69,7 @@ private void ensureNotFinalized() {
}
}

public Optional<V> get(K key, A additionalData) {
public synchronized Optional<V> get(K key, A additionalData) {
finalized = true;

return computedValues.computeIfAbsent(key, _ignored -> {
Expand Down