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

Make LocalCache not use synchronized to detect recursive loads. #6857

Merged
1 commit merged into from
Dec 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import com.google.common.testing.TestLogHandler;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
Expand Down Expand Up @@ -2639,8 +2640,86 @@ public void testSerializationProxyManual() {
assertEquals(localCacheTwo.ticker, localCacheThree.ticker);
}

public void testLoadDifferentKeyInLoader() throws ExecutionException, InterruptedException {
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
String key1 = "key1";
String key2 = "key2";

assertEquals(
key2,
cache.get(
key1,
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(key2, identityLoader()); // loads a different key, should work
}
}));
}

public void testRecursiveLoad() throws InterruptedException {
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
String key = "key";
CacheLoader<String, String> loader =
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(key, identityLoader()); // recursive load, this should fail
}
};
testLoadThrows(key, cache, loader);
}

public void testRecursiveLoadWithProxy() throws InterruptedException {
String key = "key";
String otherKey = "otherKey";
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
CacheLoader<String, String> loader =
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(
key,
identityLoader()); // recursive load (same as the initial one), this should fail
}
};
CacheLoader<String, String> proxyLoader =
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(otherKey, loader); // loads another key, is ok
}
};
testLoadThrows(key, cache, proxyLoader);
}

// utility methods

private void testLoadThrows(
String key, LocalCache<String, String> cache, CacheLoader<String, String> loader)
throws InterruptedException {
CountDownLatch doneSignal = new CountDownLatch(1);
Thread thread =
new Thread(
() -> {
try {
cache.get(key, loader);
} catch (UncheckedExecutionException | ExecutionException e) {
doneSignal.countDown();
}
});
thread.start();

boolean done = doneSignal.await(1, TimeUnit.SECONDS);
if (!done) {
StringBuilder builder = new StringBuilder();
for (StackTraceElement trace : thread.getStackTrace()) {
builder.append("\tat ").append(trace).append('\n');
}
fail(builder.toString());
}
}

/**
* Returns an iterable containing all combinations of maximumSize, expireAfterAccess/Write,
* weakKeys and weak/softValues.
Expand Down
31 changes: 24 additions & 7 deletions android/guava/src/com/google/common/cache/LocalCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2180,12 +2180,7 @@ V lockedGetOrLoad(K key, int hash, CacheLoader<? super K, V> loader) throws Exec

if (createNewEntry) {
try {
// Synchronizes on the entry to allow failing fast when a recursive load is
// detected. This may be circumvented when an entry is copied, but will fail fast most
// of the time.
synchronized (e) {
return loadSync(key, hash, loadingValueReference, loader);
}
return loadSync(key, hash, loadingValueReference, loader);
} finally {
statsCounter.recordMisses(1);
}
Expand All @@ -2201,7 +2196,22 @@ V waitForLoadingValue(ReferenceEntry<K, V> e, K key, ValueReference<K, V> valueR
throw new AssertionError();
}

checkState(!Thread.holdsLock(e), "Recursive load of: %s", key);
// As of this writing, the only prod ValueReference implementation for which isLoading() is
// true is LoadingValueReference. (Note, however, that not all LoadingValueReference instances
// have isLoading()==true: LoadingValueReference has a subclass, ComputingValueReference, for
// which isLoading() is false!) However, that might change, and we already have a *test*
// implementation for which it doesn't hold. So we check instanceof to be safe.
if (valueReference instanceof LoadingValueReference) {
// We check whether the thread that is loading the entry is our current thread, which would
// mean that we are both loading and waiting for the entry. In this case, we fail fast
// instead of deadlocking.
checkState(
((LoadingValueReference<K, V>) valueReference).getLoadingThread()
!= Thread.currentThread(),
"Recursive load of: %s",
key);
}

// don't consider expiration as we're concurrent with loading
try {
V value = valueReference.waitForValue();
Expand Down Expand Up @@ -3427,6 +3437,8 @@ static class LoadingValueReference<K, V> implements ValueReference<K, V> {
final SettableFuture<V> futureValue = SettableFuture.create();
final Stopwatch stopwatch = Stopwatch.createUnstarted();

final Thread loadingThread;

public LoadingValueReference() {
this(LocalCache.<K, V>unset());
}
Expand All @@ -3438,6 +3450,7 @@ public LoadingValueReference() {
*/
public LoadingValueReference(ValueReference<K, V> oldValue) {
this.oldValue = oldValue;
this.loadingThread = Thread.currentThread();
}

@Override
Expand Down Expand Up @@ -3541,6 +3554,10 @@ public ValueReference<K, V> copyFor(
ReferenceQueue<V> queue, @CheckForNull V value, ReferenceEntry<K, V> entry) {
return this;
}

Thread getLoadingThread() {
return this.loadingThread;
}
}

// Queues
Expand Down
79 changes: 79 additions & 0 deletions guava-tests/test/com/google/common/cache/LocalCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import com.google.common.testing.TestLogHandler;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
Expand Down Expand Up @@ -2688,8 +2689,86 @@ public void testSerializationProxyManual() {
assertEquals(localCacheTwo.ticker, localCacheThree.ticker);
}

public void testLoadDifferentKeyInLoader() throws ExecutionException, InterruptedException {
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
String key1 = "key1";
String key2 = "key2";

assertEquals(
key2,
cache.get(
key1,
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(key2, identityLoader()); // loads a different key, should work
}
}));
}

