From 8a01911f7a535f93c3eef3c33eef0bed93a8d56b Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Mon, 13 Nov 2023 13:09:03 +0100 Subject: [PATCH] Fix race condition when updating provider and ownership at the same time --- .../ui/serviceconnection/RelayListListener.kt | 7 +++++++ .../mullvadvpn/usecase/RelayListFilterUseCase.kt | 7 +++++++ .../kotlin/net/mullvad/mullvadvpn/lib/ipc/Request.kt | 6 ++++++ .../mullvadvpn/service/endpoint/RelayListListener.kt | 11 +++++++++++ 4 files changed, 31 insertions(+) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/RelayListListener.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/RelayListListener.kt index 30b8540bf986..b581a3b935e3 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/RelayListListener.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/RelayListListener.kt @@ -50,5 +50,12 @@ class RelayListListener( messageHandler.trySendRequest(Request.SetProviders(value)) } + fun updateSelectedOwnershipAndProviderFilter( + ownership: Constraint, + providers: Constraint + ) { + messageHandler.trySendRequest(Request.SetOwnershipAndProviders(ownership, providers)) + } + private fun defaultRelayList() = RelayList(ArrayList(), WireguardEndpointData(ArrayList())) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/RelayListFilterUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/RelayListFilterUseCase.kt index a26f302f9ca4..a9c1fd5d24c2 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/RelayListFilterUseCase.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/RelayListFilterUseCase.kt @@ -23,6 +23,13 @@ class RelayListFilterUseCase( relayListListener.updateSelectedProvidersFilter(providers) } + fun updateOwnershipAndProviderFilter( + ownership: Constraint, + providers: Constraint + ) { + relayListListener.updateSelectedOwnershipAndProviderFilter(ownership, providers) + } + fun selectedOwnership(): Flow> = settingsRepository.settingsUpdates.map { settings -> settings?.relaySettings?.relayConstraints()?.ownership ?: Constraint.Any() diff --git a/android/lib/ipc/src/main/kotlin/net/mullvad/mullvadvpn/lib/ipc/Request.kt b/android/lib/ipc/src/main/kotlin/net/mullvad/mullvadvpn/lib/ipc/Request.kt index b73010785a5d..ced3f85486d1 100644 --- a/android/lib/ipc/src/main/kotlin/net/mullvad/mullvadvpn/lib/ipc/Request.kt +++ b/android/lib/ipc/src/main/kotlin/net/mullvad/mullvadvpn/lib/ipc/Request.kt @@ -109,6 +109,12 @@ sealed class Request : Message.RequestMessage() { @Parcelize data object FetchRelayList : Request() + @Parcelize + data class SetOwnershipAndProviders( + val ownership: Constraint, + val providers: Constraint + ) : Request() + companion object { private const val MESSAGE_KEY = "request" diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt index 186ac21092cf..5a572ad43de5 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt @@ -87,6 +87,17 @@ class RelayListListener( relayList = daemon.await().getRelayLocations() } } + + scope.launch { + endpoint.dispatcher.parsedMessages + .filterIsInstance() + .collect { request -> + val update = + getCurrentRelayConstraints() + .copy(ownership = request.ownership, providers = request.providers) + daemon.await().setRelaySettings(RelaySettings.Normal(update)) + } + } } fun onDestroy() {