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

Race condition in ConcurrentMapCache [SPR-16533] #21076

Closed
spring-projects-issues opened this issue Feb 25, 2018 · 1 comment
Closed

Race condition in ConcurrentMapCache [SPR-16533] #21076

spring-projects-issues opened this issue Feb 25, 2018 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Feb 25, 2018

William Hoyle opened SPR-16533 and commented

ConcurrentMapCache.get(Object, Callable) can throw a NullPointerException if the key is evicted by another thread during the get.

From the source:

if (this.store.containsKey(key)) {
	// get(key) may return null if another thread evicts the key  
	return (T) get(key).get();
}

This is fixed by #18383 in the 5.0 branch.

Test code:

import org.springframework.cache.concurrent.ConcurrentMapCache;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Scratch {
    public static void main(String[] args) {
        ConcurrentMapCache cache = new ConcurrentMapCache("foo");

        ExecutorService executor = Executors.newCachedThreadPool();
        AtomicInteger count = new AtomicInteger();
        int iterations = 100;
        for (int i = 0; i < iterations; i++) {
            executor.execute(() -> {
                for (int j = 0; j < iterations; j++) {
                    int k = j;
                    try {
                        if (cache.get("test", () -> k) != null) {
                            count.incrementAndGet();
                        }
                    } catch (Throwable t) {
                        // ignore problems
                    }
                    cache.evict("test");
                }
            });
        }
        executor.shutdown();

        int expected = iterations * iterations;
        if (count.get() != expected) {
            throw new AssertionError("Expected " + expected + ", got " + count);
        }
    }
}

Affects: 4.3.14

Issue Links:

Referenced from: commits 06e2bad

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

The containsKey check isn't necessary anyway since we can simply check the get(key) result for null, differentiating between a non-existing key and a null value that way. Fixed for 4.3.15 now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants