Skip to content

NPE with AsyncWeigher #206

@Ked-Ki-Work

Description

@Ked-Ki-Work

When using an AsyncLoadingCache where asyncLoad can return CompletableFuture(null), there is a potential for NullPointerException in AsyncWeigher.

Essentially, if the weigher does not null check the value, it can throw an NPE. This violates the documentation of Weigher, which has the value argument marked @NonNull.

The culprit for the Weigher case seems to be in the definition of AsyncWeigher, which delegates to the user-defined weigher whenever the future is done, without null-checking:

    @Override
    public int weigh(K key, CompletableFuture<V> future) {
      return isReady(future) ? delegate.weigh(key, future.join()) : 0;
    }

Even though LocalAsyncLoadingCache.put(K, CompletableFuture<V>) attempts to check if the future contains null before passing it to the backing cache (and thus invoking the weigher), if the future finishes just after the initial check, but before the weigher is called, this NPE occurs. (See around this line.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions