From f899f658c098b7d5f0f0430e645c91665a8c8e9e Mon Sep 17 00:00:00 2001 From: lprimak Date: Sat, 3 Aug 2024 00:00:07 -0500 Subject: [PATCH 1/2] bugfix: prevent deployment failures from causing an undeployment failure, and leaking ClassLoader resources in the process --- .../apache/catalina/core/StandardContext.java | 30 ++++++++++++++++--- .../com/sun/enterprise/web/WebContainer.java | 7 +++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java b/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java index a84ece1ec7a..9a18734b473 100644 --- a/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java +++ b/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java @@ -2303,9 +2303,9 @@ protected void addChild(Container child, boolean isProgrammatic, Wrapper oldJspServlet = null; // Allow webapp to override JspServlet inherited from global web.xml. - boolean isJspServlet = "jsp".equals(wrapperName); + boolean isJspServlet = Constants.JSP_SERVLET_NAME.equals(wrapperName); if (isJspServlet) { - oldJspServlet = (Wrapper) findChild("jsp"); + oldJspServlet = (Wrapper) findChild(Constants.JSP_SERVLET_NAME); if (oldJspServlet != null) { removeChild(oldJspServlet); } @@ -3238,7 +3238,7 @@ public void addInstanceListener(InstanceListener listener) { public void addJspMapping(String pattern) { String servletName = findServletMapping("*.jsp"); if (servletName == null) { - servletName = "jsp"; + servletName = Constants.JSP_SERVLET_NAME; } if( findChild(servletName) != null) { @@ -5766,6 +5766,8 @@ public synchronized void start() throws LifecycleException { } catch (Throwable t) { log.log(Level.SEVERE, LogFacade.STARTUP_CONTEXT_FAILED_EXCEPTION, getName()); try { + // ensure that all JSP resources are released in stop() method below + forceLoadJspServlet(); stop(); } catch (Throwable tt) { log.log(Level.SEVERE, LogFacade.CLEANUP_FAILED_EXCEPTION, tt); @@ -5951,7 +5953,11 @@ public synchronized void stop(boolean isShutdown) if ((manager != null) && (manager instanceof Lifecycle)) { if(manager instanceof StandardManager) { - ((StandardManager)manager).stop(isShutdown); + try { + ((StandardManager)manager).stop(isShutdown); + } catch (LifecycleException e) { + log.log(Level.INFO, LogFacade.MANAGER_NOT_STARTED_INFO, e); + } } else { ((Lifecycle)manager).stop(); } @@ -8132,4 +8138,20 @@ private File getExtractedMetaInfResourcePath(String path) { } return null; } + + + /** + * Force loading of the JSP servlet. This is needed in case of + * initialization failure, so the JSP servlet would be unloaded properly + * and release all of its resources + * + * @throws ServletException + */ + private void forceLoadJspServlet() throws ServletException { + Container jspServlet = findChild(Constants.JSP_SERVLET_NAME); + if (jspServlet instanceof Wrapper) { + Wrapper jspServletWrapper = (Wrapper) jspServlet; + jspServletWrapper.load(); + } + } } diff --git a/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/WebContainer.java b/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/WebContainer.java index cc27e187be2..36d3dc4587c 100644 --- a/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/WebContainer.java +++ b/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/WebContainer.java @@ -37,7 +37,7 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2016-2022] [Payara Foundation and/or its affiliates] +// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates] package com.sun.enterprise.web; @@ -232,6 +232,9 @@ public class WebContainer implements org.glassfish.api.container.Container, Post @Inject private LoggingRuntime loggingRuntime; + @Inject + private Deployment deployment; + private final Map connectorMap = new HashMap<>(); private EmbeddedWebContainer _embedded; @@ -2058,7 +2061,7 @@ public void unloadWebModule(String contextRoot, String appName, String virtualSe } } - if (!hasBeenUndeployed) { + if (!hasBeenUndeployed && deployment.getCurrentDeploymentContext() == null) { logger.log(Level.SEVERE, LogFacade.UNDEPLOY_ERROR, contextRoot); } } From 1e70b7d0699fb3df72d947c02bfd2af347cb72f6 Mon Sep 17 00:00:00 2001 From: Lenny Primak Date: Wed, 21 Aug 2024 01:09:45 -0500 Subject: [PATCH 2/2] Update appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java Co-authored-by: Andrew Pielage --- .../src/main/java/org/apache/catalina/core/StandardContext.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java b/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java index 9a18734b473..42758ba328c 100644 --- a/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java +++ b/appserver/web/web-core/src/main/java/org/apache/catalina/core/StandardContext.java @@ -5956,7 +5956,7 @@ public synchronized void stop(boolean isShutdown) try { ((StandardManager)manager).stop(isShutdown); } catch (LifecycleException e) { - log.log(Level.INFO, LogFacade.MANAGER_NOT_STARTED_INFO, e); + log.log(Level.INFO, e.getMessage()); } } else { ((Lifecycle)manager).stop();