From 3706b3cc5c601dfb2b14da9550e460226235e326 Mon Sep 17 00:00:00 2001 From: ganfra Date: Tue, 19 Sep 2023 16:10:27 +0200 Subject: [PATCH 1/2] Fix room member infinite loop --- .../matrix/impl/room/RustMatrixRoom.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt index 5a2862546a..1ed21d4347 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt @@ -44,8 +44,8 @@ import io.element.android.libraries.matrix.api.timeline.item.event.EventType import io.element.android.libraries.matrix.impl.core.toProgressWatcher import io.element.android.libraries.matrix.impl.media.MediaUploadHandlerImpl import io.element.android.libraries.matrix.impl.media.map -import io.element.android.libraries.matrix.impl.poll.toInner import io.element.android.libraries.matrix.impl.notificationsettings.RustNotificationSettingsService +import io.element.android.libraries.matrix.impl.poll.toInner import io.element.android.libraries.matrix.impl.room.location.toInner import io.element.android.libraries.matrix.impl.timeline.RustMatrixTimeline import io.element.android.libraries.matrix.impl.util.destroyAll @@ -187,21 +187,25 @@ class RustMatrixRoom( _membersStateFlow.value = MatrixRoomMembersState.Pending(prevRoomMembers = currentMembers) var rustMembers: List? = null try { - rustMembers = buildList { - while (true) { - // Loading the whole iterator as a stop-gap measure. - // We should probably implement some sort of paging in the future. - addAll(innerRoom.members().nextChunk(1000u) ?: break) + rustMembers = innerRoom.members().use { membersIterator -> + buildList { + while (true) { + // Loading the whole membersIterator as a stop-gap measure. + // We should probably implement some sort of paging in the future. + addAll(membersIterator.nextChunk(1000u) ?: break) + } } } val mappedMembers = rustMembers.parallelMap(RoomMemberMapper::map) _membersStateFlow.value = MatrixRoomMembersState.Ready(mappedMembers) Result.success(Unit) - } catch (cancellationException: CancellationException) { - throw cancellationException } catch (exception: Exception) { _membersStateFlow.value = MatrixRoomMembersState.Error(prevRoomMembers = currentMembers, failure = exception) - Result.failure(exception) + if (exception is CancellationException) { + throw exception + } else { + Result.failure(exception) + } } finally { rustMembers?.destroyAll() } @@ -466,7 +470,7 @@ class RustMatrixRoom( } private fun messageEventContentFromParts(body: String, htmlBody: String?): RoomMessageEventContentWithoutRelation = - if(htmlBody != null) { + if (htmlBody != null) { messageEventContentFromHtml(body, htmlBody) } else { messageEventContentFromMarkdown(body) From 85125aa854046c2f330c02fefd75cbcb60a338cd Mon Sep 17 00:00:00 2001 From: ganfra Date: Tue, 19 Sep 2023 18:06:56 +0200 Subject: [PATCH 2/2] Fix warning and yield --- .../libraries/matrix/impl/room/RustMatrixRoom.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt index 1ed21d4347..fea5058db5 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt @@ -59,6 +59,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.withContext +import kotlinx.coroutines.yield import org.matrix.rustcomponents.sdk.RequiredState import org.matrix.rustcomponents.sdk.Room import org.matrix.rustcomponents.sdk.RoomListItem @@ -192,6 +193,7 @@ class RustMatrixRoom( while (true) { // Loading the whole membersIterator as a stop-gap measure. // We should probably implement some sort of paging in the future. + yield() addAll(membersIterator.nextChunk(1000u) ?: break) } } @@ -199,13 +201,12 @@ class RustMatrixRoom( val mappedMembers = rustMembers.parallelMap(RoomMemberMapper::map) _membersStateFlow.value = MatrixRoomMembersState.Ready(mappedMembers) Result.success(Unit) + } catch (exception: CancellationException) { + _membersStateFlow.value = MatrixRoomMembersState.Error(prevRoomMembers = currentMembers, failure = exception) + throw exception } catch (exception: Exception) { _membersStateFlow.value = MatrixRoomMembersState.Error(prevRoomMembers = currentMembers, failure = exception) - if (exception is CancellationException) { - throw exception - } else { - Result.failure(exception) - } + Result.failure(exception) } finally { rustMembers?.destroyAll() }