Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-2752

WeldInitialListener can throw NullPointerException if app fails to start

XMLWordPrintable

    • Hide

      Using Open Liberty:

      • Create an app with a ServletContainerInitializer which throws an Error from its onStartup method.
      • Ensure the app contains a CDI bean
      • Ensure the server config contains both the servlet and cdi features
      • Deploy the app and start the server

      The following error is seen in the log:
      [24/08/2023, 16:17:17:872 BST] 0000003f com.ibm.ws.webcontainer.webapp E SRVE0285E: Exception caught while destroying context: java.lang.NullPointerException: Cannot invoke "org.jboss.weld.module.web.servlet.HttpContextLifecycle.contextDestroyed(jakarta.servlet.ServletContext)" because "this.lifecycle" is null
      at org.jboss.weld.module.web.servlet.WeldInitialListener.contextDestroyed(WeldInitialListener.java:113)
      at com.ibm.ws.cdi.web.factories.Weld50InitialListener.contextDestroyed(Weld50InitialListener.java:51)
      at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextDestroyed(WebApp.java:2700)
      at com.ibm.ws.webcontainer.webapp.WebApp.destroy(WebApp.java:4036)
      at com.ibm.ws.webcontainer.osgi.webapp.WebApp.destroy(WebApp.java:1470)
      at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.stopWebApp(DynamicVirtualHost.java:510)
      at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApp(DynamicVirtualHost.java:488)
      at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApplication(DynamicVirtualHost.java:473)
      at com.ibm.ws.webcontainer.osgi.WebContainer.startWebApplication(WebContainer.java:1206)
      at com.ibm.ws.webcontainer.osgi.WebContainer.access$100(WebContainer.java:112)
      at com.ibm.ws.webcontainer.osgi.WebContainer$3.run(WebContainer.java:994)
      at com.ibm.ws.threading.internal.ExecutorServiceImpl$RunnableWrapper.run(ExecutorServiceImpl.java:247)
      at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
      at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
      at java.base/java.lang.Thread.run(Thread.java:857)
      {{}}

      Show
      Using Open Liberty: Create an app with a ServletContainerInitializer which throws an Error from its onStartup method. Ensure the app contains a CDI bean Ensure the server config contains both the servlet and cdi features Deploy the app and start the server The following error is seen in the log: [24/08/2023, 16:17:17:872 BST] 0000003f com.ibm.ws.webcontainer.webapp E SRVE0285E: Exception caught while destroying context: java.lang.NullPointerException: Cannot invoke "org.jboss.weld.module.web.servlet.HttpContextLifecycle.contextDestroyed(jakarta.servlet.ServletContext)" because "this.lifecycle" is null at org.jboss.weld.module.web.servlet.WeldInitialListener.contextDestroyed(WeldInitialListener.java:113) at com.ibm.ws.cdi.web.factories.Weld50InitialListener.contextDestroyed(Weld50InitialListener.java:51) at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextDestroyed(WebApp.java:2700) at com.ibm.ws.webcontainer.webapp.WebApp.destroy(WebApp.java:4036) at com.ibm.ws.webcontainer.osgi.webapp.WebApp.destroy(WebApp.java:1470) at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.stopWebApp(DynamicVirtualHost.java:510) at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApp(DynamicVirtualHost.java:488) at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApplication(DynamicVirtualHost.java:473) at com.ibm.ws.webcontainer.osgi.WebContainer.startWebApplication(WebContainer.java:1206) at com.ibm.ws.webcontainer.osgi.WebContainer.access$100(WebContainer.java:112) at com.ibm.ws.webcontainer.osgi.WebContainer$3.run(WebContainer.java:994) at com.ibm.ws.threading.internal.ExecutorServiceImpl$RunnableWrapper.run(ExecutorServiceImpl.java:247) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:857) {{}}

      If something goes wrong early in the startup of a web app such that WeldInitialListener.contextInitialized is not called, it will throw a NullPointerException if WeldInitialListener.contextDestroyed is called.

      This can only happen if the servlet container calls contextDestroyed without first calling contextInitialized, but we couldn't find anything in the servlet spec that gives a guarantee that this won't happen.

      It also doesn't cause much harm because this only occurs if the app fails to start, but it does cause confusion to users debugging problems because the exception in the logs can look like the cause of the application startup failure rather than a symptom of it.

      Can we add a check to WeldInitialListener.contextDestroyed to make sure lifecycle has been initialized before calling it?

      Related Open Liberty issue: https://github.com/OpenLiberty/open-liberty/issues/26054

              manovotn Matěj Novotný
              azquelt Andrew Rouse (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: