[Red5commits] [2395] fixed potential memory leak with RTMPT connections that are not properly closed

jbauch luke at codegent.com
Mon Oct 15 09:10:09 PDT 2007


fixed potential memory leak with RTMPT connections that are not properly closed (Jira APPSERVER-193)


Timestamp: 10/15/07 11:03:12 EST (less than one hour ago) 
Change: 2395 
Author: jbauch

Files (see diff or trac for details): 
doc/trunk/changelog.txt
java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTConnection.java
java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTServlet.java


Trac: http://mirror1.cvsdude.com/trac/osflash/red5/changeset/2395

Index: /java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTConnection.java
===================================================================
--- /java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTConnection.java (revision 2219)
+++ /java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTConnection.java (revision 2395)
@@ -120,5 +120,9 @@
      */
 	protected int clientId;
-
+	/**
+	 * Servlet that created this connection.
+	 */
+	protected RTMPTServlet servlet;
+	
 	/** Constructs a new RTMPTConnection. */
     RTMPTConnection() {
@@ -150,4 +154,13 @@
 	}
 
+    /**
+     * Set the servlet that created the connection.
+     * 
+     * @param servlet
+     */
+    protected void setServlet(RTMPTServlet servlet) {
+    	this.servlet = servlet;
+    }
+    
 	/**
      * Getter for property 'closing'.
@@ -177,4 +190,8 @@
 		}
 		pendingMessages.clear();
+		if (servlet != null) {
+			servlet.notifyClosed(this);
+			servlet = null;
+		}
 	}
 
Index: /java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTServlet.java
===================================================================
--- /java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTServlet.java (revision 2272)
+++ /java/server/trunk/src/org/red5/server/net/rtmpt/RTMPTServlet.java (revision 2395)
@@ -21,6 +21,7 @@
 
 import java.io.IOException;
-import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.servlet.ServletException;
@@ -73,9 +74,8 @@
 	private static final int RESPONSE_TARGET_SIZE = 32768;
 
-	// TODO: we need to check the map periodically for disconnected clients
 	/**
 	 * Holds a map of client id -> client object.
 	 */
-	protected HashMap<Integer, RTMPTConnection> rtmptClients = new HashMap<Integer, RTMPTConnection>();
+	protected Map<Integer, RTMPTConnection> rtmptClients = new ConcurrentHashMap<Integer, RTMPTConnection>();
 
 	/**
@@ -224,6 +224,6 @@
 	 */
 	protected RTMPTConnection getClient(HttpServletRequest req) {
-		Integer id = getClientId(req);
-		if (id == null || !rtmptClients.containsKey(id)) {
+		final Integer id = getClientId(req);
+		if (id == null) {
 			if (log.isDebugEnabled()) {
 				log.debug("Unknown client id: " + id);
@@ -231,5 +231,14 @@
 			return null;
 		}
-		return rtmptClients.get(id);
+		
+		final RTMPTConnection result = rtmptClients.get(id);
+		if (result == null) {
+			if (log.isDebugEnabled()) {
+				log.debug("Unknown client id: " + id);
+			}
+			return null;
+		}
+		
+		return result;
 	}
 
@@ -297,4 +306,5 @@
 		// TODO: should we evaluate the pathinfo?
 		RTMPTConnection client = handler.createRTMPTConnection();
+		client.setServlet(this);
 		if (client.getId() == 0) {
 			// no more clients are available for serving
@@ -494,3 +504,23 @@
 	}
 
+	/** {@inheritDoc} */
+	@Override
+	public void destroy() {
+		// Cleanup connections
+		for (RTMPTConnection conn: rtmptClients.values()) {
+			conn.close();
+		}
+		rtmptClients.clear();
+		super.destroy();
+	}
+
+	/**
+	 * A connection has been closed that was created by this servlet.
+	 * 
+	 * @param conn
+	 */
+	protected void notifyClosed(RTMPTConnection conn) {
+		rtmptClients.remove(conn.getId());
+	}
+	
 }
Index: /doc/trunk/changelog.txt
===================================================================
--- /doc/trunk/changelog.txt (revision 2371)
+++ /doc/trunk/changelog.txt (revision 2395)
@@ -12,4 +12,6 @@
 Bugfixes:
 - pause near end of buffered streams works as expected (Jira APPSERVER-199)
+- fixed potential memory leak with RTMPT connections that are not properly
+  closed (Jira APPSERVER-193)
 
 


Note:
Diffs are chopped if more than 25k.
This is to get past the limit on the mailing list.



More information about the Red5commits mailing list