This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new e63ed88  WICKET-6965 Memory leak in WicketEndpoint (#505)
e63ed88 is described below

commit e63ed88ef6ba5335194d0839f4cd6f0e1fbba791
Author: Martin Grigorov <[email protected]>
AuthorDate: Wed Mar 23 13:46:24 2022 +0200

    WICKET-6965 Memory leak in WicketEndpoint (#505)
    
    * WICKET-6965 Memory leak in WicketEndpoint
    
    Make WicketEndpoint#REGISTERED_LISTENERS a static field.
    Unregister the listener at Application#onBeforeDestroyed()
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    * WICKET-6965 Memory leak in WicketEndpoint
    
    Drop the usage of AtomicBoolean to track whether an application is
    running or not. Use the same set for 'registered listeners' and `alive
    applications'
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
---
 .../wicket/protocol/ws/javax/WicketEndpoint.java   | 36 ++++++++++------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git 
a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketEndpoint.java
 
b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketEndpoint.java
index 40ba7d4..4214a95 100644
--- 
a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketEndpoint.java
+++ 
b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketEndpoint.java
@@ -22,7 +22,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import jakarta.websocket.CloseReason;
 import jakarta.websocket.Endpoint;
@@ -43,17 +42,18 @@ import org.slf4j.LoggerFactory;
  */
 public class WicketEndpoint extends Endpoint
 {
-
        private static final Logger LOG = 
LoggerFactory.getLogger(WicketEndpoint.class);
 
        /**
+        * A set of started applications for which this endpoint is registered.
+        */
+       private static final Set<String> RUNNING_APPLICATIONS = 
ConcurrentHashMap.newKeySet();
+
+       /**
         * The name of the request parameter that holds the application name
         */
        private static final String WICKET_APP_PARAM_NAME = "wicket-app-name";
 
-       private final AtomicBoolean applicationDestroyed = new 
AtomicBoolean(false);
-       private final Set<String> registeredListeners = 
ConcurrentHashMap.newKeySet();
-
        private JavaxWebSocketProcessor javaxWebSocketProcessor;
 
        @Override
@@ -62,9 +62,9 @@ public class WicketEndpoint extends Endpoint
                String appName = getApplicationName(session);
 
                WebApplication app = (WebApplication) 
WebApplication.get(appName);
-               if (registeredListeners.add(appName))
+               if (RUNNING_APPLICATIONS.add(appName))
                {
-                       app.getApplicationListeners().add(new 
ApplicationListener(applicationDestroyed));
+                       app.getApplicationListeners().add(new 
ApplicationListener());
                }
 
                try
@@ -90,7 +90,8 @@ public class WicketEndpoint extends Endpoint
                LOG.debug("Web Socket connection with id '{}' has been closed 
with code '{}' and reason: {}",
                                session.getId(), closeCode, reasonPhrase);
 
-               if (isApplicationAlive() && javaxWebSocketProcessor != null)
+               String applicationName = getApplicationName(session);
+               if (isApplicationAlive(applicationName) && 
javaxWebSocketProcessor != null)
                {
                        javaxWebSocketProcessor.onClose(closeCode, 
reasonPhrase);
                }
@@ -110,7 +111,8 @@ public class WicketEndpoint extends Endpoint
 
                super.onError(session, t);
 
-               if (isApplicationAlive() && javaxWebSocketProcessor != null)
+               String applicationName = getApplicationName(session);
+               if (isApplicationAlive(applicationName) && 
javaxWebSocketProcessor != null)
                {
                        javaxWebSocketProcessor.onError(t);
                }
@@ -123,8 +125,9 @@ public class WicketEndpoint extends Endpoint
                    (t instanceof IOException && "Broken 
pipe".equals(t.getMessage()));
        }
 
-       private boolean isApplicationAlive() {
-               return applicationDestroyed.get() == false;
+       private boolean isApplicationAlive(String appName)
+       {
+               return RUNNING_APPLICATIONS.contains(appName);
        }
 
        private String getApplicationName(Session session)
@@ -161,17 +164,12 @@ public class WicketEndpoint extends Endpoint
 
        private static class ApplicationListener implements IApplicationListener
        {
-               private final AtomicBoolean applicationDestroyed;
-
-               private ApplicationListener(AtomicBoolean applicationDestroyed)
-               {
-                       this.applicationDestroyed = applicationDestroyed;
-               }
-
                @Override
                public void onBeforeDestroyed(Application application)
                {
-                       applicationDestroyed.set(true);
+                       String appName = application.getName();
+                       RUNNING_APPLICATIONS.remove(appName);
+                       application.getApplicationListeners().remove(this);
                }
        }
 }

Reply via email to