Fix (potential) crashes when deleting a server (#3392)

* Prevent crash when pausing if server is deleted

* Handle race when deleting

 - Handle a race condition where we delete and finish the activity at the same time during which it might not yet have been fully removed
 - Handle a race condition where a back-up may be restored and a server is deleted on startup, which causes the authentication repository to become unavailable with a minor delay
This commit is contained in:
Joris Pelgröm 2023-03-03 20:39:43 +01:00 committed by GitHub
parent a74aae6851
commit 231622a0ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 11 deletions

View file

@ -27,13 +27,17 @@ abstract class LaunchPresenterBase(
.filter { serverManager.authenticationRepository(it.id).getSessionState() == SessionState.ANONYMOUS }
.forEach { serverManager.removeServer(it.id) }
if (
serverManager.isRegistered() &&
serverManager.authenticationRepository().getSessionState() == SessionState.CONNECTED
) {
resyncRegistration()
view.displayWebview()
} else {
try {
if (
serverManager.isRegistered() &&
serverManager.authenticationRepository().getSessionState() == SessionState.CONNECTED
) {
resyncRegistration()
view.displayWebview()
} else {
view.displayOnBoarding(false)
}
} catch (e: IllegalArgumentException) { // Server was just removed, nothing is added
view.displayOnBoarding(false)
}
}

View file

@ -207,7 +207,11 @@ class SettingsActivity : BaseActivity() {
fun setAppActive(serverId: Int?, active: Boolean) = runBlocking {
serverManager.getServer(serverId ?: ServerManager.SERVER_ID_ACTIVE)?.let {
serverManager.integrationRepository(it.id).setAppActive(active)
try {
serverManager.integrationRepository(it.id).setAppActive(active)
} catch (e: IllegalArgumentException) {
Log.w(TAG, "Cannot set app active $active for server $serverId")
}
}
}

View file

@ -112,7 +112,10 @@ class ServerSettingsPresenterImpl @Inject constructor(
// Remove server anyway, the user wants to delete and we don't need the server for that
}
serverManager.removeServer(serverId)
view.onRemovedServer(success = true, hasAnyRemaining = serverManager.defaultServers.any())
view.onRemovedServer(
success = true,
hasAnyRemaining = serverManager.defaultServers.any { it.id != serverId }
)
} ?: run {
view.onRemovedServer(success = false, hasAnyRemaining = true)
}
@ -178,6 +181,11 @@ class ServerSettingsPresenterImpl @Inject constructor(
}
override fun setAppActive(active: Boolean) = runBlocking {
serverManager.integrationRepository(serverId).setAppActive(active)
try {
serverManager.integrationRepository(serverId).setAppActive(active)
} catch (e: IllegalArgumentException) {
Log.w(TAG, "Cannot set app active $active for server $serverId")
Unit
}
}
}

View file

@ -247,7 +247,12 @@ class WebViewPresenterImpl @Inject constructor(
override fun setAppActive(active: Boolean) = runBlocking {
serverManager.getServer(serverId)?.let {
serverManager.integrationRepository(serverId).setAppActive(active)
try {
serverManager.integrationRepository(serverId).setAppActive(active)
} catch (e: IllegalStateException) {
Log.w(TAG, "Cannot set app active $active for server $serverId")
Unit
}
} ?: Unit
}