Skip to content

Commit

Permalink
bugfix: removed class loader leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lprimak committed May 7, 2024
1 parent e5c074a commit 9b6fdb0
Show file tree
Hide file tree
Showing 20 changed files with 246 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target/
.vscode/
.idea
.DS_Store
.sdkmanrc
gfbuild.log
nb-configuration.xml
/appserver/tests/quicklook/classes/
Expand All @@ -26,4 +27,4 @@ appserver/tests/quicklook/quicklook_summary.txt
**/nbproject
.flattened-pom.xml
**/__pycache__
appserver/packager/legal/src/main/resources/glassfish/legal/3RD-PARTY-LICENSE.txt
appserver/packager/legal/src/main/resources/glassfish/legal/3RD-PARTY-LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2021] Payara Foundation and/or affiliates
// Portions Copyright [2019-2024] Payara Foundation and/or affiliates

package com.sun.ejb;

Expand Down Expand Up @@ -269,7 +269,7 @@ public class EjbInvocation //

EjbInvocation(String compEnvId, Container container) {
super.componentId = compEnvId;
super.container = container;
setContainer(container);
super.setComponentInvocationType(ComponentInvocation.ComponentInvocationType.EJB_INVOCATION);

EjbBundleDescriptor ejbBundleDesc = container.getEjbDescriptor().getEjbBundleDescriptor();
Expand Down Expand Up @@ -371,6 +371,7 @@ public EjbInvocation clone() {
@Override
public Object getJaccEjb() {
Object bean = null;
Object container = getContainer();
if( container != null ) {
bean = ((Container) container).getJaccEjb(this);
}
Expand Down Expand Up @@ -452,7 +453,7 @@ public void setTransactionOperationsManager(TransactionOperationsManager transac
*/
@Override
public boolean userTransactionMethodsAllowed() {
return ((Container) container).userTransactionMethodsAllowed(this);
return ((Container) getContainer()).userTransactionMethodsAllowed(this);
}

/**
Expand All @@ -461,15 +462,15 @@ public boolean userTransactionMethodsAllowed() {
*/
@Override
public void userTransactionLookupAllowed() throws NameNotFoundException {
((BaseContainer) container).checkUserTransactionLookup(this);
((BaseContainer) getContainer()).checkUserTransactionLookup(this);
}

/**
* Called by the UserTransaction when transaction is started.
*/
@Override
public void doAfterUtxBegin() {
((Container) container).doAfterBegin(this);
((Container) getContainer()).doAfterBegin(this);
}

public InterceptorManager.InterceptorChain getInterceptorChain() {
Expand Down Expand Up @@ -650,11 +651,11 @@ public Object[] getInterceptorInstances() {

@Override
public Object invokeBeanMethod() throws Throwable {
return ((BaseContainer) container).invokeBeanMethod(this);
return ((BaseContainer) getContainer()).invokeBeanMethod(this);
}

public com.sun.enterprise.security.SecurityManager getEjbSecurityManager() {
return ((BaseContainer)container).getSecurityManager();
return ((BaseContainer) getContainer()).getSecurityManager();
}

@Override
Expand Down Expand Up @@ -684,7 +685,7 @@ public boolean authorizeWebService(Method m) throws Exception {
private Exception authorizeWebServiceAndSetMethod(Method m) {
try {
this.method = m;
if (((com.sun.ejb.Container) container).authorize(this)) {
if (((com.sun.ejb.Container) getContainer()).authorize(this)) {
// Record the method on which the successful
// authorization check was performed.
setWebServiceMethod(m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018-2020] [Payara Foundation and/or its affiliates]
// Portions Copyright [2018-2024] [Payara Foundation and/or its affiliates]

package com.sun.ejb;

Expand All @@ -58,7 +58,7 @@ public EjbInvocationFactory(String compEnvId, Container container) {

public EjbInvocation create() {
EjbInvocation ejbInv = new EjbInvocation(compEnvId, container);
ejbInv.jndiEnvironment = container.getEjbDescriptor();
ejbInv.setJNDIEnvironment(container.getEjbDescriptor());
return ejbInv;
}

Expand All @@ -67,7 +67,7 @@ public <C extends ComponentContext> EjbInvocation create(Object ejb, C ctx) {
ejbInv.ejb = ejb;
ejbInv.instance = ejb;
ejbInv.context = ctx;
ejbInv.jndiEnvironment = container.getEjbDescriptor();
ejbInv.setJNDIEnvironment(container.getEjbDescriptor());

return ejbInv;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2021] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]

package com.sun.ejb.containers;

Expand Down Expand Up @@ -2002,7 +2002,7 @@ public void preInvoke(EjbInvocation inv) {
}

inv.transactionAttribute = inv.invocationInfo.txAttr;
inv.container = this;
inv.setContainer(this);

if (inv.mustInvokeAsynchronously()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]

package com.sun.ejb.containers;

Expand Down Expand Up @@ -158,7 +159,7 @@ public void cleanupContainerTasks(Container container) {
Map.Entry<Long, EjbFutureTask> next = iterator.next();

EjbAsyncTask task = next.getValue().getEjbAsyncTask();
if( task.getEjbInvocation().container == container ) {
if( task.getEjbInvocation().getContainer() == container ) {

removedTasks.add(task.getInvId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2024] Payara Foundation and/or affiliates

package com.sun.ejb.containers;

Expand Down Expand Up @@ -88,7 +89,7 @@ EjbInvocation getEjbInvocation() {
public V call()
throws Exception {
V returnValue = null;
BaseContainer container = (BaseContainer) inv.container;
BaseContainer container = (BaseContainer) inv.getContainer();
ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
try {
Utility.setContextClassLoader(container.getClassLoader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2024] Payara Foundation and/or affiliates

package com.sun.ejb.containers;

Expand Down Expand Up @@ -89,7 +90,7 @@ public ComponentInvocation startInvocation() {

// Do the portions of preInvoke that don't need a Method object.
inv.isWebService = true;
inv.container = container_;
inv.setContainer(container_);
inv.transactionAttribute = Container.TX_NOT_INITIALIZED;

// AS per latest spec change, the MessageContext object in WebSvcCtxt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2017-2021] [Payara Foundation and/or its affiliates]
// Portions Copyright [2017-2024] [Payara Foundation and/or its affiliates]
package org.glassfish.ejb.mdb;

import com.sun.appserv.connectors.internal.api.ConnectorRuntime;
Expand Down Expand Up @@ -216,7 +216,7 @@ public final class MessageBeanContainer extends BaseContainer implements Message
messageBeanClient_ = clientFactory.createMessageBeanClient(msgBeanDesc);

componentInvocation = createComponentInvocation();
componentInvocation.container = this;
componentInvocation.setContainer(this);
invocationManager.preInvoke(componentInvocation);
messageBeanClient_.setup(this);

Expand Down Expand Up @@ -1135,7 +1135,7 @@ public void beforeMessageDelivery(Method method, MessageDeliveryType deliveryTyp
invocation.context = context;
invocation.instance = context.getEJB();
invocation.ejb = context.getEJB();
invocation.container = this;
invocation.setContainer(this);

// Message Bean Container only starts a new transaction if
// there is no imported transaction and the message listener
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2023] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2023-2024] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -79,6 +79,12 @@ public void postConstruct() {

@Override
public void onEvent(final ApplicationEvent event) {
switch (event.getType()) {
case DESTROY_FINISHED:
case RELOAD_FINISHED:
openTracingHelper.canTraceCache.clear(event.getResourceConfig().getClassLoader());
break;
}
LOG.config(() -> "onEvent(event.type=" + event.getType() + ")");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2023 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2023-2024 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -368,7 +368,7 @@ private Traced computeTracedAnnotation(ResourceInfo resourceInfo, BeanManager be
return result == null ? NULL_TRACED : result;
}

private static ResourceCache<Boolean> canTraceCache = new ResourceCache<>();
static ResourceCache<Boolean> canTraceCache = new ResourceCache<>();
/**
* Helper method that checks if any specified skip patterns match this method name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2023 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2023-2024 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -47,7 +47,6 @@
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import org.eclipse.microprofile.opentracing.Traced;

class ResourceCache<T> {

Expand Down Expand Up @@ -83,4 +82,12 @@ public int hashCode() {
T get(ResourceInfo info, Supplier<T> supplier) {
return tracedCache.computeIfAbsent(new ResourceKey(info), k -> supplier.get());
}

/**
* clear all classes belonging to this class loader
* @param classLoader
*/
void clear(ClassLoader classLoader) {
tracedCache.entrySet().removeIf(entry -> entry.getKey().resourceClass.getClassLoader() == classLoader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2024] Payara Foundation and/or affiliates

package org.glassfish.persistence.ejb.entitybean.container;

Expand Down Expand Up @@ -1284,7 +1285,7 @@ private void internalRemoveBeanUnchecked(EJBLocalRemoteObject localRemoteObj, bo
context.incrementCalls();

inv.instance = inv.ejb = context.getEJB();
inv.container = this;
inv.setContainer(this);
invocationManager.preInvoke(inv);

// call ejbLoad if necessary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2016-2021] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2016-2024] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -214,7 +214,7 @@ private InstanceImpl(ComponentInvocation currentInvocation) {
} else if (currentInvocation.getJNDIEnvironment() instanceof JndiNameEnvironment) {
componentId = DOLUtils.toEarComponentId(
DOLUtils.getApplicationName((JndiNameEnvironment)
currentInvocation.jndiEnvironment));
currentInvocation.getJNDIEnvironment()));
isApplicationComponent = true;
} else {
// checkState() later should error out due to this condition
Expand Down
Loading

0 comments on commit 9b6fdb0

Please sign in to comment.