[Android] Fix ConcurrentModification exceptions on the remote access server

Nicolas Pomepuy git at videolan.org
Thu Apr 25 12:22:35 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Apr 25 07:18:04 2024 +0200| [cd537aeffe568af1b1fd0170db1d8b13147bdc89] | committer: Duncan McNamara

Fix ConcurrentModification exceptions on the remote access server

> https://code.videolan.org/videolan/vlc-android/commit/cd537aeffe568af1b1fd0170db1d8b13147bdc89
---

 .../vlc/webserver/websockets/RemoteAccessWebSockets.kt   | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt
index 4be4d0f4fb..b08c0e6fde 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/websockets/RemoteAccessWebSockets.kt
@@ -198,22 +198,24 @@ object RemoteAccessWebSockets {
 
    suspend fun sendToAll(message: String) {
        if (BuildConfig.DEBUG) Log.d(TAG, "WebSockets: sendToAll called on ${websocketSession.size} sessions with message '$message'")
+       val iterator = ArrayList(websocketSession).iterator()
        val toRemove = hashSetOf<DefaultWebSocketServerSession>()
-       websocketSession.forEach {
+       while (iterator.hasNext()) {
+           val connection = iterator.next()
            try {
-               it.send(Frame.Text(message))
+               connection.send(Frame.Text(message))
            } catch (e: Exception) {
-               toRemove.add(it)
+               toRemove.add(connection)
            }
        }
        websocketSession.removeAll(toRemove)
    }
 
     suspend fun closeAllSessions() {
-        with(websocketSession.iterator()) {
-            forEach {
-                it.close()
-            }
+        val iterator = ArrayList(websocketSession).iterator()
+        while (iterator.hasNext()) {
+            val connection = iterator.next()
+            connection.close()
         }
     }
 }
\ No newline at end of file



More information about the Android mailing list