Skip to content

Instantly share code, notes, and snippets.

@mondain
Created April 23, 2025 17:05
Show Gist options
  • Save mondain/cb66f08dd57dfcf240dfb53818d609db to your computer and use it in GitHub Desktop.
Save mondain/cb66f08dd57dfcf240dfb53818d609db to your computer and use it in GitHub Desktop.
Red5 WsSession leak fix

Change log for WsSession leak in Red5; new version 2.0.16 released 4/23/2025

The leak was primarily seen during regular use of Red5 Pro's RTC client which uses a WebSocket for the initial connection with a switch to DataChannel if accepted / configured in the RTC client during startup.

Classes modified:

  • org.red5.net.websocket.WebSocketConnection - local reference to the connected scope was modified from a WeakReference to the WebSocketScope itself and marked final. In the ctor the scope is also added to the WsSession user properties for consolidated access within Tomcat. Several timeout properties were also added from the latest Tomcat 11.x documentation: BLOCKING_SEND_TIMEOUT our default to 8000ms, ABNORMAL_SESSION_CLOSE_SEND_TIMEOUT our default 10000ms, SESSION_CLOSE_TIMEOUT our default 5000ms. The properties are standard for Tomcat and may be modified at-will via system property override; all values are long type in milliseconds. The maximum idle timeout is now configured to use the largest of the two values given read or write timeout. Lastly, a new method onReceive(WSMessage) was created to prevent a lookup of scope during the receive event handlers which already have a local WebSocketConnection.

  • org.red5.net.websocket.server.DefaultWebSocketEndpoint - In onOpen, the scope lookup was moved from endpoint configuration to the current session value's user properties; a scope lookup in onClose also uses the session. The scope is now only local to the method to prevent any possible cross-thread issues. The new WebSocketConnection.onReceive method replaces the WebSocketScope.onMessage method, no method scoped scope lookup is utilized.

  • org.red5.net.websocket.server.WsHttpUpgradeHandle - Class variables for WebSocketScopeManager and WebSocketScope were moved from init to preInit. References for wsSession, connection, and upgradeInfo are now nulled out in close(CloseReason) as well as the socketWrapper if not already null as well as adding a direct close call on the wrapper. A WebSocketScope lookup in timeoutAsync was removed as scope is local.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment