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

Bug Report: NPE when @CacheResult annotated method returns null #7049

Closed
sschulze opened this issue Nov 7, 2024 · 2 comments
Closed

Bug Report: NPE when @CacheResult annotated method returns null #7049

sschulze opened this issue Nov 7, 2024 · 2 comments
Assignees
Labels
Status: Open Issue has been triaged by the front-line engineers and is being worked on verification Type: Bug Label issue as a bug defect

Comments

@sschulze
Copy link

sschulze commented Nov 7, 2024

Brief Summary

My runtime is Payara (Community 5.2022.5 and Enterprise 5.67) with the integrated Hazelcast as JCache layer.
In a CDI bean I have a public method annotated with @CacheResult. Under some circumstances this method can return null. Currently, this results in a NPE quite deep in Hazelcast.

Expected Outcome

I expect that the method returns null and probably doesn't cache anything (because JCache doesn't support to cache null values).

This expectation is supported by the JCache TCK test org.jsr107.tck.annotation.AbstractBlogManagerInterceptionTest#test_AT_CacheRemoveEntry() where in the end it is asserted that the annotated method returns a null value.

Current Outcome

Currently, the following exception is thrown:

java.lang.NullPointerException: Null value is not allowed!
    at com.hazelcast.internal.util.Preconditions.checkNotNull(Preconditions.java:59) ~[hazelcast.jar:4.2.5]
    at com.hazelcast.cache.impl.CacheProxyUtil.validateNotNull(CacheProxyUtil.java:89) ~[hazelcast.jar:4.2.5]
    at com.hazelcast.cache.impl.CacheProxySupport.putAsyncInternal(CacheProxySupport.java:351) ~[hazelcast.jar:4.2.5]
    at com.hazelcast.cache.impl.CacheProxy.put(CacheProxy.java:525) ~[hazelcast.jar:4.2.5]
    at com.hazelcast.cache.impl.CacheProxy.put(CacheProxy.java:141) ~[hazelcast.jar:4.2.5]
    at fish.payara.cdi.jsr107.CacheResultInterceptor.cacheResult(CacheResultInterceptor.java:79)

which is probably caused by the missing null-check in CacheResultInterceptor#cacheResult(...) between proceeding and before putting as the specification of javax.cache.Cache#put(K, V) declares it to throw a NPE when invoked with a null value.

        try {
            result = ctx.proceed();
            cache.put(key, result);
        } catch (Throwable e) {

Reproducer

Apply following patch to https://github.com/payara/Payara-Examples/blob/main/javaee/jcache/jcache-cdi/src/main/java/fish/payara/examples/jcachecdi/Model.java

diff --git a/javaee/jcache/jcache-cdi/src/main/java/fish/payara/examples/jcachecdi/Model.java b/javaee/jcache/jcache-cdi/src/main/java/fish/payara/examples/jcachecdi/Model.java
index 26cf0fa..656d0cf 100644
--- a/javaee/jcache/jcache-cdi/src/main/java/fish/payara/examples/jcachecdi/Model.java
+++ b/javaee/jcache/jcache-cdi/src/main/java/fish/payara/examples/jcachecdi/Model.java
@@ -57,6 +57,7 @@ public class Model implements Serializable {
      */
     @CacheResult(cacheName = "cache")
     public String slowOperation(int number){
+       if (number == 0) return null;
         try{

             Thread.currentThread().sleep(2000);

Then start the application and invoke the Servlet with POST /NewServlet?number=0

Operating System

Suse Linux, MacOS

JDK Version

Corretto-11.0.20

Payara Distribution

Payara Server Full Profile

@sschulze sschulze added Status: Open Issue has been triaged by the front-line engineers and is being worked on verification Type: Bug Label issue as a bug defect labels Nov 7, 2024
@artur-mal
Copy link

Greetings @sschulze

Regarding the Payara Server versions you mentioned:

  • Community 5.2022.5: This version does not have long-term support. Consequently, Payara 5 Community has been replaced by Payara 6 Community. Support is only provided for the latest version, which is 6.2024.10 at the time of writing.

  • Enterprise 5.67: this is an official Payara Enterprise build, and its support is covered through an active Payara Enterprise subscription.

Can you confirm that you either:

  • Belong to an organization that has an active Payara Enterprise subscription? If so, please contact one of the account holders of your subscription to raise this request via the official support channels.

  • Received the build through an organization that has an active Payara Enterprise subscription that re-distributes the build to you? If so, please contact them to receive support since all requests must come from companies that hold active accounts.

If none of the above applies to your case, I’m afraid that you’re in violation of the Enterprise EULA, and I must request that you stop using this binary immediately and instead use the latest Payara Community release 6.2024.10.

After this, you are free to verify if this issue is present on the latest Community build so that we can proceed with investigating your report as usual. If this is the case, please raise a new issue with these details.

We’ll proceed to close this issue.

@sschulze
Copy link
Author

sschulze commented Nov 8, 2024

...

Yes, the client I'm working for is a big company with Payara Enterprise Subscription and a massive fleet of Payaras but it's a rather long way through the organization to get this handled via their Enterprise support.
And because I also encountered this issue in a local Payara 5 Community setup as well and after checking the sources-jar of payara-embedded-all-6.2024.10 and recognized the exact same lines of code there (ctx.proceed() directly followed by the cache.put), I thought it might be of interest for you and others to have this issue filed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Open Issue has been triaged by the front-line engineers and is being worked on verification Type: Bug Label issue as a bug defect
Projects
None yet
Development

No branches or pull requests

2 participants