jersey icon indicating copy to clipboard operation
jersey copied to clipboard

Nullpointer in ServerProcessingBinder

Open jerseyrobot opened this issue 8 years ago • 9 comments

In a certain configuration, when a Factory is bound to a request scope, nullpointers occurs in ServerProcessingBinder.

I have tried to corner the issue, from what I have figured out, the nullpointer only occurred when the dependency jersey-media-json-jackson is in the classpath. If this dependency is not in the classpath, then all is fine. Moreover, If I use the same configuration, but if I use Tomcat instead of Grizzly, there are no problem at all.

I have created to test project to reproduce the problem: https://github.com/amanteaux/jersey-test In this project, the only problem is the error log displayed. However, in some cases, Jersey also returns an error 500 response (though Jersey does execute the web-service). That is why I qualify this bug as critical: I cannot use Grizzly in production since all my web-services that are using a Context parameter are sometimes returning error 500 responses.

Here is a very small extract of the error log: janv. 29, 2017 7:07:14 PM org.glassfish.jersey.internal.Errors logErrors WARNING: The following warnings have been detected: WARNING: HK2 failure has been detected in a code that does not run in an active Jersey Error scope. WARNING: Unknown HK2 failure detected: MultiException stack 1 of 1 java.lang.NullPointerException at org.glassfish.jersey.server.internal.process.ServerProcessingBinder$ContainerRequestFactory$1.transform(ServerProcessingBinder.java:154) at org.glassfish.jersey.server.internal.process.ServerProcessingBinder$ContainerRequestFactory$1.transform(ServerProcessingBinder.java:151) [...]

Affected Versions

[2.23.1, 2.25, 2.25.1]

jerseyrobot avatar Jan 29 '17 18:01 jerseyrobot

  • Issue Imported From: https://github.com/jersey/jersey/issues/3498
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: @mpotociar

jerseyrobot avatar Apr 20 '18 08:04 jerseyrobot

@glassfishrobot Commented Reported by amanteaux

jerseyrobot avatar Jan 29 '17 18:01 jerseyrobot

@glassfishrobot Commented This issue was imported from java.net JIRA JERSEY-3226

jerseyrobot avatar Apr 25 '17 05:04 jerseyrobot

@injeniero Commented @mpotociar This happens randomly because it will depend on the order the factory is killed while the scope is shutting down. So a new factory is needed to dispose the object it created if the factory was killed first. When this happen, the new factory will fail to be created if it depends on other objects that are no longer in the scope.

This is the issue I reported and a fix. I have the fix on prod and is working ok.

https://github.com/jersey/jersey/issues/3503

jerseyrobot avatar Jun 12 '17 23:06 jerseyrobot

@glucas Commented I was hitting this pretty reliably in some unit tests. The proposed fix from #3503 works for me.

jerseyrobot avatar Aug 13 '17 18:08 jerseyrobot

Hitting something similar. Could it be the same issue?

1-Apr-2020 08:53:45.101 SEVERE [https-openssl-nio-8443-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.atomgraph.linkeddatahub.Application] in context with path [] threw exception [A MultiException has 3 exceptions.  They are:
1. java.lang.NullPointerException
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.atomgraph.linkeddatahub.server.provider.ApplicationFactory errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.atomgraph.linkeddatahub.server.provider.ApplicationFactory
] with root cause
        java.lang.NullPointerException
                at org.glassfish.jersey.server.internal.process.RequestProcessingConfigurator$ContainerRequestFactory.get(RequestProcessingConfigurator.java:66)
                at org.glassfish.jersey.server.internal.process.RequestProcessingConfigurator$ContainerRequestFactory.get(RequestProcessingConfigurator.java:55)
                at org.glassfish.jersey.inject.hk2.SupplierFactoryBridge.provide(SupplierFactoryBridge.java:76)
                at org.jvnet.hk2.internal.FactoryCreator.create(FactoryCreator.java:129)
                at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
                at org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:59)
                at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
                at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:93)
                at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:67)
                at org.glassfish.jersey.inject.hk2.ContextInjectionResolverImpl.resolve(ContextInjectionResolverImpl.java:103)
                at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:188)
                at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:211)
                at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:334)
                at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
                at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:46)
                at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
                at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:93)
                at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:67)
                at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:151)
                at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:518)
                at org.glassfish.jersey.inject.hk2.RequestContext.lambda$findOrCreate$0(RequestContext.java:60)
                at org.glassfish.jersey.internal.inject.ForeignDescriptorImpl.dispose(ForeignDescriptorImpl.java:63)
                at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.remove(Hk2RequestScope.java:126)
                at java.lang.Iterable.forEach(Iterable.java:75)
                at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release(Hk2RequestScope.java:143)
                at org.glassfish.jersey.process.internal.RequestScope.release(RequestScope.java:246)
                at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:267)
                at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
                at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
                at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
                at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
                at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
                at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
                at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.catalina.filters.HttpHeaderSecurityFilter.doFilter(HttpHeaderSecurityFilter.java:126)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
                at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
                at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
                at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
                at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
                at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
                at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
                at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
                at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
                at java.lang.Thread.run(Thread.java:748)

namedgraph avatar Apr 21 '20 21:04 namedgraph

I am seeing the same thing as namedgraph. This has been plaguing me for a while. Seems to to be some sort of issue in initialization for me. When it happens, it affects all API requests hitting the application from the time it is deployed until we restart it. Restarting fixes it sometimes.

java.lang.NullPointerException
        at org.glassfish.jersey.server.internal.process.RequestProcessingConfigurator$ContainerRequestFactory.get(RequestProcessingConfigurator.java:66)

twoodhouse avatar May 01 '20 23:05 twoodhouse

@twoodhouse see https://github.com/eclipse-ee4j/jersey/issues/2627 and try:

public class SomeFactory implements Factory<Some>
 {

    @Context ServiceLocator serviceLocator;
    
    public Some provide()
    {
        getSome(serviceLocator.getService(ContainerRequestContext.class));
    }

    public Some getSome(ContainerRequestContext crc)
    {
        // request-specific logic here
    }
...

namedgraph avatar May 02 '20 06:05 namedgraph

Workaround for Kotlin:

class MyClassFactory @Inject constructor(
    private val serviceLocator: ServiceLocator
) : Factory<MyClass> {

    private fun getRequestContext() = serviceLocator.getService(ContainerRequestContext::class.java)

    override fun provide(): MyClass {
        getMyClass(getRequestContext())
    }

    private fun getMyClass(crc: ContainerRequestContext): MyClass {
        // your code for creating/extracting your class using ContainerRequestContext
    }

    override fun dispose(t: MyClass) {
        // your code to dispose it if you store something outside of ContainerRequestContext
        // and it needs to be disposed on factory scope end
    }
}

InnokentiyStruchkov avatar Sep 21 '23 11:09 InnokentiyStruchkov