public void testRecursiveLoad() throws InterruptedException {
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
String key = "key";
CacheLoader<String, String> loader =
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(key, identityLoader()); // recursive load, this should fail
}
};
testLoadThrows(key, cache, loader);
}

public void testRecursiveLoadWithProxy() throws InterruptedException {
String key = "key";
String otherKey = "otherKey";
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
CacheLoader<String, String> loader =
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(
key,
identityLoader()); // recursive load (same as the initial one), this should fail
}
};
CacheLoader<String, String> proxyLoader =
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return cache.get(otherKey, loader); // loads another key, is ok
}
};
testLoadThrows(key, cache, proxyLoader);
}

// utility methods

private void testLoadThrows(
String key, LocalCache<String, String> cache, CacheLoader<String, String> loader)
throws InterruptedException {
CountDownLatch doneSignal = new CountDownLatch(1);
Thread thread =
new Thread(
() -> {
try {
cache.get(key, loader);
} catch (UncheckedExecutionException | ExecutionException e) {
doneSignal.countDown();
}
});
thread.start();

boolean done = doneSignal.await(1, TimeUnit.SECONDS);
if (!done) {
StringBuilder builder = new StringBuilder();
for (StackTraceElement trace : thread.getStackTrace()) {
builder.append("\tat ").append(trace).append('\n');
}
fail(builder.toString());
}
}

/**
* Returns an iterable containing all combinations of maximumSize, expireAfterAccess/Write,
* weakKeys and weak/softValues.
Expand Down
31 changes: 24 additions & 7 deletions guava/src/com/google/common/cache/LocalCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2184,12 +2184,7 @@ V lockedGetOrLoad(K key, int hash, CacheLoader<? super K, V> loader) throws Exec

if (createNewEntry) {
try {
// Synchronizes on the entry to allow failing fast when a recursive load is
// detected. This may be circumvented when an entry is copied, but will fail fast most
// of the time.
synchronized (e) {
return loadSync(key, hash, loadingValueReference, loader);
}
return loadSync(key, hash, loadingValueReference, loader);

Choose a reason for hiding this comment

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

Sorry for this post merge comment, I was expecting to find this change on current Guava release, however it looks like the current version on master is still using synchronized (see below). Was this change reverted?

https://github.com/google/guava/blob/master/guava/src/com/google/common/cache/LocalCache.java#L2186-L2191

Copy link
Member

Choose a reason for hiding this comment

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

No problem at all.

Indeed, we reverted this in https://github.com/google/guava/releases/tag/v33.1.0 because of a bug: #6851 (comment)

From what I understand, the JDK issue will be fixed in Java 24: openjdk/jdk#21565

But I know that's not much help now :/

Choose a reason for hiding this comment

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

Thanks for the quick reply! it looks like the suggested path is migrating to Caffeine, I'll look into that. Thank you!

Copy link
Member

Choose a reason for hiding this comment

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

You're welcome.

To be clear, Caffeine also uses synchronized, so its users also need the JDK fix to eliminate the problem with virtual threads. It just offers plenty of other advantages over Guava :)

Choose a reason for hiding this comment

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

Indeed, due the the dependency with Java ConcurrentHashMap, however it looks like AsynCache is not affected ben-manes/caffeine#779

} finally {
statsCounter.recordMisses(1);
}
Expand All @@ -2205,7 +2200,22 @@ V waitForLoadingValue(ReferenceEntry<K, V> e, K key, ValueReference<K, V> valueR
throw new AssertionError();
}

checkState(!Thread.holdsLock(e), "Recursive load of: %s", key);
// As of this writing, the only prod ValueReference implementation for which isLoading() is
// true is LoadingValueReference. (Note, however, that not all LoadingValueReference instances
// have isLoading()==true: LoadingValueReference has a subclass, ComputingValueReference, for
// which isLoading() is false!) However, that might change, and we already have a *test*
// implementation for which it doesn't hold. So we check instanceof to be safe.
if (valueReference instanceof LoadingValueReference) {
// We check whether the thread that is loading the entry is our current thread, which would
// mean that we are both loading and waiting for the entry. In this case, we fail fast
// instead of deadlocking.
checkState(
((LoadingValueReference<K, V>) valueReference).getLoadingThread()
!= Thread.currentThread(),
"Recursive load of: %s",
key);
}

// don't consider expiration as we're concurrent with loading
try {
V value = valueReference.waitForValue();
Expand Down Expand Up @@ -3517,12 +3527,15 @@ static class LoadingValueReference<K, V> implements ValueReference<K, V> {
final SettableFuture<V> futureValue = SettableFuture.create();
final Stopwatch stopwatch = Stopwatch.createUnstarted();

final Thread loadingThread;

public LoadingValueReference() {
this(null);
}

public LoadingValueReference(@CheckForNull ValueReference<K, V> oldValue) {
this.oldValue = (oldValue == null) ? LocalCache.unset() : oldValue;
this.loadingThread = Thread.currentThread();
}

@Override
Expand Down Expand Up @@ -3647,6 +3660,10 @@ public ValueReference<K, V> copyFor(
ReferenceQueue<V> queue, @CheckForNull V value, ReferenceEntry<K, V> entry) {
return this;
}

Thread getLoadingThread() {
return this.loadingThread;
}
}

static class ComputingValueReference<K, V> extends LoadingValueReference<K, V> {
Expand Down