NullPointerException in WebSocket Endpoint (follow on from another NPE question I asked)

I was seeing an NPE when trying to connect to my Websocket Endpoint, which I asked about here: NullPointerException when authenticating WebSocket connection

Since my Websocket endpoint now uses the injected Context and Model Context, that NPE went away; however, I am now seeing a new one but only on certain XWiki deployments. My dev environment is a clean v13 install and it works fine. On a v12 that was upgraded to v13, I see the NPE. Maybe it’s not the upgrade that is the issue and it’s a race condition or something else but, that’s the only key difference I can point out right now.

The v13 being used is 13.10.5 across all instances.

The NPE being thrown is:

2022-07-13 09:56:50,705 [http-nio-8080-exec-3 -] ERROR c.x.x.XWiki                    - Failed to get URL for provided wiki [t725e89b91a72447dab483c204e54e]
java.lang.NullPointerException: null
        at com.xpn.xwiki.XWiki.getServletPath(
        at com.xpn.xwiki.web.XWikiServletURLFactory.addServletPath(
        at com.xpn.xwiki.web.XWikiServletURLFactory.createURL(
        at com.xpn.xwiki.web.XWikiDefaultURLFactory.createURL(
        at com.xpn.xwiki.web.XWikiDefaultURLFactory.createURL(
        at com.xpn.xwiki.user.impl.xwiki.XWikiAuthServiceImpl.getAuthenticator(
        at com.xpn.xwiki.user.impl.xwiki.XWikiAuthServiceImpl.checkAuth(
        at com.seekingbinary.xwiki.authn.CustomAuthenticationService.checkAuth(
        at com.xpn.xwiki.XWiki.checkAuth(
        at org.xwiki.websocket.internal.DefaultWebSocketContext.createExecutionContext(
        at org.xwiki.websocket.internal.DefaultWebSocketContext.initialize(
        at org.xwiki.websocket.internal.XWikiEndpointConfigurator.modifyHandshake(
        at org.apache.tomcat.websocket.server.UpgradeUtil.doUpgrade(
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
        at org.xwiki.container.servlet.filters.internal.SetHTTPHeaderFilter.doFilter(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
        at org.xwiki.container.servlet.filters.internal.SavedRequestRestorerFilter.doFilter(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
        at org.xwiki.container.servlet.filters.internal.SetCharacterEncodingFilter.doFilter(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
        at org.xwiki.resource.servlet.RoutingFilter.doFilter(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
        at org.apache.catalina.core.StandardWrapperValve.invoke(
        at org.apache.catalina.core.StandardContextValve.invoke(
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
        at org.apache.catalina.core.StandardHostValve.invoke(
        at org.apache.catalina.valves.ErrorReportValve.invoke(
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(
        at org.apache.catalina.core.StandardEngineValve.invoke(
        at org.apache.catalina.connector.CoyoteAdapter.service(
        at org.apache.coyote.http11.Http11Processor.service(
        at org.apache.coyote.AbstractProcessorLight.process(
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$
        at org.apache.tomcat.util.threads.TaskThread$
        at java.base/

Looking at the XWiki source I can see that DefaultWebSocketContext#createExecutionContext creates a stub context and calls checkAuth on the Authenticator on line 149. I think the problem is that the DefaultWikiDescriptorDocumentHelper.getDocument( requires the XWikiContext to have been set/initialised on the Execution; however, that hasn’t happened yet as it’s only on return of the DefaultWebSocketContext#createExecutionContext that the context that is created is set on the Execution (in DefaultWebSocketContext#initialize on line 105).

To be more specific…

  1. DefaultWebSocketContext#initialize is called, which calls…
  2. DefaultWebSocketContext#createExecutionContext, which calls…
  3. xcontext.getWiki().checkAuth(xcontext);, which eventually calls…
  4. DefaultWikiDescriptorDocumentHelper#getDocument, which calls…
  5. XWikiContext context = xcontextProvider.get();. (Line 186), which calls…
  6. XWikiContextProvider#get, which calls…
  7. XWikiContextProvider#getXWikiContext, which calls…
  8. ExecutionContext econtext = this.execution.getContext();, which I think is returning null, so the xcontextProvider.get(); returns null.
    • As the context object is null, Line 187 in DefaultWikiDescriptorDocumentHelper#getDocument fails with an NPE.
  9. (If it didn’t NPE) The stack unravels back to DefaultWebSocketContext#initialize (step 1), which calls…
  10. DefaultWebSocketContext#initialize(ExecutionContext context), which calls…
  11. this.execution.pushContext(context, false); - Which I think is too late in this process.

Does this look like I’m on the right track and there is a bug here or could there be something else going on? I’ll continue to dig as I’m surprised I am not seeing this on other environments so there is something I’m probably missing.

Thanks in advance!


I think you can create a ticket for that one, personally I find it weird that DefaultWebSocketContext.createExecutionContext is calling XWiki.checkAuth but I don’t know much about this code. @mflorea would know better but he’s off these days.

Note that I created a ticket related to a NPE in DefaultWebSocketContext.createExecutionContext few weeks ago, but it was for a different reason apparently, see: Loading....

Thanks @surli. I’ll wait to see if @mflorea has any comments just in case, otherwise I’ll raise an issue in Jira. I’ll try to get to the bottom of why it’s happening in one xwiki install but not others.

@surli / @mflorea I’ve created: Loading...

@mflorea Have you had by chance an opportunity to look at this issue at all?

Thanks in advance,