Skip to content

DefaultRedisCacheWriter potentially leaks lock in rare case? #2598

Closed
@zhiyazw

Description

@zhiyazw

We're using @Cacheable to annotate methods, and using Redis as the storage.

One day I found one of the @Cacheable method always blocked until timeout.
When I checked the corresponding redis keys, I found there was only one key with "~lock" suffix.

For example, configure the annotation like this:

@Cacheable(cacheNames ="UserCache", //
            key = "T(java.lang.String).format(\"%s|%s|usercache\", #org, #division)", //
            unless = "#result == null")
public UserList getUsers(String org, String division)

In normal, there should be KEYS like this: UserCache_spring|framework|usercache. However, when this issue occurred, all such keys has expired (ttl=24 hours).
There was only one key named UserCache~lock, it should have been there for more than 24 hours.

The possible reason is, inside https://github.com/spring-projects/spring-data-redis/blob/main/src/main/java/org/springframework/data/redis/cache/DefaultRedisCacheWriter.java#putIfAbsent, in a very rare condition, a writing thread crashes between the line the lock is created and the line entering try block, then it doesn't get guarantee to be released, so cause a lock leak problem.

A link of this issue in gitter: https://matrix.to/#/!MJtHKfEduxwvhntKyg:gitter.im/$BJCPcSX_PqDSf2wQpcz_LYNl4ixJCTNkkk2V6Un3kRg?via=gitter.im&via=matrix.org

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions