app: release the installer lock when cancelled

MagiskInstaller.exec() only cleared its static session flag after the
operation returned normally. When the calling coroutine was cancelled,
withContext() threw once the blocking operation finished and the flag
stayed set, so every later install, patch, uninstall, restore or
environment fix failed immediately until the app process died.

Restore images and Fix environment ran in a composition scope through
rememberLoadingDialog(), so this happened just by rotating while their
loading dialog was showing, which also lost the result toast and the
reboot after fixing the environment. Leaving a running flash screen
triggered it too.

Release the flag in a finally block, and run both operations in
HomeViewModel, which survives configuration changes. Remove
rememberLoadingDialog(), which has no other users.

Assisted-by: Claude Opus 5.5
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 18112c7366
commit 7dca458185
4 changed files with 54 additions and 68 deletions
@@ -34,7 +34,6 @@ import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -57,7 +56,6 @@ import com.topjohnwu.magisk.core.di.ServiceLocator
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.onEach
@@ -117,10 +115,6 @@ private class ConfirmDialogState(
}
}
interface LoadingDialogHandle {
suspend fun <R> withLoading(block: suspend () -> R): R
}
interface ConfirmDialogHandle {
fun showConfirm(
title: String,
@@ -139,22 +133,6 @@ interface ConfirmDialogHandle {
): ConfirmResult
}
private class LoadingDialogHandleImpl(
private val visible: MutableState<Boolean>,
private val coroutineScope: CoroutineScope
) : LoadingDialogHandle {
override suspend fun <R> withLoading(block: suspend () -> R): R {
return coroutineScope.async {
try {
visible.value = true
block()
} finally {
visible.value = false
}
}.await()
}
}
private class ConfirmDialogHandleImpl(
private val state: ConfirmDialogState,
private val coroutineScope: CoroutineScope,
@@ -238,14 +216,6 @@ fun rememberConfirmCallback(
}
}
@Composable
fun rememberLoadingDialog(): LoadingDialogHandle {
val visible = remember { mutableStateOf(false) }
val scope = rememberCoroutineScope()
LoadingDialog(visible = visible.value)
return remember { LoadingDialogHandleImpl(visible, scope) }
}
@Composable
fun rememberConfirmDialog(
onConfirm: (() -> Unit)? = null,
@@ -2,7 +2,6 @@ package com.topjohnwu.magisk.ui.home
import android.os.Build
import android.os.PowerManager
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
@@ -57,7 +56,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
@@ -81,23 +79,17 @@ import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.DownloadEngine
import com.topjohnwu.magisk.core.download.Subject
import com.topjohnwu.magisk.core.ktx.reboot
import com.topjohnwu.magisk.core.ktx.toast
import com.topjohnwu.magisk.core.tasks.MagiskInstaller
import com.topjohnwu.magisk.ui.MainActivity
import com.topjohnwu.magisk.ui.component.AdaptiveColumns
import com.topjohnwu.magisk.ui.component.LoadingDialog
import com.topjohnwu.magisk.ui.component.MagiskDialog
import com.topjohnwu.magisk.ui.component.MarkdownTextAsync
import com.topjohnwu.magisk.ui.component.onlyHorizontal
import com.topjohnwu.magisk.ui.component.rememberLoadingDialog
import com.topjohnwu.magisk.ui.component.verticalScrollbar
import com.topjohnwu.magisk.ui.install.InstallDialog
import com.topjohnwu.magisk.ui.install.InstallViewModel
import com.topjohnwu.magisk.ui.navigation.LocalNavigator
import com.topjohnwu.magisk.ui.navigation.Route
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import com.topjohnwu.magisk.core.R as CoreR
@@ -112,8 +104,6 @@ fun HomeScreen(
val context = LocalContext.current
val navigator = LocalNavigator.current
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
val scope = rememberCoroutineScope()
val loadingDialog = rememberLoadingDialog()
var showUninstallDialog by rememberSaveable { mutableStateOf(false) }
var showManagerDialog by rememberSaveable { mutableStateOf(false) }
@@ -141,6 +131,8 @@ fun HomeScreen(
}
}
LoadingDialog(visible = uiState.showLoading)
if (showUninstallDialog) {
UninstallComposableDialog(
onDismiss = { showUninstallDialog = false },
@@ -151,15 +143,7 @@ fun HomeScreen(
},
onRestoreImage = {
showUninstallDialog = false
scope.launch {
val success = loadingDialog.withLoading {
MagiskInstaller.Restore().exec()
}
context.toast(
if (success) CoreR.string.restore_done else CoreR.string.restore_fail,
Toast.LENGTH_SHORT
)
}
viewModel.restoreImages()
}
)
}
@@ -187,22 +171,7 @@ fun HomeScreen(
},
onFixEnv = {
showEnvFixDialog = false
scope.launch {
val success = loadingDialog.withLoading {
MagiskInstaller.FixEnv().exec()
}
context.toast(
if (success) CoreR.string.reboot_delay_toast else CoreR.string.setup_fail,
Toast.LENGTH_LONG
)
if (success) {
// The reboot toast has been shown; don't let leaving this screen cancel it
withContext(NonCancellable) {
delay(5000)
reboot()
}
}
}
viewModel.fixEnv()
}
)
}
@@ -11,15 +11,20 @@ import com.topjohnwu.magisk.core.BuildConfig
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.ktx.await
import com.topjohnwu.magisk.core.ktx.reboot
import com.topjohnwu.magisk.core.ktx.toast
import com.topjohnwu.magisk.core.repository.NetworkService
import com.topjohnwu.magisk.core.tasks.MagiskInstaller
import com.topjohnwu.magisk.utils.asFlow
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import com.topjohnwu.magisk.core.R as CoreR
class HomeViewModel(
@@ -38,6 +43,7 @@ class HomeViewModel(
val showUninstall: Boolean = false,
val showManagerInstall: Boolean = false,
val envFixCode: Int = 0,
val showLoading: Boolean = false,
val magiskState: State = computeMagiskState(),
val magiskInstalledVersion: String = computeMagiskInstalledVersion(),
val managerInstalledVersion: String = computeManagerInstalledVersion(),
@@ -137,6 +143,42 @@ class HomeViewModel(
_uiState.update { it.copy(envFixCode = 0) }
}
// Root operations run in the ViewModel, so a configuration change can't cancel them halfway
// and lose their result
fun restoreImages() {
viewModelScope.launch {
val success = withLoading { MagiskInstaller.Restore().exec() }
showToast(if (success) CoreR.string.restore_done else CoreR.string.restore_fail)
}
}
fun fixEnv() {
viewModelScope.launch {
val success = withLoading { MagiskInstaller.FixEnv().exec() }
AppContext.toast(
if (success) CoreR.string.reboot_delay_toast else CoreR.string.setup_fail,
Toast.LENGTH_LONG
)
if (success) {
// The reboot toast has been shown; don't let leaving the app cancel it
withContext(NonCancellable) {
delay(5000)
reboot()
}
}
}
}
private suspend fun <R> withLoading(block: suspend () -> R): R {
_uiState.update { it.copy(showLoading = true) }
try {
return block()
} finally {
_uiState.update { it.copy(showLoading = false) }
}
}
fun hideNotice() {
Config.safetyNotice = false
_uiState.update { it.copy(isNoticeVisible = false) }
@@ -506,8 +506,13 @@ abstract class MagiskInstallImpl protected constructor(
if (haveActiveSession.getAndSet(true))
return false
val result = withContext(Dispatchers.IO) { operations() }
// Always release the session: if the caller is cancelled, withContext throws once the
// operations finish, and a stuck session would make every later operation fail
val result = try {
withContext(Dispatchers.IO) { operations() }
} finally {
haveActiveSession.set(false)
}
if (result)
return true