fix(session): serialize launch requests in a coordinator (#3044)

* fix(session): serialize launch requests in a coordinator

Replace the gameLaunchRequest counter and the single pendingGameLink field
with a LaunchCoordinator. A launch is now identified by a ticket, so a stale
callback can only act on its own request, and a game link is superseded by
any newer launch. Launching is also refused while a package install stage
is in progress.

* fix(frontend): a replacement intent supersedes uncommitted launch state

A game link that resolved while the launcher was not resumed stayed pending, so
opening Settings, following a notification or picking a shortcut left it in the
coordinator and the next library refresh launched the old game over the new
screen. The same gap let an ACTION_SETUP intent leave an accepted launch current,
so a late Xbox manifest check could start the game after the user had moved on.

A replacement activity intent now cancels every ticket this activity has not
dispatched, and the refresh path may only finish a link owned by the intent the
activity shows. The toast for a superseded store lookup is gated on its ticket,
and the coordinator test now proves cancelAll() invalidates a link ticket.

The deferred Proton-compat launch is untouched: its ordering still predates this
change and has its own follow-up.
This commit is contained in:
visail authored and GitHub committed 2026-11-24 17:14:35 -05:00
1 parent 7c8f5c2707
commit 9a7cb70969
4 files changed
+402 -36

No files matched your search

@@ -53,6 +53,8 @@ import com.droiddeck.launcher.session.SessionLogShare
import com.droiddeck.launcher.session.SessionPaths
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SteamRepair
import com.droiddeck.launcher.session.LaunchCoordinator
import com.droiddeck.launcher.session.LaunchTicket
import com.droiddeck.launcher.core.CpuCores
import com.droiddeck.launcher.core.DownloadSource
import com.droiddeck.launcher.core.PhantomProcessLimit
@@ -215,7 +217,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
private var soundtracksLoaded by mutableStateOf(false)
private var gameStoresSpeedTier by mutableStateOf("fast")
private var navRequest by mutableStateOf<String?>(null)
private val gameLaunchRequest = java.util.concurrent.atomic.AtomicLong()
private val launchCoordinator = LaunchCoordinator<Intent, com.droiddeck.launcher.frontend.GameRequest>()
private var preparingGames by mutableStateOf(emptySet<String>())
private var storesShowMature by mutableStateOf(false)
private var storesSort by mutableStateOf(SessionPrefs.STORES_SORTS.first())
@@ -897,7 +899,6 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
runCatching { com.droiddeck.launcher.frontend.AutoComponents.restoreMissing(this) }.onFailure { Log.w(TAG, "components: restore failed", it) }
}, "components-restore").start()
}
private var pendingGameLink: com.droiddeck.launcher.frontend.GameRequest? = null
private var gameLinkReading = false
private var gameLinkTried = false
private var gameSyncFolder by mutableStateOf<String?>(null)
@@ -951,7 +952,6 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
else "settings:" + (com.droiddeck.launcher.ui.SettingsSection.of(section) ?: com.droiddeck.launcher.ui.SettingsSection.OVERVIEW).key
request.data = null
gameLinkReading = false
pendingGameLink = null
syncDualPanel()
return
}
@@ -964,7 +964,6 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
steamGames = emptyList()
shortcutLibraryScanning = true
} else shortcutLibraryScanning = false
pendingGameLink = null
gameLinkReading = false
gameLinkTried = false
soundtrackLaunch = false
@@ -977,6 +976,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
return
}
if (GameLaunchIntent.accepts(request.action)) {
val ticket = launchCoordinator.beginLink(request)
gameLinkReading = true
com.droiddeck.launcher.ui.DualScreenPanel.hide(taskId)
val copy = Intent(request)
@@ -985,12 +985,17 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
ui.post {
if (intent === request && !isDestroyed) {
gameLinkReading = false
pendingGameLink = game
if (!launchCoordinator.resolve(ticket, game)) {
clearGameLinkIntent(request)
syncDualPanel()
return@post
}
if (game == null) {
launchCoordinator.consume(ticket)
syncDualPanel()
android.widget.Toast.makeText(this, R.string.game_link_invalid, android.widget.Toast.LENGTH_LONG).show()
} else {
handleGameLink(steamGames, scanned = false)
handleGameLink(steamGames, scanned = false, ticket = ticket)
refresh()
}
}
@@ -999,37 +1004,66 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
}
}
private fun launchGame(game: Library.SteamGame, returnHome: Boolean = false, onHandled: () -> Unit = {}, onSettled: () -> Unit = {}) {
val request = gameLaunchRequest.incrementAndGet()
private fun beginUiLaunch(): LaunchTicket {
val dropped = launchCoordinator.pendingLink()
val ticket = launchCoordinator.beginLaunch()
dropped?.let { if (intent === it.owner) clearGameLinkIntent(it.owner) }
return ticket
}
private fun clearGameLinkIntent(link: Intent) {
link.data = null
link.action = Intent.ACTION_MAIN
link.removeExtra(GameLaunchIntent.EXTRA_APP_ID)
}
private fun launchGame(
game: Library.SteamGame,
returnHome: Boolean = false,
onHandled: () -> Unit = {},
onSettled: () -> Unit = {},
ticket: LaunchTicket? = null,
) {
if (!SessionPrefs.soundtracksIngame(this)) com.droiddeck.launcher.playback.SoundtrackController.pause(this)
if (pkgStage != null) {
ticket?.let(launchCoordinator::releaseHandling)
onHandled()
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
return
}
val request = ticket ?: beginUiLaunch()
if (!launchCoordinator.isCurrent(request)) {
onHandled()
return
}
val files = game.gameFiles
if (game.source != com.droiddeck.launcher.stores.Store.XBOX.id || files == null) {
if (startGame(game, returnHome, onHandled)) onSettled()
if (startGame(game, returnHome, onHandled, request)) onSettled()
else launchCoordinator.releaseHandling(request)
return
}
Thread({
val refused = com.droiddeck.launcher.stores.xbox.XboxManifest.needsAppModel(files)
ui.post {
when {
request != gameLaunchRequest.get() || isFinishing || isDestroyed -> onHandled()
!launchCoordinator.isCurrent(request) || isFinishing || isDestroyed -> {
launchCoordinator.releaseHandling(request)
onHandled()
}
refused -> {
onHandled()
android.widget.Toast.makeText(this, getString(R.string.game_needs_app_model, game.name), android.widget.Toast.LENGTH_LONG).show()
onSettled()
}
startGame(game, returnHome, onHandled) -> onSettled()
startGame(game, returnHome, onHandled, request) -> onSettled()
else -> launchCoordinator.releaseHandling(request)
}
}
}, "xbox-app-model").start()
}
private fun startGame(game: Library.SteamGame, returnHome: Boolean, onHandled: () -> Unit): Boolean {
if (com.droiddeck.launcher.session.NoSteamLaunch.enabled(this, game)) return launchWithoutSteam(game, returnHome, onHandled)
private fun startGame(game: Library.SteamGame, returnHome: Boolean, onHandled: () -> Unit, ticket: LaunchTicket): Boolean {
if (com.droiddeck.launcher.session.NoSteamLaunch.enabled(this, game)) return launchWithoutSteam(game, returnHome, onHandled, ticket)
onHandled()
if (SessionState.running) {
if (SessionState.mode != SessionService.MODE_STEAM || SessionState.stopRequested) {
@@ -1074,15 +1108,34 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
.apply { if (returnHome) putExtra(SessionActivity.EXTRA_LINK_TASK, task) }
}
private fun launchWithoutSteam(game: Library.SteamGame, returnHome: Boolean, onHandled: () -> Unit = {}): Boolean {
private fun launchWithoutSteam(
game: Library.SteamGame,
returnHome: Boolean,
onHandled: () -> Unit = {},
ticket: LaunchTicket? = null,
): Boolean {
if (pkgStage != null) {
onHandled()
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
return false
}
val app = applicationContext
val request = ticket ?: beginUiLaunch()
if (!launchCoordinator.isCurrent(request)) {
onHandled()
return false
}
Thread({
val added = if (com.droiddeck.launcher.session.NoSteamLaunch.needsAddedGame(game)) runCatching { com.droiddeck.launcher.session.NoSteamLaunch.addedGame(app, game) }.getOrNull() else null
val command = com.droiddeck.launcher.session.NoSteamLaunch.command(game, added)
ui.post {
onHandled()
if (isDestroyed) return@post
if (!launchCoordinator.isCurrent(request) || isFinishing || isDestroyed) {
launchCoordinator.releaseHandling(request)
return@post
}
if (command == null) {
launchCoordinator.releaseHandling(request)
android.widget.Toast.makeText(this, R.string.game_link_failed, android.widget.Toast.LENGTH_LONG).show()
return@post
}
@@ -1234,22 +1287,27 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
ui.postDelayed(poll, SESSION_SWITCH_POLL_MS)
}
private fun handleGameLink(games: List<Library.SteamGame>, scanned: Boolean = true) {
private fun handleGameLink(games: List<Library.SteamGame>, scanned: Boolean = true, ticket: LaunchTicket? = null) {
if (!lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED)) return
val request = pendingGameLink ?: return
val link = launchCoordinator.pendingLink() ?: return
if (ticket != null && link.ticket !== ticket) return
val request = link.request ?: return
if (launchCoordinator.isHandling(link.ticket)) return
if (ticket == null && !link.ownedBy(intent)) {
// A refresh may only finish the link that belongs to the intent this activity shows.
launchCoordinator.consume(link.ticket)
return
}
val game = request.find(games, byTitle = scanned)
if (!scanned && game == null) return
gameLinkTried = true
ui.postDelayed({
if (!isDestroyed && lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED)) syncDualPanel()
}, LINK_PANEL_MS)
val link = intent
val linkIntent = link.owner
fun consume() {
if (intent !== link || pendingGameLink !== request) return
pendingGameLink = null
link.data = null
link.action = Intent.ACTION_MAIN
link.removeExtra(GameLaunchIntent.EXTRA_APP_ID)
if (!launchCoordinator.consume(link.ticket)) return
if (intent === linkIntent) clearGameLinkIntent(linkIntent)
}
if (game == null) {
consume()
@@ -1257,7 +1315,10 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
android.widget.Toast.makeText(this,
if (request.program != null) R.string.game_link_program_unknown else R.string.game_link_missing,
android.widget.Toast.LENGTH_LONG).show()
} else launchGame(game, returnHome = true, onSettled = ::consume)
} else {
if (!launchCoordinator.beginHandling(link.ticket)) return
launchGame(game, returnHome = true, onSettled = ::consume, ticket = link.ticket)
}
}
private fun chooseGameShortcut(game: Library.SteamGame) {
@@ -1448,16 +1509,20 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
},
onStoreGame = { store, id ->
val key = "${store.id}:$id"
if (key !in preparingGames) {
if (pkgStage != null) {
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
} else if (key !in preparingGames) {
preparingGames = preparingGames + key
val ticket = beginUiLaunch()
val app = applicationContext
Thread({
val g = runCatching { com.droiddeck.launcher.stores.StoreLaunch.installedGame(app, store, id) }.getOrNull()
runOnUiThread {
if (g != null && !isFinishing && !isDestroyed) prepareAndLaunch(g, key)
else {
if (g != null && launchCoordinator.isCurrent(ticket) && !isFinishing && !isDestroyed) {
prepareAndLaunch(g, key, ticket)
} else {
preparingGames = preparingGames - key
if (g == null && !isFinishing && !isDestroyed) android.widget.Toast.makeText(this, R.string.stores_launch_not_registered, android.widget.Toast.LENGTH_LONG).show()
if (g == null && launchCoordinator.isCurrent(ticket) && !isFinishing && !isDestroyed) android.widget.Toast.makeText(this, R.string.stores_launch_not_registered, android.widget.Toast.LENGTH_LONG).show()
}
}
}, "store-play").start()
@@ -1960,10 +2025,9 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
gameLaunchRequest.incrementAndGet()
supersedeUncommittedLaunch()
if (intent.action == SteamIntegration.ACTION_SETUP) {
gameLinkReading = false
pendingGameLink = null
syncDualPanel()
readIntegrationIntent(intent)
return
@@ -1973,6 +2037,11 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
startSteamAtStartupIfEnabled()
}
/** A replacement activity intent supersedes anything this activity has not dispatched yet. */
private fun supersedeUncommittedLaunch() {
launchCoordinator.cancelAll()
}
private var soundtrackLaunch = false
// A restarted launcher starts before the old one stops, so each clears the hooks only if they are still its own.
@@ -2251,13 +2320,22 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
if (Build.VERSION.SDK_INT >= 33) state.getParcelable(key, Intent::class.java)
else @Suppress("DEPRECATION") state.getParcelable(key)
private fun prepareAndLaunch(g: Library.SteamGame, key: String) {
val request = gameLaunchRequest.incrementAndGet()
private fun prepareAndLaunch(g: Library.SteamGame, key: String, ticket: LaunchTicket? = null) {
if (pkgStage != null) {
preparingGames = preparingGames - key
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
return
}
val request = ticket ?: beginUiLaunch()
if (!launchCoordinator.isCurrent(request)) {
preparingGames = preparingGames - key
return
}
val done = { preparingGames = preparingGames - key }
com.droiddeck.launcher.frontend.AutoComponents.beforeLaunch(this, g, onBlocked = done) {
if (request != gameLaunchRequest.get() || isFinishing || isDestroyed) { done(); return@beforeLaunch }
if (!launchCoordinator.isCurrent(request) || isFinishing || isDestroyed) { done(); return@beforeLaunch }
com.droiddeck.launcher.stores.StoreLaunch.prepare(this, g) {
if (request == gameLaunchRequest.get() && !isFinishing && !isDestroyed) launchGame(g, onHandled = done)
if (launchCoordinator.isCurrent(request) && !isFinishing && !isDestroyed) launchGame(g, onHandled = done, ticket = request)
else done()
}
}
@@ -2267,7 +2345,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
// The queued launch was saved before this; a restored activity keeps its copy, and this one must not start it too.
queuedLaunchRunner.destroy()
triggerKeys.cancel()
gameLaunchRequest.incrementAndGet()
launchCoordinator.cancelAll()
cancelCompatLaunch()
updates.unregister()
runCatching { unregisterReceiver(shortcutsInstallStatus) }
@@ -2346,7 +2424,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
else com.droiddeck.launcher.ui.DualScreenPanel.hide(taskId)
}
private fun launchPending(): Boolean = gameLinkReading || (pendingGameLink != null && !gameLinkTried)
private fun launchPending(): Boolean = gameLinkReading || (launchCoordinator.hasPending() && !gameLinkTried)
private fun launchAndroidApp(app: HomeApp.LaunchableApp, displayId: Int?) {
val onOwnScreen = displayId == null || displayId == (display?.displayId ?: Display.DEFAULT_DISPLAY)